C # RSA without padding

I am busy trying to port Java code that looks like this:

        Cipher rsa = Cipher.getInstance("RSA/ECB/nopadding");
        rsa.init(Cipher.DECRYPT_MODE, RSAPrivateKey);
        decryptedData = rsa.doFinal(data, 0, 128);

to C #, but as RSACryptoServiceProvider seems, forces you to use ODAP or PKCS1. I know that laying is not safe, but in this case Im works with a closed source client, so I can not do anything about it. Is there any way around this problem?

+3
source share
2 answers

You might want to get the code from BouncyCastle, http://www.bouncycastle.org/csharp/ and change the code from the link below and make sure that it can use the encryption mentioned above.

http://www.java2s.com/Code/Java/Security/Whatisinbouncycastlebouncycastle.htm

+1

BouncyCastle nopadding RSA-.

public string RsaEncryptWithPublic(string clearText, string publicKey)
    {
        //  analogue of Java:
        //  Cipher rsa = Cipher.getInstance("RSA/ECB/nopadding");
        try
        {
            var bytesToEncrypt = Encoding.ASCII.GetBytes(clearText);

            var encryptEngine = new RsaEngine(); // new Pkcs1Encoding (new RsaEngine());


            using (var txtreader = new StringReader("-----BEGIN PUBLIC KEY-----\n" + publicKey+ "\n-----END PUBLIC KEY-----"))
            {
                var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();

                encryptEngine.Init(true, keyParameter);
            }

            var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
            return encrypted;
        }
        catch 
        {

            return "";
        }
    }
0

Source: https://habr.com/ru/post/1719847/


All Articles