Hi
I am trying to convert my B4A code to B4i code without an luck. I am reading a encrypted QR Code, basically a json file and the B4A works fine but the B4i does not because the Decrypt2 method returns data that does appear to be decrypted and then the BytesToString method causes an error 'Error decoding data as string'. I see a number of threads talking about the same issue with the only answer being white spaces added to string being decrypted this is not the case here. I see the byte arrays in B4a are Signed array's and the byte array's in B4i are Unsigned. The input, IV and pass method strings are both the same in B4a and B4i versions, length and content. Is there something else I need to do? Any help most appreciated.
I am trying to convert my B4A code to B4i code without an luck. I am reading a encrypted QR Code, basically a json file and the B4A works fine but the B4i does not because the Decrypt2 method returns data that does appear to be decrypted and then the BytesToString method causes an error 'Error decoding data as string'. I see a number of threads talking about the same issue with the only answer being white spaces added to string being decrypted this is not the case here. I see the byte arrays in B4a are Signed array's and the byte array's in B4i are Unsigned. The input, IV and pass method strings are both the same in B4a and B4i versions, length and content. Is there something else I need to do? Any help most appreciated.
B4X:
'B4A Code
Sub AES_Decrypt(input As String, IV As String, pass As String) As String
Dim su As StringUtils
Dim inputB() As Byte = su.DecodeBase64(input)
Dim passB() As Byte = su.DecodeBase64(pass)
Dim IVb() As Byte = su.DecodeBase64(IV)
Dim kg As KeyGenerator
Dim C As Cipher
kg.Initialize("AES")
kg.KeyFromBytes(passB)
C.Initialize("AES/CBC/PKCS5Padding")
C.InitialisationVector = IVb
Dim datas() As Byte = C.Decrypt(inputB, kg.Key, True)
' this result is readable text
Dim result As String = BytesToString(datas, 0, datas.Length, "UTF8")
Return result
End Sub
B4X:
'B4I Code
Sub AES_Decrypt(input As String, IV As String, pass As String) As String
Dim su As StringUtils
'Dim bc As ByteConverter
Dim inputB() As Byte = su.DecodeBase64(input.Trim)
Dim passB() As Byte = su.DecodeBase64(pass)
Dim IVb() As Byte = su.DecodeBase64(IV)
Dim C As Cipher
Dim keys() As Byte = C.GenerateKey(passB, "SHA-256", Null, 0)
'C.Initialize("AES/CBC/PKCS5Padding")
'C.InitialisationVector = IVb
Dim data() As Byte = C.Decrypt2(inputB, keys,"AES", IVb, C.OPTION_PKCS7Padding)
Return BytesToString(data, 0, data.Length, "UTF-8")
End Sub
Last edited: