C# RSA with no padding

后端 未结 2 1843
-上瘾入骨i
-上瘾入骨i 2021-02-06 16:28

I\'m busy trying to port Java code that looks like this

        Cipher rsa = Cipher.getInstance(\"RSA/ECB/nopadding\");
        rsa.init(Cipher.DECRYPT_MODE, RSA         


        
相关标签:
2条回答
  • 2021-02-06 16:52

    You might want to get the code from BouncyCastle, http://www.bouncycastle.org/csharp/, and modify the code from the link below, and ensure that it can use the encryption that you list above.

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

    0 讨论(0)
  • 2021-02-06 17:13

    BouncyCastle will help us to make nopadding RSA encryption.

    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 "";
            }
        }
    

    also dont forget to put it at top:

    using Org.BouncyCastle.Crypto;
    using Org.BouncyCastle.Crypto.Engines;
    using Org.BouncyCastle.OpenSsl;
    
    0 讨论(0)
提交回复
热议问题