Android Question Calling KeyFromBYtes on a ByteArray

gregmatthews

Member
Licensed User
Hi,
I need to decrypt an AES encrypted string within B4A, I have the key and IV used for encryption. I am having trouble setting up Cipher to decrypt.

B4X:
iv = Array As Byte(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6) ' 16 bytes for AES
mykey = Array As Byte(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6) ' 16 bytes for AES
c.Initialize("AES")
c.InitialisationVector = iv
kg.KeyFromBytes(mykey)

The last line creates a Java exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String javax.crypto.KeyGenerator.getAlgorithm()' on a null object reference

Any ideas?
G.
 

gregmatthews

Member
Licensed User
Oh, so close now, all the right characters decrypted, though not necessarily in the right positions...
B4X:
Dim bconv As ByteConverter
Dim data(0) As Byte
data = B64.DecodeStoB(encryptedData)
decryptedData = c.Decrypt(data, kg.Key, True)
decryptedString = bconv.StringFromBytes(decryptedData, "ASCII")

Results in decrypted data having an array of bytes with the Ascii characters interspersed with zeroes
decryptedString is then the correct characters, interspersed with nulls
I've tried UTF-8 and ASCII in the StringFromBytes, both give the interspersed nulls
I've tried UTF-16 and get Chinese
What am I missing/not understanding?
(Also tried the above with littlendian set to true)
 
Last edited:
Upvote 0

gregmatthews

Member
Licensed User
Oh, I'm so ashamed, I've had to knife and fork it by copying every 2nd byte to a new array and then convert that to a string. I hope that approach is so offensive to someone's programming sensibilities that can help me correct my aberrant code.
 
Upvote 0
Top