0

I have some code in PHP:

function get_RSAencrypt($public_key) {
    global $config;
    $rsa = new phpseclib\Crypt\RSA();
    $rsa->setEncryptionMode(phpseclib\Crypt\RSA::ENCRYPTION_PKCS1);
    $rsa->loadKey($public_key);
    $requestkey = base64_encode($rsa->encrypt($config['momo_key']));
    return $requestkey;
}  

My public key(for eg.)

-----BEGIN RSA PUBLIC KEY-----  
MEgCQQDjtTNZJnbMWXON/mhhLzENzQW8TOH/gaOZ72u6FEzfjyWSfGsP6/rMIVjY
2w44ZyqNG2p45PGmp3Y8bquPAQGnAgMBAAE=  
-----END RSA PUBLIC KEY-----  

PHP code work

Then I tried to find some example for C# .NET Framework 4.6, but not work. Can someoone provide me a link?

neubert
  • 15,947
  • 24
  • 120
  • 212
supper aban_89
  • 339
  • 1
  • 2
  • 15
  • 1
    This is a public RSA key in PKCS#1 format (PEM encoded), which is not supported by .NET Framework. An import is possible e.g. with BouncyCastle, [here](https://stackoverflow.com/a/63526681). However, the posted key is only 512 bit and therefore insecure. Nowadays 2048 bit keys are applied. – Topaco May 09 '21 at 10:01
  • @Topaco For encryption for long term confidentiality I would recommend a 4096 bit RSA key, as long as post quantum algorithms are not commonplace. – Maarten Bodewes May 09 '21 at 10:45
  • Mr supper, please note that requests for samples are considered off topic. Not that I mind overly much in this case, but next time you may want to consider that and at least phrase the question differently (e.g. "how can I ..."). – Maarten Bodewes May 09 '21 at 10:47
  • Yes thanks so much! Key was provided by another system so I need to use it. BTW, I've tried BouncyCastle. I got an error which I mentioned in this post https://stackoverflow.com/questions/67456847/c-sharp-rsa-pem-encoded-pkcs1-error-unable-to-cast-object-of-type-org-bouncyc. Please help look at this. Thanks BRO! – supper aban_89 May 09 '21 at 10:50
  • @Topaco Oh sorry! I miss your link. It work! Thanks so much BRO! – supper aban_89 May 10 '21 at 09:34

1 Answers1

1

Try this:

using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        var key = @"-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu
KUpRKfFLfRYC9AIKjbJTWit+CqvjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm
o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k
TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp7
9mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy
v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs
/5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00
-----END RSA PRIVATE KEY-----";

        var ciphertext = "L812/9Y8TSpwErlLR6Bz4J3uR/T5YaqtTtB5jxtD1qazGPI5t15V9drWi58colGOZFeCnGKpCrtQWKk4HWRocQ==";

        var ciphertextBytes = Convert.FromBase64String(ciphertext);

        var rsa = RSA.Create();
        var rx = new Regex("-+[^-]+-+");
        key = rx.Replace(key, "")
                .Replace("\r", "")
                .Replace("\n", "");
        var keyBytes = Convert.FromBase64String(key);

        rsa.ImportRSAPrivateKey(keyBytes, out _);

        var plaintextBytes = rsa.Decrypt(ciphertextBytes, RSAEncryptionPadding.Pkcs1);

        var plaintext = Encoding.Default.GetString(plaintextBytes);

        Console.WriteLine(plaintext);
    }
}

ImportRSAPrivateKey wasn't introduced until .NET Core 3.0+ but since you're using C# .NET Framework 4.6 I think you're probably good!

neubert
  • 15,947
  • 24
  • 120
  • 212