locked
Key not valid for use in specified state in RSA encryption RRS feed

  • Question

  •  RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

    private void button1_Click(object sender, EventArgs e)
            {

               var rsa = new RSACryptoServiceProvider();
                var rsaKeyPair = DotNetUtilities.GetRsaKeyPair(rsa);
                var writer = new StringWriter();
                var writer2 = new StringWriter();
                var pemWriter = new PemWriter(writer);
                var pemWriter2 = new PemWriter(writer2);
                pemWriter.WriteObject(rsaKeyPair.Public);
                pemWriter2.WriteObject(rsaKeyPair.Private);
                string Pub = writer.ToString();
                string Prv = writer2.ToString();


                plaintext = ByteConverter.GetBytes(txtplain.Text);
                encryptedtext = Encryption(plaintext, Pub, false);

    }

     static public byte[] Encryption(byte[] Data, string RSAKey, bool DoOAEPPadding)
            {
                try
                {
                    
                    byte[] encryptedData;
                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                    {
                        RSA.ImportParameters(GetRSAParameters(RSAKey));
                        encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
                    }
                    return encryptedData;
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);


                    return null;
                }


            }

    private static RSAParameters GetRSAParameters(string pPublicKey)
            {
                byte[] lDer;
                int lBeginStart = "-----BEGIN PUBLIC KEY-----".Length;
                int lEndLenght = "-----END PUBLIC KEY-------".Length;
                string KeyString = pPublicKey.Substring(lBeginStart, (pPublicKey.Length - lBeginStart - lEndLenght));
                lDer = Convert.FromBase64String(KeyString);

                RSAParameters lRSAKeyInfo = new RSAParameters();
                lRSAKeyInfo.Modulus = GetModulus(lDer);
                lRSAKeyInfo.Exponent = GetExponent(lDer);
                return lRSAKeyInfo;
            }


            private static byte[] GetModulus(byte[] pDer)
            {
                string lModulus = BitConverter.ToString(pDer).Replace("-", "").Substring(58, 256);
                return StringHexToByteArray(lModulus);
            }


            private static byte[] GetExponent(byte[] pDer)
            {
                int lExponentLenght = pDer[pDer.Length - 3];
                string lExponent = BitConverter.ToString(pDer).Replace("-", "").Substring((pDer.Length * 2) - lExponentLenght * 2, lExponentLenght * 2);

                return StringHexToByteArray(lExponent);
            }


            public static byte[] StringHexToByteArray(string hex)
            {
                return Enumerable.Range(0, hex.Length)
                                 .Where(x => x % 2 == 0)
                                 .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                                 .ToArray();
            }

    Wednesday, March 18, 2020 11:49 AM

All replies

  • Hi ProgCDB,

    Thank you for posting here.

    I try to make a test based on your code, but I need more information.

    Could you provide more details about your exception with which line of the code thrown the exception and some code about ‘ByteConverter’? It will help us to do the test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 19, 2020 8:26 AM