des和RSA两种加密方式的例子

 static public string RSA_Encrypt(string str_Plain_Text, out string str_Public_Key, out string str_Private_Key)
    {
        str_Public_Key = "";
        str_Private_Key = "";
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
        try
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
            str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));

            //OAEP padding is only available on Microsoft Windows XP or later. 
            byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
            str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
            str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
            string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
            return str_Cypher_Text;
        }
        catch (CryptographicException e)
        {

            return null;
        }
    }
    //RSA解密
    static public string RSA_Decrypt(string str_Cypher_Text, string str_Private_Key)
    {
        byte[] DataToDecrypt = Convert.FromBase64String(str_Cypher_Text);
        try
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            //RSA.ImportParameters(RSAKeyInfo);
            byte[] bytes_Public_Key = Convert.FromBase64String(str_Private_Key);
            RSA.ImportCspBlob(bytes_Public_Key);

            //OAEP padding is only available on Microsoft Windows XP or later. 
            byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
            return str_Plain_Text;
        }
        catch (CryptographicException e)
        {
            throw e;
            return null;
        }
    }

 

 

 

 

 

 

 

 

 

  //加密
    public static String Encrypt(String str)
    {
        String encryptKey = "Aast";
        DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
        byte[] key = Encoding.Unicode.GetBytes(encryptKey);
        byte[] data = Encoding.Unicode.GetBytes(str);
        System.IO.MemoryStream MStream = new System.IO.MemoryStream();
        CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
        CStream.Write(data, 0, data.Length);
        CStream.FlushFinalBlock();
        return Convert.ToBase64String(MStream.ToArray());
    }
    //解密
    public static String Decryption(String str)
    {
        String encryptKey = "Aast";
        DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
        byte[] key = Encoding.Unicode.GetBytes(encryptKey);
        byte[] data = Convert.FromBase64String(str);
        System.IO.MemoryStream MStream = new System.IO.MemoryStream();
        CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
        CStream.Write(data, 0, data.Length);
        CStream.FlushFinalBlock();
        return Encoding.Unicode.GetString(MStream.ToArray());
    }

 

 

你可能感兴趣的:(.net)