Android Question Cipher String with Public Key

Abílio Magalhães

Member
Licensed User
Longtime User
Hello,

I have to Cipher a specific string with a given Public Key using the RSA algorithm and encode it on Base64.

I've tried it in many ways, but can't figure out the proper one, can you elucidate me on this ?

Thanks in advance.

Regards.
 

Abílio Magalhães

Member
Licensed User
Longtime User
B4X:
Sub base64_encode_rsa_public_encrypt(md5 As String) As String
  
    Dim su As StringUtils
    Dim data() As Byte = su.DecodeBase64(File.ReadString(File.DirAssets,"pkey.txt"))
    Dim p1 As KeyPairGenerator
    Dim c1 As Cipher
    Dim r1 As ByteConverter
  
 
    p1.Initialize("RSA",1024)
    p1.PublicKeyFromBytes(data)
 
    c1.Initialize("RSA")
 
    Dim d1() As Byte
    d1 = c1.Encrypt(r1.StringToBytes(md5,"UTF8"),p1.PublicKey,False)
 
    Dim res As String
    res = su.EncodeBase64(d1)
    Return res
  
End Sub


running this code I get the following error:

B4X:
java.io.IOException: Bad Base64 input character decimal 45 in array position 0

All I want to do is to Cipher the MD5 string with the given Public Key and encode it Base64
 
Upvote 0

Abílio Magalhães

Member
Licensed User
Longtime User
After deleting the BEGIN and END from PublicKey the program pauses on this line
B4X:
p1.PublicKeyFromBytes(data)

with the following error:

B4X:
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

(...)

Caused by: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

I guess the BEGIN/END wasn't suposed to be there and the decodeBase64 causes the previous error.
 
Upvote 0

Abílio Magalhães

Member
Licensed User
Longtime User
The key was wrong, but now I've another road bump.

Here goes the code:

B4X:
Sub encripta_md5_em_rsa_base64(md5_hash As String) As String
   
   
    Dim ForeignKPG As KeyPairGenerator
    Dim c As Cipher
    Dim su As StringUtils
    Dim Bconv As ByteConverter
   
    Dim ForeignPubKeyString As String
    Dim ForeignPubKeyBytes(0) As Byte
   
    Dim MessageBytes(0),MessageBytesEncrypted(0) As Byte
    Dim MessageStringEncrypted As String
       
   
   
   
    ' inicializa
  
    c.Initialize("RSA")
    ForeignKPG.Initialize("RSA", 2048)
    ForeignPubKeyString = File.ReadString(File.DirAssets, "pkey.txt").Trim

    ' converte a chave pública, faz o seu decode
    ForeignPubKeyBytes = su.DecodeBase64(ForeignPubKeyString)
   
    ForeignPubKeyString = Bconv.StringFromBytes(ForeignPubKeyBytes, "UTF8")
   
    ' instancia a chave
    ForeignKPG.publicKeyFromBytes(ForeignPubKeyBytes)
   
   
    ' converte o md5_hash em bytes
    MessageBytes = Bconv.StringToBytes(md5_hash, "UTF8")
   
    ' encripta
    MessageBytesEncrypted = c.encrypt(MessageBytes, ForeignKPG.PublicKey, False)
   
    ' encode em Base64
    MessageStringEncrypted = su.EncodeBase64(MessageBytesEncrypted)
    Return MessageStringEncrypted
   
End Sub


It stops on this line:

B4X:
ForeignKPG.publicKeyFromBytes(ForeignPubKeyBytes)

With the following error:

B4X:
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

Caused by: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag


any help pls?
 
Upvote 0

Abílio Magalhães

Member
Licensed User
Longtime User
Could you post how pkey.txt is created (and the content)?

Well I just noticed that the key itself is not a regular Public Key, it is a certificate.
I guess I've to extract the Public Key from the certificate.
Is there a way to do it in-code or I've to do it in the command line?
 
Upvote 0
Top