时间:2021-05-20
第一个为大家分享的是C#字符串使用密钥进行加解密代码,具体内容如下
public class DesEncrypt { /// <summary> /// 算法偏移量 /// </summary> const string m_IV = "12345678"; /// <summary> /// 功能描述:根据输入的密钥生成8位密钥 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-07-20 17:25:26 /// </summary> /// <param name="strkey">strkey</param> /// <returns>8位密钥</returns> private static string GetKey(string strkey) { if (string.IsNullOrEmpty(strkey)) { strkey = "InfoColl"; } if (strkey.Length % 8 == 0) { return strkey; } else { return GetKey(strkey + "0"); } } /// <summary> /// 功能描述:加密字符串 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-07-20 17:18:31 /// 任务编号: /// </summary> /// <param name="strSourceString">原字符串</param> /// <param name="strKey">密钥</param> /// <returns>加密后的字符串</returns> public static string Encrypt(string strSourceString, string strKey) { strKey = GetKey(strKey); byte[] btKey = Encoding.UTF8.GetBytes(strKey); byte[] btIV = Encoding.UTF8.GetBytes(m_IV); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { try { byte[] inData = Encoding.UTF8.GetBytes(strSourceString); using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch { return strSourceString; } } } /// <summary> /// 功能描述:解密字符串 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-07-20 17:18:49 /// 任务编号: /// </summary> /// <param name="strEncryptedString">原字符串</param> /// <param name="strKey">密钥</param> /// <returns>解密后的字符串</returns> public static string Decrypt(string strEncryptedString, string strKey) { strKey = GetKey(strKey); byte[] btKey = Encoding.UTF8.GetBytes(strKey); byte[] btIV = Encoding.UTF8.GetBytes(m_IV); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { try { byte[] inData = Convert.FromBase64String(strEncryptedString); using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.UTF8.GetString(ms.ToArray()); } catch { return strEncryptedString; } } } }C#字符串加密和解密
using System.Security.Cryptography;using System.IO;//默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> /// DES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密钥,要求为8位</param> /// <returns>加密成功返回加密后的字符串,失败返回源串 </returns> public static string EncryptDES(string encryptString, string encryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));//转换为字节 byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();//实例化数据加密标准 MemoryStream mStream = new MemoryStream();//实例化内存流 //将数据流链接到加密转换的流 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } string EncryptStr = EncryptDESString.EncryptDES("aaaaaaaaaa", "ssssssss"); //返回加密后的字符串 string DecryptStr = EncryptDESString.DecryptDES(EncryptStr, "ssssssss");//解密字符串以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#实现简易的加密、解密字符串工具类。分享给大家供大家参考。具体如下:这里的类CypherHelper是一个用于加密、解密字符串的工具类~~~,适
本文所述为在C#中使用Pointer指针的简单示例,非常适合新手参考学习。该实例演示了字符串的加密及解密的过程,将字符串指针p指向字符数组b,并将参数p传给函数
本文实例讲述了C#字符串加密解密方法。分享给大家供大家参考。具体如下:复制代码代码如下:#region加密解密staticstringencryptKey="O
ASCII码转换为int:ord('A')65int转为ASCII码:chr(65)'A'题目内容:实现一个凯撒密码的变种算法,对输入字符串进行加解密处理把字母
栅栏加解密是对较短字符串的一种处理方式,给定行数Row,根据字符串长度计算出列数Column,构成一个方阵。加密过程:就是按列依次从上到下对明文进行排列,然后按