B4J Question Problem Encrypting a string using a Public Certificate

M.SALIM

Member
Below is the code I'm trying to use to encrypt string using a Public Cert (see attachment).

ENC:
Private Sub EncryptRSAWithPublicKey1(Text As String,PublicKey As String) As String
    Dim kpg     As KeyPairGenerator
    Dim Cipher  As Cipher
    Dim su      As StringUtils
    Dim pubkey() As Byte = su.DecodeBase64(PublicKey)
 
    Cipher.Initialize("RSA/ECB/PKCS1Padding")
    kpg.Initialize("RSA", 1024)
    kpg.PublicKeyFromBytes(pubkey)
    Dim bytes() As Byte = Text.GetBytes("UTF8")
  
   Return su.EncodeBase64(Cipher.Encrypt(bytes,kpg.PublicKey,False))
End Sub

But app is crashing with the following errors:
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:239)
at java.base/java.security.KeyFactory.generatePublic(KeyFactory.java:352)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper$KeyPairGeneratorWrapper.PublicKeyFromBytes(CipherWrapper.java:446)
at b4j.example.b4xmainpage._encryptrsawithpublickey1(b4xmainpage.java:833)
at b4j.example.b4xmainpage._gen2_click(b4xmainpage.java:982)


Can anyone help??
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…