B4J Question RSA Encription problem

Pablo Torres

Active Member
Licensed User
Hi, I have the following code

B4X:
    Dim su As StringUtils
                       
    Dim Bconv As ByteConverter
       
    Dim ForeignPubKeyString As String
    Dim ForeignPubKeyBytes(0) As Byte
    Dim ForeignPrivKeyBytes(0) As Byte
    Dim ForeignPrivKeyString As String
       
    Dim MessageBytes(0),MessageBytesEncrypted(0) As Byte
    Dim MessageStringEncrypted As String
       
    Dim ForeignKPG As KeyPairGenerator
    Dim c As Cipher
    c.Initialize("RSA/ECB/PKCS1Padding")
    ForeignKPG.Initialize("RSA", 2048)
    ForeignPubKeyString = File.ReadString(File.DirApp & "\certs\srv\CAPTICKET\","public.cer")
                       
    'Convert the Public Key
    ForeignPubKeyString=ForeignPubKeyString.Replace("-----BEGIN CERTIFICATE-----","")
    ForeignPubKeyString=ForeignPubKeyString.Replace("-----END CERTIFICATE-----","")
    ForeignPubKeyBytes=su.DecodeBase64(ForeignPubKeyString)
'******************THIS IS WHERE OT GETS THE ERROR********************
    ForeignKPG.publicKeyFromBytes(ForeignPubKeyBytes)
'********************************************************************    ForeignPubKeyString=Bconv.StringFromBytes(ForeignPubKeyBytes, "UTF8")
           
    'Convert the Private Key
    ForeignPrivKeyString = File.ReadString(File.DirApp & "\certs\srv\CAPTICKET\","private.key")
    ForeignPrivKeyString=ForeignPrivKeyString.Replace("-----BEGIN PRIVATE KEY-----","")
    ForeignPrivKeyString=ForeignPrivKeyString.Replace("-----END PRIVATE KEY-----","")
    ForeignPrivKeyBytes=su.DecodeBase64(ForeignPrivKeyString)
    ForeignKPG.PrivateKeyFromBytes(ForeignPrivKeyBytes)
    ForeignPrivKeyString=Bconv.StringFromBytes(ForeignPrivKeyBytes, "UTF8")
                   
                       
    'Local test with server's public & private key
    MessageBytes = Bconv.StringToBytes(text, "UTF8")
    MessageBytesEncrypted = c.encrypt(MessageBytes, ForeignKPG.PublicKey, False)
    MessageStringEncrypted=Bconv.HexFromBytes(MessageBytesEncrypted)
    MessageStringEncrypted=su.EncodeBase64(MessageBytesEncrypted)
   
    Return MessageStringEncrypted
I dont know what It's wrong, I'm just trying to encode a string (text) using RSA2048 and a public Key I created

This is the error Log:

Error occurred on line: 204 (Main)
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = 2)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:205)
at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper$KeyPairGeneratorWrapper.PublicKeyFromBytes(CipherWrapper.java:446)
at b4j.example.main._encrypttext(main.java:437)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at b4j.example.main.main(main.java:29)
Caused by: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = 2)
at sun.security.x509.X509Key.decode(X509Key.java:398)
at sun.security.x509.X509Key.decode(X509Key.java:403)
at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:84)
at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:298)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:201)
... 18 more

I'm also uploading the public cert and the private key files

Any help will be very appreciated
 

Attachments

Pablo Torres

Active Member
Licensed User
I managed to run the example, and also managed to get it working with .pem keys (private.pem, public.pem), but I cant get it working with public.cer and private.key, and thats what I need.
I understand that there is a diference between .key, .pem and .cer, nut I´m not able to solve it

Any help?
 
Top