问题描述
- C# AES加密的问题~~~~
-
将从CSV文件中读取到的身份证号码加密,要求使用AES加密,加密的函数接口请调用”AES.CS”源文件。
ASE.CS源文件的代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;namespace Utility
{
public class AES
{
/////
///// 获取密钥
/////
//private static string Key
//{
// get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
//}///// <summary> ///// 获取向量 ///// </summary> //private static string IV //{ // get { return @"L+~f4,Ir)b$=pkf"; } //} //public static void Decrypt<T>(T info) //{ // foreach (var property in info.GetType().GetProperties()) // { // var pro = property.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "DecodeAttribute"); // if (pro != null) // { // property.SetValue(info,AESDecrypt(property.GetValue(info).ToString())); // } // } //} //public static void Encrypt<T>(T info) //{ // foreach (var property in info.GetType().GetProperties()) // { // var pro = property.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "DecodeAttribute"); // if (pro!=null) // { // property.SetValue(info, AESEncrypt(property.GetValue(info).ToString())); // } // } //} /// <summary> /// AES加密 /// </summary> /// <param name="plainStr">明文字符串</param> /// <returns>密文</returns> public static string AESEncrypt(string plainStr) { byte[] bKey = Encoding.UTF8.GetBytes(Config.aesKey); byte[] bIV = Encoding.UTF8.GetBytes(Config.aesIV); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } } catch { } aes.Clear(); return encrypt; } /// <summary> /// AES加密 /// </summary> /// <param name="plainStr">明文字符串</param> /// <param name="returnNull">加密失败时是否返回 null,false 返回 String.Empty</param> /// <returns>密文</returns> public static string AESEncrypt(string plainStr, bool returnNull) { string encrypt = AESEncrypt(plainStr); return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt); } /// <summary> /// AES解密 /// </summary> /// <param name="encryptStr">密文字符串</param> /// <returns>明文</returns> public static string AESDecrypt(string encryptStr) { byte[] bKey = Encoding.UTF8.GetBytes(Config.aesKey); byte[] bIV = Encoding.UTF8.GetBytes(Config.aesIV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } } catch { } aes.Clear(); return decrypt; } /// <summary> /// AES解密 /// </summary> /// <param name="encryptStr">密文字符串</param> /// <param name="returnNull">解密失败时是否返回 null,false 返回 String.Empty</param> /// <returns>明文</returns> public static string AESDecrypt(string encryptStr, bool returnNull) { string decrypt = AESDecrypt(encryptStr); return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt); } }
}
新手,求告诉,怎么把这结合起来
解决方案
string oldStr1 = "你要加密的明文字符串";
string newStr = AES.AESEncrypt(oldStr);
string oldStr2 = AES.AESDecrypt(newStr);
//oldStr1和oldStr2应该相等
解决方案二:
直接引用这个类
然后申明一个实例,调用AESEncrypt来加密,AESDecrypt解密
解决方案三:
using System;
using System.Security.Cryptography;
using System.Text;
namespace Encrypt
{
///
/// Description of AESEncrypt.
///
public class AESEncrypt
{
public AESEncrypt()
{......
答案就在这里:C#_AES加密
时间: 2024-09-20 20:39:41