Android Question Help!! Error in Digital Signature

Abílio Magalhães

Member
Licensed User
Longtime User
Hello to all,

I've got a problem in my Application, which is really strange.

I've an error when I want to sign a digital signature, based on RSA 1024, in Android 4.2.2, but when I run my App on Android 4.0.4 it just runs correctly.

Here's the code

B4X:
Dim xbytes()    As Byte
Dim xchave        As String
Dim sf            As StringFunctions
Dim str                As String
str = File.ReadString(File.DirAssets, "file.txt")
str = sf.Decrypt(str)

xchave = str

Dim private_key_byte As StringUtils
xbytes = private_key_byte.DecodeBase64(xchave)

Dim key_ As KeyPairGenerator
    
key_.Initialize("RSA",1024)
  
key_.PrivateKeyFromBytes(xbytes)

1. I locate the file to decrypt.
2. I decrypt the file , and I've already made a Log and it decrypts it correctly.
3. When I run the key_.PrivateKeyFromBytes(xbytes) line the App 'crashes', and I got this Log error.

B4X:
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
    at org.apache.harmony.xnet.provider.jsse.OpenSSLRSAKeyFactory.engineGeneratePrivate(OpenSSLRSAKeyFactory.java:77)
    at java.security.KeyFactory.generatePrivate(KeyFactory.java:186)
    at anywheresoftware.b4a.agraham.encryption.CipherWrapper$KeyPairGeneratorWrapper.PrivateKeyFromBytes(CipherWrapper.java:457)
    at b4a.tapinvoice.cls_doccab._certificadocumentosaftpt(cls_doccab.java:1439)
    at b4a.tapinvoice.main._funcao_finaliza_documento(main.java:7864)
    at b4a.tapinvoice.main._botao_imprimir_click(main.java:2465)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:161)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:157)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
    at android.view.View.performClick(View.java:4207)
    at android.view.View$PerformClick.run(View.java:17372)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5042)
    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:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(Native Method)
    at org.apache.harmony.xnet.provider.jsse.OpenSSLRSAKeyFactory.engineGeneratePrivate(OpenSSLRSAKeyFactory.java:73)
    ... 22 more

Is there a problem running the App on Android 4.2.2?

I can't get where the problem lives, and mostly, why when I run it on 4.0.4 it just plays perfectly.


Thank you all.
Best regards.
 

Abílio Magalhães

Member
Licensed User
Longtime User
Hi Erel,

We have confirmed here that the byte Array is exactly the same either on 4.0.4 and 4.2.2.

We've already experimented inserting the Array bytes into the source code, to make shure that the parameters were working fine, but the App continue crashing at the same line of code, described up here.

B4X:
Dim key_ As KeyPairGenerator
key_.Initialize("RSA",1024)

'It crashed on the next line
key_.PrivateKeyFromBytes(bytes)


'After we do this
Dim ByteEnc AsByteConverter

Dim MyMac As Mac

MyMac.Initialise("HMAC-SHA1",key_.PrivateKey)

MyMac.Update(ByteEnc.StringToBytes(textToSign,"utf-8"))

Dim x AsStringUtils

SAFPTHash = x.EncodeBase64(MyMac.Sign())

  Dim xsign As Signature

xsign.Initialise("SHA1withRSA",xsign.SIGNATURE_SIGN,key_.PrivateKey)

xsign.Update(ByteEnc.StringToBytes(textToSign,"utf-8"))

SAFPTHash = x.EncodeBase64(xsign.Sign())

Since we are completely stopped on the line key_.PrivateKeyFromBytes(bytes) (on OS 4.2.2), can you show us another way to do this?

We've never used JavaObject lib., so we're not understanding how can we use it to solve this issue.
 
Upvote 0

Abílio Magalhães

Member
Licensed User
Longtime User
The key was created on the Macintosh terminal (Linux), with this prompt

cmd> openssl genrsa -out PrivateKey.pem 1024

This line creates a Private Key with RSA algorithm with 1024bts.

Then We've created a Public Key based on this Private Key, with the following prompt.

cmd> openssl rsa -in PrivateKey.pem -out PublicKey.pem -outform PEM –pubout

Then we've created a StringFunction variable to decrypt our key, then we've converted it to bytes (after the decoding) so sign it.

to sum up, the bytes come from de DecodeBase64 of the private key, to do the signature.
 
Upvote 0
Top