encrypt in1st app, decrypt in 2nd app

Arrie

Member
Licensed User
Longtime User
Hi all,

in the 1st application a textbox must be filled with a nickname
this nickname is encrypted and stored in a .csv file, no problem with this part.

in the 2nd application the .csv file is loaded in a table. The encrypted nickname must be decrypted but i get an error that the arraylen(secret()) is 0 (zero)
B4X:
Sub Globals '1st app
    'Declare the global variables here.
..
..
bit.New1
crypto.New1
Dim string(0) As Byte, secret(0) As Byte 
PassPhrase = "schoen" 
..
End Sub

Sub Encrypt_Save
..
string() = Bit.StringToBytes(Nickname,0,StrLength(Nickname)) 'Convert the string to an array of bytes.
    secret() = Crypto.Encrypt(PassPhrase, string()) 'Save the encrypted data.
    For i = 0 To ArrayLen(secret())-1 'Show the encrypted data in the TextBox
        s = s & bit.DecToHex(secret(i))
    Next
    Nickname = s
s = ""
..

end sub


Sub Globals  '2nd application
    'Declare the global variables here.
..
..
Dim string(0) As Byte
Dim secret(0) As Byte 
Crypto.New1
Bit.New1
PassPhrase = "schoen"
..
End Sub

Sub Decrypt
..
Nickname = TabelDeelnemers.Cell("Nickname",2)
..
secret() = Nickname
..
If ArrayLen(secret()) = 0 Then Return
    string() = Crypto.Decrypt(PassPhrase,secret()) 'Decrypt the data.
    Nickname_Decrypted = Bit.BytesToString(string(),0,ArrayLen(string())) 'Convert the array to a string.
..
End sub
I assume the variable secret() must be set to Nickname, but i get the mentioned error. Other ways to set the secret() var. is unknown to me. I hope that someone
in this forum can help.

thanks in advance

Arrie
 

Arrie

Member
Licensed User
Longtime User
i'm using now :

Secret() = Bit.BytesToString(Nickname,0,ArrayLen(Nickname))
string() = Crypto.Decrypt(PassPhrase,Secret()) 'Decrypt the data.
Nickname_2 = Bit.BytesToString(string(),0,ArrayLen(string())) 'Convert the array to a string.

But still an error (in dutch) "de objectverwijzing is niet op een exemplaar van een object ingesteld", (in english it must be something like this) "The objectreference is not on a valid object"
 

Arrie

Member
Licensed User
Longtime User
Yes for both
Bit.new1 is in sub Globals
Bitwise.dll only for desktop (it's a desktop app)
 

Arrie

Member
Licensed User
Longtime User
Oke,

Uploaded, "Deelnemers.txt" must be renamed to "Deelnemers.csv" in this file is the encrypted data stored from the 1st application (as seen is the original post)

Arrie
 

Attachments

  • DecryptProbleem.sbp
    2 KB · Views: 186

agraham

Expert
Licensed User
Longtime User
Try this
B4X:
   Dim Secret(StrLength(Nickname)/2) As byte
   For i = 0 To ArrayLen(Secret()) - 1
      Secret(i) = Bit.HexToDec(SubString(Nickname, i*2, 2))
   Next
test1 doesn't show up in your listbox
test2 decodes as "w"
test3 decodes as "e"
test4 decodes as "Arjan"
test5 fails on bad data
 

Arrie

Member
Licensed User
Longtime User
Agraham,

Thanks, but all decrypted data i see is complete fluffy stuff. I've copied your lines of code, but still no solution.

Thanks anyway

Arrie
 

agraham

Expert
Licensed User
Longtime User
Were those decryptions that I gave correct or not? They looked like they might be, especially test4 with an inital capital letter and what seems a sensible name. What are you doing that is different
B4X:
Sub Button29_Click 'Printen gegevens deelnemers
Naam = Listbox38.Item(Listbox38.SelectedIndex)
Aantal = TabelDeelnemers.RowCount
For Teller = 1 To Aantal -1
   If TabelDeelnemers.Cell("naam", Teller) = Naam Then
         Nickname = TabelDeelnemers.Cell("Bijnaam",Teller)  'Bijnaam is dutch for Nickname
      End If
Next
   Dim Secret(StrLength(Nickname)/2) As byte
   For i = 0 To ArrayLen(Secret()) - 1
      Secret(i) = Bit.HexToDec(SubString(Nickname, i*2, 2))
   Next
   string() = Crypto.Decrypt(PassPhrase,secret()) 'Decrypt the data.
   Nickname_2 = Bit.BytesToString(string(),0,ArrayLen(string())) 'Convert the array to a string.
Msgbox(Nickname_2)
End Sub
 

Arrie

Member
Licensed User
Longtime User
Hi Agraham,

The answers you gave were correct. Especially test4 (my name is Arjan). But when i create a new nickname in the first application, then there will be an error "CryptDecrypt failed at adress 0x8009005"

It appears when the original stringlength<6 everything works fine, but when it is 6 or more the abovementioned errormessage is shown.

Arrie
 

agraham

Expert
Licensed User
Longtime User
It appears when the original stringlength<6 everything works fine, but when it is 6 or more the abovementioned errormessage is shown.
That must be something in your code. Are you truncating the encrypted string somehow? If you download the Crypto example from here Basic4ppc - Windows Mobile programming and Pocket PC Development (use only the source file together with the latest libraries from the Basic4ppc Desktop\Libraries folder) and replace the key with your key it not only produces the samre results as your tests but aalso deals OK with strings longer than 6.
 

Arrie

Member
Licensed User
Longtime User
Hi Agraham,

I'm not aware that the data is truncated. can you decrypt this string "a498a0f2283376354b96f58f3b5473f4", when the result is what i expect, the data is ok. The original line (before encryption ) is 9 digits.

Thanks,

Arrie
 

agraham

Expert
Licensed User
Longtime User
The problem is with DecToHex, it doesn't always return two characters so the data is being truncated. You need something like this.

B4X:
For i = 0 To ArrayLen(secret())-1
  If secret(i)< 16 Then s = s & "0"
  s = s & bit.DecToHex(secret(i))
Next

EDIT:- Typo in the code above corrected, was 15 now 16
 
Last edited:

Arrie

Member
Licensed User
Longtime User
Oke Agraham,

So if i understood it correct, in the encryption of the data (in the 1st application) the truncade took place, and in the decryption of the data (in the 2nd application, we must use a prefix zero if the secret datapacket is smaller then 16(dec) or F(hex), is this correct ? And if so is this than a bug or just stupid programming from me ;)

Greetings Arrie
 

agraham

Expert
Licensed User
Longtime User
we must use a prefix zero if the secret datapacket is smaller then 16(dec) or F(hex), is this correct ?
Actually smaller than 16(dec) = 10(hex). As the conversion from hex back to bytes assumes each byte is represented by a 2 character hex number then the original byte to hex must make sure that each bytes maps to a 2 character hex number. The library only produces a single hex character for values less than 16 so the "0" needs to be added to make it 2 characters.

And if so is this than a bug or just stupid programming from me ;)
Neither, just a "feature" of the library you didn't know about.
 

Arrie

Member
Licensed User
Longtime User
Oke, still working on this problem. Now if the original stringlength is unequal ie 15 etc. then the i in the For .... next statement will only go to 7. Is there a function to decide if the stringlength is unequal or equal. I don't know if i stated it correct, but i mean can the stringlength be divided by 2.

Arrie
 
Top