Android Question Problem with simple encryption

Bpick

Member
Licensed User
Longtime User
I am trying to learn the encryption methods, and in my sample, I'm failing.. big time.

This is the code
B4X:
Log ("starting encryption")
   Dim kg As KeyGenerator
   Dim c As Cipher
   Dim B64 As Base64
   Dim Bconv As ByteConverter

   Dim data(0) As Byte
   Dim iv(0) As Byte
   iv = Array As Byte(3,14,159,26,53,58,97,93) ' 16 bytes for AES
Log ("Completed dimming")   
   c.Initialize("DESEDE/CBC/PKCS5Padding") 
    Log ("DES padded")
   c.InitialisationVector = iv
    Log ("IV initialised")
   kg.Initialize("DESEDE")
    Log ("kg.initialized")
   kg.KeyFromBytes(Bconv.StringToBytes(Key,"UTF-8"))
   Log("Bconv.stringtobytes done")
   data = Bconv.StringToBytes(str, "UTF-8")    
   Log ("Data to encrypt done")
    Log (kg.Key)
    data = c.Encrypt(data, kg.Key, False)    '<---- Fails here
   Log ("data encypted w/key")

The problem is a "java.security.InvalidKeyException: src.length=3 srcPos=0 dst.length=8 dstPos=0 length=8"
kg.Key=javax.crypto.spec.SecretKeySpec@119

Here is the original code from this page: http://www.b4x.com/android/forum/threads/help-on-encryption-library-please.12638/#post-175924
B4X:
Sub Encrypt(dataToEncrypt As String ) As String

   Dim kg As KeyGenerator
   Dim c As Cipher
   Dim B64 As Base64
   Dim bconv As ByteConverter

   Dim data(0) As Byte
   Dim iv(0) As Byte
   iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
    
   c.Initialize("DESEDE/CBC/PKCS5Padding")  
   c.InitialisationVector = iv
   kg.Initialize("DESEDE")
 
   kg.KeyFromBytes(bconv.StringToBytes("1234567890123456","ASCII"))
   data = Bconv.StringToBytes(dataToEncrypt, "ASCII")    
   data = c.Encrypt(data, kg.Key, True)          

   Return B64.EncodeBtoS(data, 0, data.Length)

I am having problems making sense of the code.
Key is a previous password that has been established.
 
Top