Howdy.
This code is working PERFECT! thanks for share it. I am only show in this code the Decrypt AES
I would like to capture or handle when the cHashKey="12345" is different, I mean, when I put this code with a wrong cHashKey on purpose. That mean, if some one try to do something wrong (try to guess or use brute force), the APP must show a message. But really I don't know how to capture the error.
when I did the test, the line with this code. decrypted = C.Decrypt(decrypted, kg.Key, False) reproduce an error, obviously because the cHashKey is not the same ,it does not match. I would like to send a message to end user when the cHashkey does not match. and of course avoid the APP stop in that point.
I will very appreciate any light in the end of the tunnel
Sub Process_Globals
Type Message (Address As String, Body As String)
Dim cHashKey As String
cHashKey = "12345"
End Sub
Sub Activity_Create(FirstTime As Boolean)
YadescifradoAES=DecryptAES(mystringsAES)
Log("Descifrado en AES= " & YadescifradoAES)
ToastMessageShow("AES descifrado = " & YadescifradoAES,True)
End Sub
Sub DecryptAES(dataToDecrypt As String) As String
Dim su As StringUtils
Dim kg As KeyGenerator
Dim C As Cipher
Dim md As MessageDigest
Dim decrypted() As Byte
Dim BConv As ByteConverter
If cHashKey.Trim = "" Then Return False
kg.Initialize("AES")
kg.KeyFromBytes(md.GetMessageDigest(cHashKey.GetBytes("UTF8"), "MD5"))
C.Initialize("AES/ECB/PKCS5Padding")
decrypted = su.DecodeBase64(dataToDecrypt)
decrypted = C.Decrypt(decrypted, kg.Key, False)
Return BConv.StringFromBytes(decrypted, "UTF8")
End Sub
The message in Logs in B4A IDE
decrypted = C.Decrypt(decrypted, kg.Key, True)
java.lang.NullPointerException: iv == null
at javax.crypto.spec.IvParameterSpec.<init>(IvParameterSpec.java:45)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.doFinal(CipherWrapper.java:135)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.Decrypt(CipherWrapper.java:150)
at my.exampleSmsReply.testmodule._decryptaes(testmodule.java:205)
at my.exampleSmsReply.testmodule._parsesmsintent(testmodule.java:406)
at my.exampleSmsReply.testmodule._service_start(testmodule.java:541)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at my.exampleSmsReply.testmodule.handleStart(testmodule.java:93)
at my.exampleSmsReply.testmodule.onStartCommand(testmodule.java:68)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2568)
at android.app.ActivityThread.access$2000(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4947)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)