ALl key with Instruction given. Please Help With this RSA code to make me learn more

hello.please someone can please help me with this encryption,tried 2 days with chatgpt but couldn’t.giving the public key + all the infomation needed.please help me learn a little bit more.

FUNCTION Constant "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPPrcA1ZO5YmIPDqq/fBp6wQl3ABCDEFghijklMNOPQRSTUvwxYZ0123456789ABCDEFpTNEnXUkX5aR8kJWcy59ogGsE/cCu2+xzfkQOu9urc12dyw1zaPDVqQRSwIDAQAB" -> VAR "PUBKEY"

FUNCTION Constant "{\"account\":\"<USERNAME>\",\"password\":\"<PASSWORD>\",\"deviceId\":\"abcdef123456\"}" -> VAR "RAW"

FUNCTION Base64Encode "<RAW>" -> VAR "B64DATA"

FUNCTION CSharpCode "System.Security.Cryptography;
using System.Text;
string keyBase64 = VARIABLES[\"PUBKEY\"];
string base64Data = VARIABLES[\"B64DATA\"];
byte[] keyBytes = Convert.FromBase64String(keyBase64);
RSA rsa = RSA.Create();
rsa.ImportSubjectPublicKeyInfo(keyBytes, out _);
int chunkSize = rsa.KeySize / 8 - 11;
List<byte> result = new List<byte>();
for (int i = 0; i < base64Data.Length; i += chunkSize) {
    string part = base64Data.Substring(i, Math.Min(chunkSize, base64Data.Length - i));
    byte[] partBytes = Encoding.ASCII.GetBytes(part);
    byte[] encrypted = rsa.Encrypt(partBytes, RSAEncryptionPadding.Pkcs1);
    result.AddRange(encrypted);
}
string finalB64 = Convert.ToBase64String(result.ToArray());
string finalJson = \"{\\\"data\\\":[\\\"\" + finalB64 + \"\\\"]}\";
RETURN finalJson;" -> VAR "encrypted_payload"

the request content or Payload is like this

{"data":["zHCw2/nO9cvRMHPCTaOK5LHtEnPy38FLpv0hvQ6tY69/mZX7a0HWptWghE6gHV/PGJQg4HepjUHg8tXCOu3ByyFMYpqww0rZW8CklG7JyJsZNmD9YD8M/k4pP9tTc/6UyilvBr3wuRJliSEbas0cNotlKGLK5LLez8mle2ikD2AQl5A0yc59zUmS07n++llAR1oTK80dfxUSYQy/J9PBbnOsRAzAD/7GIjj+wBl8nr0nP7xkgpiEhyIazXNybcRnDfVipyPMQTstmqDW5Spw1fH/z1HE4mMAVO0ih0p9nv5TIwdDgETsT3B7sEek7/OcUigGJJCHq4keSmzFt9ETBA=="]}

input:
1234:1234
the payload change everytime.

1 Like

Hi @n00biE that normal test your payload with request

RSA encryption is non-deterministic means the output changes even with the same input.

(●’◡’●)

1 Like

bro,i tried put the code in OB2 but get error everytime,do you think you can make a config which generate that encryption please? thank you

BLOCK:ConstantString
  value = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPPrcA1ZO5YmIPDqq/fBp6wQl3ABCDEFghijklMNOPQRSTUvwxYZ0123456789ABCDEFpTNEnXUkX5aR8kJWcy59ogGsE/cCu2+xzfkQOu9urc12dyw1zaPDVqQRSwIDAQAB"
  => VAR @PUBKEY
ENDBLOCK

BLOCK:ConstantString
  value = $"{\"account\":\"<input.USER>\",\"password\":\"<input.PASS>\",\"deviceId\":\"abcdef123456\"}"
  => VAR @RAW
ENDBLOCK

BLOCK:UTF8ToBase64
  input = @RAW
  => VAR @b64
ENDBLOCK

     string b64Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(b64));

     
        byte[] keyBytes = Convert.FromBase64String(PUBKEY);

       
         RSA rsa = RSA.Create();
        rsa.ImportSubjectPublicKeyInfo(keyBytes, out _);

     
        int keySizeInBytes = rsa.KeySize / 8;
        int maxChunkSize = keySizeInBytes - 11;

        byte[] dataBytes = Encoding.ASCII.GetBytes(b64Data); 
        List<byte> encryptedBytes = new List<byte>();

        for (int i = 0; i < dataBytes.Length; i += maxChunkSize)
        {
            int length = Math.Min(maxChunkSize, dataBytes.Length - i);
            byte[] chunk = new byte[length];
            Array.Copy(dataBytes, i, chunk, 0, length);

            byte[] encryptedChunk = rsa.Encrypt(chunk, RSAEncryptionPadding.Pkcs1);
            encryptedBytes.AddRange(encryptedChunk);
        }

       
        string finalEncryptedB64 = Convert.ToBase64String(encryptedBytes.ToArray());

        
        var payload = new {
            data = new[] { finalEncryptedB64 }
        };

        string payloadJson = JsonConvert.SerializeObject(payload);

Here is code but your RSA key still is invalid

if you have problem you can contact me Telegram: Contact @MrRooot

1 Like