Android Question Problem with RSA/openSSL and PHP

chefe82

Member
Licensed User
Longtime User
Hello,

I went through the following tutorial
https://www.b4x.com/android/forum/threads/using-rsa-in-b4a-to-communicate-with-php-openssl.59445/


I get the following error message

javax.crypto.IllegalBlockSizeException: input must be under 256 bytes
at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:246)
at javax.crypto.Cipher.doFinal(Cipher.java:1502)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.doFinal(CipherWrapper.java:140)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.Encrypt(CipherWrapper.java:160)
at de.abstellung.suedwest.regio.frm_schichten._jobdone(frm_schichten.java:1183)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:996)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

This is my B4A Code

B4X:
    If res.Length<12 Then
                    Msgbox(res,"Error")
                Else
                    Dim KeyL As List
                    Dim KeyMap As Map
                    KeyL = parser.NextArray
                    If KeyL.Size > 0 Then
                        For i =0 To KeyL.Size -1
                            KeyMap = KeyL.Get(i)
                            ForeignPubKeyString=KeyMap.Get("PubKey")
                            ForeignPrivKeyString=KeyMap.Get("PrivKey")
                           
                        Next
           
                    End If
               
                    Log("Responce: " & Job.GetString)
               
                    c.Initialize("RSA/ECB/PKCS1Padding")
                    ForeignKPG.Initialize("RSA", 2048)
                 
                    'Convert the Public Key
                    ForeignPubKeyBytes=su.DecodeBase64(ForeignPubKeyString)
                    ForeignPubKeyString=Bconv.StringFromBytes(ForeignPubKeyBytes, "UTF8")
                    Log (ForeignPubKeyString)
                 
                    ForeignPubKeyString=ForeignPubKeyString.Replace("-----BEGIN PUBLIC KEY-----","")
                    ForeignPubKeyString=ForeignPubKeyString.Replace("-----END PUBLIC KEY-----","")
     
                    Log ("Truncated Pub: " & ForeignPubKeyString)

                    Log (ForeignPrivKeyString)
                 
                    'Convert the Private Key
                    ForeignPrivKeyBytes=su.DecodeBase64(ForeignPrivKeyString)
                    ForeignPrivKeyString=Bconv.StringFromBytes(ForeignPrivKeyBytes, "UTF8")
                    Log (ForeignPrivKeyString)
             
                    ForeignPrivKeyString=ForeignPrivKeyString.Replace("-----BEGIN PRIVATE KEY-----","")
                    ForeignPrivKeyString=ForeignPrivKeyString.Replace("-----END PRIVATE KEY-----","")
                 
                    Log ("Truncated Private: " & ForeignPubKeyString)
               
                    ForeignPubKeyBytes=su.DecodeBase64(ForeignPubKeyString)
                    ForeignKPG.publicKeyFromBytes(ForeignPubKeyBytes)
                 
                    ForeignPrivKeyBytes=su.DecodeBase64(ForeignPrivKeyString)
                    ForeignKPG.PrivateKeyFromBytes(ForeignPrivKeyBytes)
               
               
                    MessageBytes = Bconv.StringToBytes(res, "UTF8")
                    MessageBytesEncrypted = c.encrypt(MessageBytes, ForeignKPG.PublicKey, False)
                    MessageBytesDecrypted= c.Decrypt(MessageBytesEncrypted,ForeignKPG.PrivateKey, False)
                    MessageStringDecrypted=Bconv.StringFromBytes(MessageBytesDecrypted,"UTF8")
               

               
                    Log ("Local decrypt: " & MessageStringEncrypted)
                    '

                End If
My PHP Code
[PHP]<?php

$pub = file_get_contents('PublicKey.pem');
$pri = file_get_contents('PrivateKey.pem');


$plain = 'Hello RSA Das ist ein Test der verschlüsselung mit RSA';


openssl_public_encrypt($plain, $encrypted, $pub);
$messEN=base64_encode($encrypted);
Print ($messEN);



?>[/PHP]

Log Encrypted Message

Response from server: VLJPYPHog6ipavze6bJGuZuMHQ4oz/mhUBtzpNcWPy+1ZzXOSK5gGXyDTx7SvnJnQG8JBKR5kNQ81AdMAqC54o6E/NAvuo788AyqOyiAXbr13QNFMoEaJ5hIwa3/F7Bt8e+NFaDTgFV6QbZ6mitlueBGT1Di0GFr78XGnGJGQwGNwqmA8LZ8AlzoH2GamX876+6G5NxUnGfAaSMcX8sA7D7cB0t90pz3Jch4WFlxX7Y4FPK9WI0UgOL1qa8Sis2BWMMt+Bnd9ryV3ez28xgHQn29f6jYbq7LKcgEMrPDNlfdVd1YWDecilxRQ0zeurl5iOPjAx4DA1H/U7OpU/usww==
Length of res: 344

Can someone help me there
 

DonManfred

Expert
Licensed User
Longtime User
javax.crypto.IllegalBlockSizeException: input must be under 256 bytes
sounds like the blocksize should be less or equal to 256 bytes.
ForeignKPG.Initialize("RSA", 2048)
Try to use 256 here

Based on the tutorial you should create the keys on windowsplatform using the batch file.
 
Last edited:
Upvote 0

chefe82

Member
Licensed User
Longtime User
Error continues, I have it just as in the tutorial made with the batch file where in the examlbe is thereby.
 
Upvote 0
Top