C#中的ASCII加密/解密

20 篇文章 0 订阅
1 篇文章 0 订阅
#region ========加密======== 

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="Text"></param>
    /// <returns></returns>
	public static string Encrypt(string Text) 
	{
        return Encrypt(Text, "litianping");
	}
	/// <summary> 
	/// 加密数据 
	/// </summary> 
	/// <param name="Text"></param> 
	/// <param name="sKey"></param> 
	/// <returns></returns> 
	public static string Encrypt(string Text,string sKey) 
	{ 
		DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
		byte[] inputByteArray; 
		inputByteArray=Encoding.Default.GetBytes(Text); 
		des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
		des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
		System.IO.MemoryStream ms=new System.IO.MemoryStream(); 
		CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); 
		cs.Write(inputByteArray,0,inputByteArray.Length); 
		cs.FlushFinalBlock(); 
		StringBuilder ret=new StringBuilder(); 
		foreach( byte b in ms.ToArray()) 
		{ 
			ret.AppendFormat("{0:X2}",b); 
		} 
		return ret.ToString(); 
	} 

	#endregion
	
	#region ========解密======== 


    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="Text"></param>
    /// <returns></returns>
	public static string Decrypt(string Text) 
	{
        return Decrypt(Text, "litianping");
	}
	/// <summary> 
	/// 解密数据 
	/// </summary> 
	/// <param name="Text"></param> 
	/// <param name="sKey"></param> 
	/// <returns></returns> 
	public static string Decrypt(string Text,string sKey) 
	{ 
		DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
		int len; 
		len=Text.Length/2; 
		byte[] inputByteArray = new byte[len]; 
		int x,i; 
		for(x=0;x<len;x++) 
		{ 
			i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); 
			inputByteArray[x]=(byte)i; 
		} 
		des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
		des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
		System.IO.MemoryStream ms=new System.IO.MemoryStream(); 
		CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); 
		cs.Write(inputByteArray,0,inputByteArray.Length); 
		cs.FlushFinalBlock(); 
		return Encoding.Default.GetString(ms.ToArray()); 
	} 

	#endregion 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 C# 语言写的 AES 加密/解密代码示例: ``` using System; using System.Security.Cryptography; using System.Text; class AES { static void Main() { string text = "Hello, world!"; // 待加密的文本 string key = "MY_KEY_123456"; // 加密用的密钥 string iv = "MY_IV_654321"; // 加密用的 IV byte[] encrypted = Encrypt(text, key, iv); // 加密文本 string encryptedText = Convert.ToBase64String(encrypted); // 将加密后的字节数组转换为 Base64 编码的字符串 Console.WriteLine("加密后的文本:{0}", encryptedText); string decryptedText = Decrypt(encrypted, key, iv); // 解密文本 Console.WriteLine("解密后的文本:{0}", decryptedText); } static byte[] Encrypt(string text, string key, string iv) { byte[] textBytes = Encoding.UTF8.GetBytes(text); using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(iv); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(textBytes, 0, textBytes.Length); } return ms.ToArray(); } } } static string Decrypt(byte[] data, string key, string iv) { using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(iv); using (MemoryStream ms = new MemoryStream(data)) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) { using (StreamReader sr = new StreamReader(cs)) { return sr.ReadToEnd(); } } } } } } ``` 上述代码,Encrypt 方法用于加密文本,Decrypt 方法用于解密文本,具体实现方式使用 Aes.Create 方法创建了一个 AES 实例,并使用指定的密钥和 IV 来进行加密解密。 其加密过程,我们首先将待加密的文本转换为字节数组,然后创建一个 MemorySteram 对象,使用 CryptoStream 对象实例化时指定加密模式加密加密的字节数组,最后调用 CryptoStream 对象的 Write 方法将加密后的字节数组写入 MemorySteram ,并返回 MemorySteram 的字节数组。 解密过程,我们接收一个加密过的字节数组,同样先创建一个 AES 实例,设置加密用的密钥和 IV,然后从加密过的字节数组创建一个 MemoryStream 对象,在这个对象上使用 CryptoStream 对象实例化时指定解密模式对加密过的字节数组进行解密,最后使用 StreamReader 对象从 CryptoStream 读取出解密后的文本并返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值