RSA Encryption with custom key pair

fbritop

Active Member
Licensed User
I have a routine, in which the device request's to the server for a keypair, the server returns the public key to be stored on the device. The public key is recieved via httpjob and stored in a local database.

B4X:
'x.ReadMap is a public function to read a record from a parameters table in the database.

Dim pubkey(0) As Byte = Bconv.StringToBytes(x.ReadMap("publicKey"), "UTF8")
Dim enc As Cipher
enc.Initialize("RSA")
Dim kpg As KeyPairGenerator
kpg.Initialize("RSA", 512)
kpg.PublicKeyFromBytes(pubkey)
The last line gives me:
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.engineGeneratePublic(OpenSSLRSAKeyFactory.java:52)
   at java.security.KeyFactory.generatePublic(KeyFactory.java:171)
   at anywheresoftware.b4a.agraham.encryption.CipherWrapper$KeyPairGeneratorWrapper.PublicKeyFromBytes(CipherWrapper.java:446)
   at com.autorizalo.android.main._activity_create(main.java:534)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at com.autorizalo.android.main.afterFirstLayout(main.java:89)
   at com.autorizalo.android.main.access$100(main.java:16)
   at com.autorizalo.android.main$WaitForLayout.run(main.java:74)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4921)
   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:1027)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   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_PUBKEY(Native Method)
   at org.apache.harmony.xnet.provider.jsse.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:48)
What goes wrong? is it the UTF conversion?

Thanks in advance
Felipe Brito
 

fbritop

Active Member
Licensed User
They key is retrieved as plain text from the server:

B4X:
<RSAKeyValue><Modulus>kHndD1pdLxvJkSNq+8QaeBjKCMv/QIiiVbz78jrMibtMoprCBjZ0NxFhZVKlyRLUD3y7JXcL9gLdQj7gHuRvL/LQxfIcrbFDRKvCf7Ccg1EkV6CdTPotXeGJ+nI1qeUtVtyqImTqwnCJqd+URsnExEX8f20Caqc+8DHc86rEYBU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
Do I need to send it from the server to the device under a base64 and then decoded at the device?
 

omidaghakhani1368

Well-Known Member
Licensed User
I have a routine, in which the device request's to the server for a keypair, the server returns the public key to be stored on the device. The public key is recieved via httpjob and stored in a local database.

B4X:
'x.ReadMap is a public function to read a record from a parameters table in the database.

Dim pubkey(0) As Byte = Bconv.StringToBytes(x.ReadMap("publicKey"), "UTF8")
Dim enc As Cipher
enc.Initialize("RSA")
Dim kpg As KeyPairGenerator
kpg.Initialize("RSA", 512)
kpg.PublicKeyFromBytes(pubkey)
The last line gives me:
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.engineGeneratePublic(OpenSSLRSAKeyFactory.java:52)
   at java.security.KeyFactory.generatePublic(KeyFactory.java:171)
   at anywheresoftware.b4a.agraham.encryption.CipherWrapper$KeyPairGeneratorWrapper.PublicKeyFromBytes(CipherWrapper.java:446)
   at com.autorizalo.android.main._activity_create(main.java:534)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at com.autorizalo.android.main.afterFirstLayout(main.java:89)
   at com.autorizalo.android.main.access$100(main.java:16)
   at com.autorizalo.android.main$WaitForLayout.run(main.java:74)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4921)
   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:1027)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   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_PUBKEY(Native Method)
   at org.apache.harmony.xnet.provider.jsse.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:48)
What goes wrong? is it the UTF conversion?

Thanks in advance
Felipe Brito
Hi.
I want to use rsa encryption in my project but i dont have enough knowledge about rsa.
can you help me?
 
Top