博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【ADO.NET基础-数据加密】第一篇(加密解密篇)
阅读量:5149 次
发布时间:2019-06-13

本文共 3440 字,大约阅读时间需要 11 分钟。

可以采用下面的函数实现密码的加密 public static string EncryptString(string str)    {        //密文        string key = "www";        //把密文转成二进制型        byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);        //把字符串转成二进制型        byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str);        //生成DESCryptoServiceProvider对象        System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();             //设定        des.Key = ResizeBytesArray(bytesKey, des.Key.Length);        des.IV = ResizeBytesArray(bytesKey, des.IV.Length);        //写出被加密的数据的MemoryStream        System.IO.MemoryStream msOut = new System.IO.MemoryStream();        //生成DES加密对象        System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateEncryptor();        //生成CryptoStream        System.Security.Cryptography.CryptoStream cryptStreem =            new System.Security.Cryptography.CryptoStream(msOut,            desdecrypt,            System.Security.Cryptography.CryptoStreamMode.Write);        //写入        cryptStreem.Write(bytesIn, 0, bytesIn.Length);        cryptStreem.FlushFinalBlock();        //取得加密后的数据        byte[] bytesOut = msOut.ToArray();        //关闭        cryptStreem.Close();        msOut.Close();        //返回        return System.Convert.ToBase64String(bytesOut);    } 可以采用下面的函数实现密码的解密     ///     /// 对数据解密    ///     /// 被加密的数据    /// 
解密后的数据
public static string DecryptString(string str) { //秘文 string key = "www"; string result = ""; //把密文转成二进制型 byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //生成DESCryptoServiceProvider对象 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //设定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //返回 byte[] bytesIn = System.Convert.FromBase64String(str); //生成MemoryStream对象以便读出被加密的数据 System.IO.MemoryStream msIn = new System.IO.MemoryStream(bytesIn); //生成DES复原对象 System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor(); //生成CryptoStream对象以便读入数据 System.Security.Cryptography.CryptoStream cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); //生成StreamReader以便取得解密后的数据 System.IO.StreamReader srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8); //取得解密后的数据 result = srOut.ReadToEnd(); //关闭 srOut.Close(); cryptStreem.Close(); msIn.Close(); return result; } /// /// 使用共有key变更二进制的数据的大小 /// /// 变更二进制的数据的大小 /// 二进制的数据的大小 ///
变更后的二进制列
private static byte[] ResizeBytesArray(byte[] bytes, int newSize) { byte[] newBytes = new byte[newSize]; if (bytes.Length < newSize) { for (int i = 0; i < bytes.Length; i++) newBytes[i] = bytes[i]; } else { int pos = 0; for (int i = newSize; i < bytes.Length; i++) { newBytes[pos++] ^= bytes[i]; if (pos >= newBytes.Length) pos = 0; } }

 

转载于:https://www.cnblogs.com/888888CN/p/7126912.html

你可能感兴趣的文章