Need help understanding Crypto

tsteward

Well-Known Member
Licensed User
Longtime User
I need help understanding how to use crypto.
Below is the code from the help file.
I have modified the sub so as to attempt to decrypt a string that had been previously encrypted.
Obviously I am doing it wrong.
Please run the code and click decrypt without doing anything else. I am trying to supply and encrypted string and decode it.

B4X:
Sub Globals
      Dim string(0) As Byte, secret(0) As Byte 
                        Dim NewArray(0)
      PassPhrase = "my key" 'This is not recommended in real applications!!!   
End Sub


Sub App_Start
      Form1.Show
      Bit.New1
      Crypto.New1
End Sub


Sub btnEncrypt_Click
      string() = Bit.StringToBytes(txtString.Text,0,StrLength(txtString.Text)) '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
      txtString.Text = s
End Sub


Sub btnDecrypt_Click
      '4ba21d28c8bf314927966675a8f1a46c this is "Hello World" encrypted
      secret()=Bit.StringToBytes("4ba21d28c8bf314927966675a8f1a46c",0,StrLength("4ba21d28c8bf314927966675a8f1a46c"))
      string() = Crypto.Decrypt(PassPhrase,secret()) 'Decrypt the data.
      txtString.Text = Bit.BytesToString(string(),0,ArrayLen(string())) 'Convert the array to a string.
End Sub
 

agraham

Expert
Licensed User
Longtime User
Top