encrypt in1st app, decrypt in 2nd app

Discussion in 'Questions (Windows Mobile)' started by Arrie, Jun 1, 2008.

  1. Arrie

    Arrie Member Licensed 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)
    Code:
    Sub Globals '1st app
        'Declare the global variables here.
    ..
    ..
    bit.New1
    crypto.New1
    Dim string(0As Byte, secret(0As 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(0As Byte
    Dim secret(0As 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Secret is an array of bytes, so you can't directly assign a string to secret.
    You need to again use Secret() = Bit.StringToBytes(...)
     
  3. Arrie

    Arrie Member Licensed 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"
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Did you add a reference to the Bitwise library?
    Did you add a Bitwise object named Bit?
     
  5. Arrie

    Arrie Member Licensed User

    Yes for both
    Bit.new1 is in sub Globals
    Bitwise.dll only for desktop (it's a desktop app)
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    It is not recommended to put Bit.New1 in Sub Globals.
    Can you upload your code?
     
  7. Arrie

    Arrie Member Licensed 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
     

    Attached Files:

  8. agraham

    agraham Expert Licensed User

    Try this
    Code:
    Dim Secret(StrLength(Nickname)/2As byte
       
    For i = 0 To ArrayLen(Secret()) - 1
          Secret(i) = 
    Bit.HexToDec(SubString(Nickname, i*22))
       
    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
     
  9. Arrie

    Arrie Member Licensed 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
     
  10. agraham

    agraham Expert Licensed 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
    Code:
    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)/2As byte
       
    For i = 0 To ArrayLen(Secret()) - 1
          Secret(i) = 
    Bit.HexToDec(SubString(Nickname, i*22))
       
    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
     
  11. Arrie

    Arrie Member Licensed 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
     
  12. agraham

    agraham Expert Licensed User

    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.
     
  13. Arrie

    Arrie Member Licensed 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
     
  14. agraham

    agraham Expert Licensed User

    No, that fails with a bad data error. What was the original data?
     
  15. agraham

    agraham Expert Licensed User

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

    Code:
    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: Jun 2, 2008
  16. Arrie

    Arrie Member Licensed User

    agraham @ 9:34 Original was Basic4PPC
     
  17. Arrie

    Arrie Member Licensed 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
     
  18. agraham

    agraham Expert Licensed User

    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.

    Neither, just a "feature" of the library you didn't know about.
     
  19. Arrie

    Arrie Member Licensed 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
     
  20. agraham

    agraham Expert Licensed User

    You seem to be looking at the problem from the wrong end. The solution is in my post #15 above which you put in the encryption app to make sure the string length is an even number of characters.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice