Android Question [Solved]RSA-Encryption: Load Public Key from PHP (OpenSSL)

KMatle

Expert
Licensed User
Hi guys,

I'm trying to load this key (from PHP OpenSSL)

B4X:
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv+1jf5H71+DKLmfbTOJg t5chRSsYD2SS/hu5q8LjiZhgzU9H3KbMcoIKHQrQw5JP1tVjVFoeNRvHxYLuY0m6 mKC+mUjMZcpbwvzvDgU6Py2UABPrlnA2WmpfnlVZxDifxkyV1AhMsLQzo0mmkO48 Jvo4YvpIs5/impp2jUVxjEmI968gCNP8WvZWQJoQA8MReUbckCD40PKzS+y6xtiN fYJD0dEEvar/B8TzkbaZN+KRM4SMIXHrqoMBt2tLQx6SLcxgU8472Jtp6VqK65oX h4A7RqolbncYgxR23v7zmoI3rQWmNDb5KA2oUcH7tAzViFZ8HRnMUQSG2PGF7wC/ YwIDAQAB -----END PUBLIC KEY-----
by

B4X:
 c.Initialize("RSA/ECB/PKCS1Padding")
 ForeignKPG.Initialize("RSA", 2048)
                     
 ForeignPubKeyString=Job.GetString
 Log (ForeignPubKeyString)
                     
 ForeignPubKeyBytes=Bconv.stringtoBytes(ForeignPubKeyString,"UTF8")
 ForeignKPG.publicKeyFromBytes(ForeignPubKeyBytes)
gives me

B4X:
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
I played arround with other Ciphers but no success. Maybe I'm blind here. Any ideas?
 

Blue.Sky

Active Member
Licensed User
Hi
I use attach class to encryption and decryption data with RSA and AES algorithm
I hope that it be useful
 

Attachments

KMatle

Expert
Licensed User
Well, I can now load the public key and ecrypt a message like this:

B4X:
                      Dim su As StringUtils
                      Dim mess As String
                      Dim MessBytes(0) As Byte
                      c.Initialize("RSA/NONE/NoPadding")
                        ForeignKPG.Initialize("RSA", 2048)
                    
                      ForeignPubKeyString=Job.GetString
                      Log (ForeignPubKeyString)
                      ForeignPubKeyString=ForeignPubKeyString.Replace("-----BEGIN PUBLIC KEY----- ","")
                      ForeignPubKeyString=ForeignPubKeyString.Replace(" -----END PUBLIC KEY-----","")
                      Log (ForeignPubKeyString)
                    
                      ForeignPubKeyBytes=su.DecodeBase64(ForeignPubKeyString)
                      ForeignKPG.publicKeyFromBytes(ForeignPubKeyBytes)
                    
                      mess="Hallo"                    
                      MessageBytes = Bconv.StringToBytes(mess, "UTF8")
                    
                      MessageBytesEncrypted = c.encrypt(MessageBytes, ForeignKPG.PublicKey, False)
                      MessageStringEncrypted=Bconv.HexFromBytes(MessageBytesEncrypted)
Log (JobDone):

B4X:
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Back from Job:GetPubkey
Response from server: -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv+1jf5H71+DKLmfbTOJg t5chRSsYD2SS/hu5q8LjiZhgzU9H3KbMcoIKHQrQw5JP1tVjVFoeNRvHxYLuY0m6 mKC+mUjMZcpbwvzvDgU6Py2UABPrlnA2WmpfnlVZxDifxkyV1AhMsLQzo0mmkO48 Jvo4YvpIs5/impp2jUVxjEmI968gCNP8WvZWQJoQA8MReUbckCD40PKzS+y6xtiN fYJD0dEEvar/B8TzkbaZN+KRM4SMIXHrqoMBt2tLQx6SLcxgU8472Jtp6VqK65oX h4A7RqolbncYgxR23v7zmoI3rQWmNDb5KA2oUcH7tAzViFZ8HRnMUQSG2PGF7wC/ YwIDAQAB -----END PUBLIC KEY-----
Length of res: 450
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv+1jf5H71+DKLmfbTOJg t5chRSsYD2SS/hu5q8LjiZhgzU9H3KbMcoIKHQrQw5JP1tVjVFoeNRvHxYLuY0m6 mKC+mUjMZcpbwvzvDgU6Py2UABPrlnA2WmpfnlVZxDifxkyV1AhMsLQzo0mmkO48 Jvo4YvpIs5/impp2jUVxjEmI968gCNP8WvZWQJoQA8MReUbckCD40PKzS+y6xtiN fYJD0dEEvar/B8TzkbaZN+KRM4SMIXHrqoMBt2tLQx6SLcxgU8472Jtp6VqK65oX h4A7RqolbncYgxR23v7zmoI3rQWmNDb5KA2oUcH7tAzViFZ8HRnMUQSG2PGF7wC/ YwIDAQAB -----END PUBLIC KEY-----

"Raw Public Key"
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv+1jf5H71+DKLmfbTOJg t5chRSsYD2SS/hu5q8LjiZhgzU9H3KbMcoIKHQrQw5JP1tVjVFoeNRvHxYLuY0m6 mKC+mUjMZcpbwvzvDgU6Py2UABPrlnA2WmpfnlVZxDifxkyV1AhMsLQzo0mmkO48 Jvo4YvpIs5/impp2jUVxjEmI968gCNP8WvZWQJoQA8MReUbckCD40PKzS+y6xtiN fYJD0dEEvar/B8TzkbaZN+KRM4SMIXHrqoMBt2tLQx6SLcxgU8472Jtp6VqK65oX h4A7RqolbncYgxR23v7zmoI3rQWmNDb5KA2oUcH7tAzViFZ8HRnMUQSG2PGF7wC/ YwIDAQAB

Encrypted Message:
B215D6FB307B6BAD3D3F1D094FF8C673714DACFCF8EA30B3C59C9800985D7F974DFA9C176C5C7F62F72B4C646B7774EA57547D392121599FEB671D3E3E76001F1D5B59BEAD3E4374AB1CC90F38156C120487BD84D2ECC2F92F93EC2816020A52945D6EB71B17BFDDAB6D455F9C6083DE8313006E66800E2CF1D1C9A448216B8A96013C702B2DEB8603A3F37E4458C11554C93D0C348E9A4ED2BDCC6120B5D99F29FA87C3063D3157B4B8CD3D00B95A065C8AAB7436792DC8105A039050221F33B529E7E77882049634DC093A15FB5BA05508A84706F119E28B7874FBA551CDFC367B7997D1C0220753F80D2944CCBA0346714DA5D62AF4E0A21488614FBA64E5
 
Top