コード例 #1
0
ファイル: RSAUtility.cs プロジェクト: shermanflan/net-methods
        public static string Decrypt(byte[] cipherText, string cnxnString)
        {
            byte[] decryptedData;
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                RSAUtilityInfo info = ReadParams(cnxnString);
                string         publicKey;
                string         privateKey;

                if (info == null) // shouldn't happen
                {
                    publicKey  = rsa.ToXmlString(includePrivateParameters: false);
                    privateKey = rsa.ToXmlString(includePrivateParameters: true);

                    WriteParams(publicKey, privateKey, cnxnString);
                }
                else
                {
                    publicKey  = info.PublicKey;
                    privateKey = info.PrivateKey;
                }

                rsa.FromXmlString(privateKey);
                decryptedData = rsa.Decrypt(cipherText, false);

                return(Encoding.UTF8.GetString(decryptedData));
            }
        }
コード例 #2
0
ファイル: RSAUtility.cs プロジェクト: shermanflan/net-methods
        public static byte[] Encrypt(string clearText, string cnxnString)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                string publicKey;
                string privateKey;

                RSAUtilityInfo info = ReadParams(cnxnString);

                if (info == null)
                {
                    publicKey  = rsa.ToXmlString(includePrivateParameters: false);
                    privateKey = rsa.ToXmlString(includePrivateParameters: true);

                    WriteParams(publicKey, privateKey, cnxnString);
                }
                else
                {
                    publicKey  = info.PublicKey;
                    privateKey = info.PrivateKey;
                }

                byte[] dataToEncrypt = Encoding.UTF8.GetBytes(clearText);

                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSA.FromXmlString(publicKey);
                    return(RSA.Encrypt(dataToEncrypt, false));
                }
            }
        }