using System; |
using System.Collections.Generic; |
using System.IO; |
using System.Linq; |
using System.Security.Cryptography; |
using System.Text; |
namespace COMMON.DEncrypt |
{ |
public class TDESCSPEncrypt |
{ |
//12个字符 |
private static string customIV = "jfYi3ggEEV4=" ; //"4vHKRj3yfzU="; |
//32个字符 |
private static string customKey = "9DQrNW0KBi1LLGok6ayQtvuZucoXtLb0" ; //"xhVs6DRXLfUGxw+AhtfQdpQGoa+8SA9d"; |
/// <summary> |
/// 加密字符串 |
/// </summary> |
/// <param name="password"></param> |
/// <returns></returns> |
public static string EncryptPassword( string password, string key_Base64, string iv_Base64) |
{ |
string encryptPassword = string .Empty; |
SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider(); |
algorithm.Key = Convert.FromBase64String(key_Base64); |
algorithm.IV = Convert.FromBase64String(iv_Base64); |
algorithm.Mode = CipherMode.ECB; |
algorithm.Padding = PaddingMode.PKCS7; |
ICryptoTransform transform = algorithm.CreateEncryptor(); |
byte [] data = Encoding.Unicode.GetBytes(password); |
MemoryStream memoryStream = new MemoryStream(); |
CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write); |
cryptoStream.Write(data, 0, data.Length); |
cryptoStream.FlushFinalBlock(); |
encryptPassword = Convert.ToBase64String(memoryStream.ToArray()); |
memoryStream.Close(); |
cryptoStream.Close(); |
return encryptPassword; |
} |
/// <summary> |
/// 解密字符串 |
/// </summary> |
/// <param name="encryptPassword"></param> |
/// <returns></returns> |
public static string DecryptPassword( string encryptPassword, string key_Base64, string iv_Base64) |
{ |
string decryptPassword = string .Empty; |
SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider(); |
algorithm.Key = Convert.FromBase64String(key_Base64); |
algorithm.IV = Convert.FromBase64String(iv_Base64); |
algorithm.Mode = CipherMode.ECB; |
algorithm.Padding = PaddingMode.PKCS7; |
ICryptoTransform transform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV); |
byte [] buffer = Convert.FromBase64String(encryptPassword); |
MemoryStream memoryStream = new MemoryStream(buffer); |
CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read); |
StreamReader reader = new StreamReader(cryptoStream, Encoding.Unicode); |
decryptPassword = reader.ReadToEnd(); |
reader.Close(); |
cryptoStream.Close(); |
memoryStream.Close(); |
return decryptPassword; |
} |
public static string EncryptPassword( string password, string key_Base64) |
{ |
return EncryptPassword(password, key_Base64, customIV); |
} |
public static string DecryptPassword( string encryptPassword, string key_Base64) |
{ |
return DecryptPassword(encryptPassword, key_Base64, customIV); |
} |
public static string EncryptPassword( string password) |
{ |
return EncryptPassword(password, customKey, customIV); |
} |
public static string DecryptPassword( string encryptPassword) |
{ |
return DecryptPassword(encryptPassword, customKey, customIV); |
} |
static TripleDESCryptoServiceProvider _DCSP; |
private static TripleDESCryptoServiceProvider DCSP |
{ |
get |
{ |
if (_DCSP == null ) |
_DCSP = new TripleDESCryptoServiceProvider(); |
// |
return _DCSP; |
} |
} |
public static string CreateKEY() |
{ |
DCSP.GenerateKey(); |
return Convert.ToBase64String(DCSP.Key); |
} |
} |
} |
by: 发表于:2017-12-27 10:10:38 顶(0) | 踩(0) 回复
??
回复评论