AES与RSA加密
2017-02-07 16:05
169 查看
AES
using System; using System.Text; using System.Security.Cryptography; using System.IO; namespace winfAESEncryptDecrypt { public class AES { /// <summary> /// AES加密 /// </summary> /// <param name="data">待加密的字符数据</param> /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param> /// <param name="iv">iv向量,长度必须为128位(byte[16])</param> /// <returns>加密后的字符</returns> public string EnAES(string data, string key, string iv) { if (string.IsNullOrEmpty(data)) return data; Aes aes = Aes.Create(); byte[] tmp = null; ICryptoTransform encryptor = aes.CreateEncryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv)); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { StreamWriter writer = new StreamWriter(cs); writer.Write(data); writer.Flush(); writer.Close(); } tmp = ms.ToArray(); } return Convert.ToBase64String(tmp); } /// <summary> /// AES解密 /// </summary> /// <param name="data">待加密的字符数据</param> /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param> /// <param name="iv">iv向量,长度必须为128位(byte[16])</param> /// <returns>加密后的字符</returns> public string DeAES(string data, string key, string iv) { if (string.IsNullOrEmpty(data)) return data; string result = string.Empty; Aes aes = Aes.Create(); ICryptoTransform edcryptor = aes.CreateDecryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv)); using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data))) { using (CryptoStream cs = new CryptoStream(ms, edcryptor, CryptoStreamMode.Read)) { StreamReader reader = new StreamReader(cs); result = reader.ReadLine(); reader.Close(); } } aes.Clear(); return result; } } }RSA
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); string privateKey = rsa.ToXmlString(true); string publicKey = rsa.ToXmlString(false);
using System; using System.Security.Cryptography; using System.Text; namespace winfAESEncryptDecrypt { public class RSA { /// <summary> /// RSA加密 /// </summary> /// <param name="sdata"></param> /// <param name="sPublicKey"></param> /// <returns></returns> public string EnRSA(string sdata, string sPublicKey) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(sPublicKey); byte[] mybyte = Encoding.UTF8.GetBytes(sdata); string sEnRSA = Convert.ToBase64String(rsa.Encrypt(mybyte, false)); return sEnRSA; } /// <summary> /// RSA解密 /// </summary> /// <param name="sdata"></param> /// <param name="sPrivateKey"></param> /// <returns></returns> public string DeRAS(string sdata, string sPrivateKey) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(sPrivateKey); byte[] mybyte = Convert.FromBase64String(sdata); string sDeRAS = Encoding.UTF8.GetString(rsa.Decrypt(mybyte, false)); return sDeRAS; } } }
相关文章推荐
- Android加密DES AES RSA的简单使用
- iOS RSA,AES加密(使用openssl)
- 典型的网络接口安全机制,AES和RSA混合加密
- Android 数据加密之RSA + AES
- java实现RSA+AES的简单加密解密
- 对称加密协议DES、3DES aes 和非对称加密协议(不用分发私钥 RSA)
- java RSA和AES加密解密工具
- js的RSA和AES加密解密
- [置顶] App安全登录之密码、通讯加密方法(MD5,Base64,RSA,AES)
- Java对称与非对称加密解密,AES与RSA
- 常用加密解密算法【RSA、AES、DES、MD5】介绍和使用
- 安全篇-AES/RSA加密机制
- java 加密技术AES、MD5、RSA
- Android AES和RSA混合加密工具类实现
- DES,RSA,MD5,AES加密
- AES与RSA相结合数据加密方案
- AES+RSA进行联合加密
- ios中AES和RSA 加密
- 24、AES RSA加密处理记录
- 利用OpenSSL库对Socket传输进行安全加密(RSA+AES)