- using System;
- using System.Text;
- using System.IO;
- using System.Security.Cryptography;
- namespace CI
- {
- class CI_Encrypt
- {
- private string key;
- private string hash_type = "sha1";
- public CI_Encrypt(string key = "")
- {
- if(key.Length > 0)
- {
- this.key = key;
- }
- }
- public string encode(string data)
- {
- RijndaelManaged aes = new RijndaelManaged();
- aes.KeySize = 256;
- aes.BlockSize = 256;
- aes.Padding = PaddingMode.Zeros;
- aes.Mode = CipherMode.CBC;
- aes.Key = this.get_key(key);
- aes.GenerateIV();
- var _IV = aes.IV;
- byte[] buffer;
- ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
- using (MemoryStream msEncrypt = new MemoryStream())
- {
- using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, AESEncrypt, CryptoStreamMode.Write))
- {
- using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
- {
- //Write all data to the stream.
- swEncrypt.Write(data);
- }
- buffer = msEncrypt.ToArray();
- }
- }
- var enc = new byte [ _IV.Length + buffer.Length ];
- Array.Copy(_IV,enc ,_IV.Length);
- Array.Copy(buffer,0,enc,_IV.Length,buffer.Length);
- return this.add_cipher_noise(enc);
- }
- public string decode(string data)
- {
- string plainText;
- byte[] _enc_data = this.remove_cipher_noise(Convert.FromBase64String(data));
- using (RijndaelManaged aes = new RijndaelManaged())
- {
- aes.KeySize = 256;
- aes.BlockSize = 256;
- aes.Padding = PaddingMode.Zeros;
- aes.Mode = CipherMode.CBC;
- aes.Key = this.get_key(key);
- var _IV = new byte[aes.IV.Length];
- Array.Copy(_enc_data, _IV, _IV.Length);
- aes.IV = _IV;
- var endata = new byte[_enc_data.Length - aes.IV.Length];
- Array.Copy(_enc_data, aes.IV.Length, endata, 0, endata.Length);
- // Create a decrytor to perform the stream transform.
- ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
- // Create the streams used for decryption.
- using (MemoryStream msDecrypt = new MemoryStream(endata))
- {
- using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
- {
- using (StreamReader srDecrypt = new StreamReader(csDecrypt))
- {
- // Read the decrypted bytes from the decrypting stream
- // and place them in a string.
- plainText = srDecrypt.ReadToEnd();
- }
- }
- }
- }
- return plainText;
- }
- public string add_cipher_noise(byte[] data)
- {
- string hashkey = this.hash(System.Text.Encoding.Default.GetString(this.get_key(this.key)));
- char[] keyChar = hashkey.ToCharArray();
- byte[] encrpytData = new byte[data.Length];
- for (int i = 0, j = 0; i < data.Length; i++, j++)
- {
- if (j >= hashkey.Length)
- {
- j = 0;
- }
- var temp = (((int)data[i] + (int)keyChar[j]) % 256);
- encrpytData[i] = (byte)temp;
- }
- return System.Convert.ToBase64String(encrpytData);
- }
- public byte[] remove_cipher_noise(byte[] data)
- {
- string hashkey = this.hash(System.Text.Encoding.Default.GetString(this.get_key(this.key)));
- char[] keyChar = hashkey.ToCharArray();
- byte[] encrpytData = new byte[data.Length];
- int temp;
- for (int i = 0, j = 0; i < data.Length; i++, j++)
- {
- if (j >= hashkey.Length)
- {
- j = 0;
- }
- temp = (int)data[i] - (int)keyChar[j];
- if(temp < 0)
- {
- temp += 256;
- }
- encrpytData[i] = (byte)temp;
- }
- return encrpytData;
- }
- private string hash(string key)
- {
- byte[] hashBytes;
- if (this.hash_type == "sha1")
- {
- SHA1 sha = new SHA1CryptoServiceProvider();
- hashBytes = (sha.ComputeHash(System.Text.Encoding.Default.GetBytes(key)));
- }
- else
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- hashBytes = (md5.ComputeHash(System.Text.Encoding.Default.GetBytes(key)));
- }
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hashBytes.Length; i++)
- {
- sb.Append(hashBytes[i].ToString("X2"));
- }
- return sb.ToString().ToLower();
- }
- private byte[] get_key(string key)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] hashBytes = (md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key)));
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hashBytes.Length; i++)
- {
- sb.Append(hashBytes[i].ToString("X2"));
- }
- return System.Text.Encoding.Default.GetBytes(sb.ToString().ToLower());
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0808201614936.html
来源: http://www.codesnippet.cn/detail/0808201614936.html