/// <summary> /// 加密文件 /// </summary> public void EncryptFile(string Value, string OuputFileName) { try { // Must be 64 bits, 8 bytes. // Distribute this key to the user who will decrypt this file. //Get the Key for the file to Encrypt. string sKey = GenerateKey(); byte[] b = ASCIIEncoding.ASCII.GetBytes(Value); System.IO.FileStream fsEncrypted = new System.IO.FileStream(OuputFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write); System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); System.Security.Cryptography.ICryptoTransform desencrypt = DES.CreateEncryptor(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(fsEncrypted, desencrypt, System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(b, 0, b.Length); cs.Close(); fsEncrypted.Close(); } catch (Exception ex) { throw ex; } }
//static Logger logger = SimpleLogger.setLogger("Common"); #region �Í����W /// <summary> /// �������Í������� /// </summary> /// <param name="str">�Í������镶����</param> /// <param name="key">�p�X���[�h</param> /// <returns>�Í������ꂽ������</returns> public static string EncryptString(string str, string key) { //�������o�C�g�^�z��ɂ��� byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str); //DESCryptoServiceProvider�I�u�W�F�N�g�̍쐬 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //���L�L�[�Ə������x�N�^����� //�p�X���[�h��o�C�g�z��ɂ��� byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //���L�L�[�Ə������x�N�^��ݒ� des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //�Í������ꂽ�f�[�^������o�����߂�MemoryStream MemoryStream msOut = new System.IO.MemoryStream(); //DES�Í����I�u�W�F�N�g�̍쐬 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(); //�Í������ꂽ�f�[�^��擾 byte[] bytesOut = msOut.ToArray(); //���� cryptStreem.Close(); msOut.Close(); //Base64�ŕ�����ɕύX���Č��ʂ�Ԃ� return System.Convert.ToBase64String(bytesOut).Replace("=", ""); }
/// <summary> /// 字符串加密 /// </summary> /// <param name="strText">字符串</param> /// <param name="strEncrKey">密钥8位数字或字母</param> /// <returns></returns> public string DesEncryptString(string strText, string strEncrKey) { byte[] rgbKey = null; string SIv = "inputvec"; try { rgbKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, strEncrKey.Length)); System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider(); provider.Key = rgbKey; provider.IV = System.Text.Encoding.UTF8.GetBytes(SIv); provider.Mode = System.Security.Cryptography.CipherMode.ECB; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strText); System.IO.MemoryStream stream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream stream2 = new System.Security.Cryptography.CryptoStream(stream, provider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); return(Convert.ToBase64String(stream.ToArray())); } catch (Exception exception) { //return ("error:" + exception.Message + "\r"); throw exception; } }
/// <summary> /// DES加密,向量随机生成 /// </summary> /// <param name="plaintext">明文</param> /// <returns></returns> public static string DesEncrypt(string plaintext) { //随机生成8位向量 string iv = GenerateRandom(); byte[] rgbKey = System.Text.Encoding.ASCII.GetBytes("SolinArt"); byte[] rgbIV = System.Text.Encoding.ASCII.GetBytes(iv); byte[] _data = System.Text.Encoding.UTF8.GetBytes(plaintext); System.Security.Cryptography.DESCryptoServiceProvider _des = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.ICryptoTransform _encrypt = _des.CreateEncryptor(rgbKey, rgbIV); byte[] _result = _encrypt.TransformFinalBlock(_data, 0, _data.Length); _encrypt.Dispose(); _des.Clear(); string result = BitConverter.ToString(_result); result = iv + result.Replace("-", ""); //首尾各加16位共计32位的MD5校验码 #region 32位的MD5校验码 //string _md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(result, "MD5"); string _md5 = Md5(result); result = string.Format("{0}{1}{2}" , _md5.Substring(0, 16) , result , _md5.Substring(16) ); #endregion return(result); }
public string Descifrar(string Cadena) { try { if (string.IsNullOrEmpty(Cadena)) { return(Cadena); } byte[] PlainText; PlainText = Convert.FromBase64String(Cadena); MemoryStream memdata = new MemoryStream(); System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cryptostream = new System.Security.Cryptography.CryptoStream(memdata, DES.CreateDecryptor(Encoding.ASCII.GetBytes(Default8Key), Encoding.ASCII.GetBytes(Default8VI)), System.Security.Cryptography.CryptoStreamMode.Write); cryptostream.Write(PlainText, 0, PlainText.Length); cryptostream.FlushFinalBlock(); cryptostream.Close(); return(Encoding.ASCII.GetString(memdata.ToArray())); } catch (Exception ex) { return(null); } }
public static byte[] DES_Enc(string str, string key, string vit) { //判断向量是否为空,进行默认赋值; if (string.IsNullOrEmpty(key)) { key = "KCCT"; } //判断向量是否为空,进行默认赋值; if (string.IsNullOrEmpty(vit)) { vit = "MNSN"; } try { //实例化加解密类的对象; using (var descsp = new System.Security.Cryptography.DESCryptoServiceProvider()) { //定义字节数组,用来存储要加密的字符串; var data = System.Text.Encoding.UTF8.GetBytes(str); //实例化内存流对象; using (var mStream = new System.IO.MemoryStream()) { //使用内存流实例化加密流对象; using (var cStream = new System.Security.Cryptography.CryptoStream(mStream, descsp.CreateEncryptor(System.Text.Encoding.Unicode.GetBytes(key), System.Text.Encoding.Unicode.GetBytes(vit)), System.Security.Cryptography.CryptoStreamMode.Write)) { //向加密流中写入数据; cStream.Write(data, 0, data.Length); //释放加密流; cStream.FlushFinalBlock(); //返回加密后的字符串; return mStream.ToArray(); } } } } catch { return null; } }
//static Logger logger = SimpleLogger.setLogger("Common"); #region 暗号化関係 /// <summary> /// 文字列を暗号化する /// </summary> /// <param name="str">暗号化する文字列</param> /// <param name="key">パスワード</param> /// <returns>暗号化された文字列</returns> public static string EncryptString(string str, string key) { //文字列をバイト型配列にする byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str); //DESCryptoServiceProviderオブジェクトの作成 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //共有キーと初期化ベクタを決定 //パスワードをバイト配列にする byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //共有キーと初期化ベクタを設定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //暗号化されたデータを書き出すためのMemoryStream 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(); //Base64で文字列に変更して結果を返す return(System.Convert.ToBase64String(bytesOut).Replace("=", "")); }
/// <summary> /// 字符串加密 /// </summary> /// <param name="password">欲加密的字符串</param> /// <returns>加密后的字符串</returns> public static string PasswordEncrypt(string password) { string strCharKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string strPasswordKey = ""; for (int intIndexKey = 1; intIndexKey <= 8; intIndexKey++) { strPasswordKey += strCharKey.Substring(GetRnd(0, strCharKey.Length - 1), 1); } System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); byte[] bytInput = System.Text.Encoding.Default.GetBytes(password); des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream((System.IO.Stream)ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(bytInput, 0, bytInput.Length); cs.FlushFinalBlock(); System.Text.StringBuilder sbRet = new StringBuilder(); foreach (byte b in ms.ToArray()) { sbRet.AppendFormat("{0:X2}", b); } return(sbRet.ToString() + strPasswordKey); }
/// <summary> /// 字符串解密 /// </summary> /// <param name="encryptPassword">加密过的字符串</param> /// <returns>解密后的字符串</returns> public static string PasswordDecrypt(string encryptPassword) { try { string strPasswordKey = encryptPassword.Substring(encryptPassword.Length - 8, 8); string strEncryptPassword = encryptPassword.Substring(0, encryptPassword.Length - 8); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); int intEncryptPasswordLength = strEncryptPassword.Length / 2; byte[] bytInput = new byte[intEncryptPasswordLength]; for (int intIndex = 0; intIndex < intEncryptPasswordLength; intIndex++) { bytInput[intIndex] = Convert.ToByte(strEncryptPassword.Substring(intIndex * 2, 2), 16); } des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream((System.IO.Stream)ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(bytInput, 0, bytInput.Length); cs.FlushFinalBlock(); return(System.Text.Encoding.Default.GetString(ms.ToArray())); } catch { return(""); } }
public static string DecryptStatic(string decryptString, string decryptKey) { System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); string result; try { System.Text.Encoding uTF = System.Text.Encoding.UTF8; decryptString = uTF.GetString(System.Convert.FromBase64String(decryptString)); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(decryptKey); byte[] keys = SimpleSecurityHelper.Keys; byte[] array = System.Convert.FromBase64String(decryptString); System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(bytes, keys), System.Security.Cryptography.CryptoStreamMode.Write); cryptoStream.Write(array, 0, array.Length); cryptoStream.FlushFinalBlock(); result = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); } catch { result = "Decrypt Failed!"; } finally { memoryStream.Close(); } return(result); }
public static string DesDecrypt(string inputString, string decryptKey) { #region byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = new Byte[inputString.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(inputString); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); inputString = encoding.GetString(ms.ToArray()); } catch { throw; } return(inputString); #endregion }
public static string EncryptStatic(string encryptString) { System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); string result; try { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(SimpleSecurityHelper.KeyValue.Substring(0, 8)); byte[] keys = SimpleSecurityHelper.Keys; byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(encryptString); System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(bytes, keys), System.Security.Cryptography.CryptoStreamMode.Write); cryptoStream.Write(bytes2, 0, bytes2.Length); cryptoStream.FlushFinalBlock(); System.Text.Encoding uTF = System.Text.Encoding.UTF8; result = System.Convert.ToBase64String(uTF.GetBytes(System.Convert.ToBase64String(memoryStream.ToArray()))); } catch { result = "Encrypt Failed!"; } finally { memoryStream.Close(); } return(result); }
private static readonly String strDesKey = "gaoguanj";//加密所需8位密匙 /// /// DES加密 /// /// 要加密字符串 /// 返回加密后字符串 public static String Encrypt_DES(String str) { if (str.Trim() == "") { return(""); } try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); Byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(str); des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (Byte b in ms.ToArray()) { sb.AppendFormat("{0:X2}", b); } return(sb.ToString()); } catch (System.Exception ex) { return(""); } }
/// /// DES解密 /// /// 要解密字符串 /// 返回解密后字符串 public static String Decrypt_DES(String str) { if (str.Trim() == "") { return(""); } try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); Int32 x; Byte[] inputByteArray = new Byte[str.Length / 2]; for (x = 0; x < str.Length / 2; x++) { inputByteArray[x] = (Byte)(Convert.ToInt32(str.Substring(x * 2, 2), 16)); } des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.StringBuilder ret = new System.Text.StringBuilder(); return(System.Text.Encoding.Default.GetString(ms.ToArray())); } catch (System.Exception ex) { return(""); } }
/// <summary> /// Creates the CryptoServiceProvider based on the PBKDF2. /// </summary> /// <param name="inPBKDF2">Input PBKDF2</param> /// <returns>CSP</returns> private System.Security.Cryptography.DESCryptoServiceProvider createCSP(System.Security.Cryptography.Rfc2898DeriveBytes inPBKDF2) { inPBKDF2.Reset(); csp = new System.Security.Cryptography.DESCryptoServiceProvider(); csp.Mode = this.cipherMode; csp.Padding = this.paddingMode; csp.Key = inPBKDF2.GetBytes(csp.KeySize / 8); csp.IV = inPBKDF2.GetBytes(csp.BlockSize / 8); return(csp); }
public static byte[] E1(byte[] data) { var stream = new MemoryStream(); var des = new System.Security.Cryptography.DESCryptoServiceProvider(); des.Mode = System.Security.Cryptography.CipherMode.CBC; des.Padding = System.Security.Cryptography.PaddingMode.PKCS7; var cs = new System.Security.Cryptography.CryptoStream(stream, des.CreateEncryptor(ES.Key, ES.IV), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(data, 0, (int)data.Length); cs.FlushFinalBlock(); return(stream.ToArray()); }
/// <summary> /// DES解密 /// </summary> public static string DesDecrypt(string encrypted) { //小于40位时,不是有效的加密串 if (encrypted.Length <= 40) { return(""); } else if (encrypted.Length % 2 != 0) { return(""); //非偶数位不是有效的加密串 } //32位MD5校验码 string _md5 = encrypted.Substring(0, 16); _md5 += encrypted.Substring(encrypted.Length - 16); //移除MD5 encrypted = encrypted.Substring(16); encrypted = encrypted.Substring(0, encrypted.Length - 16); //进行MD5验证是否被修改 //if (_md5 != System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(encrypted, "MD5")) if (_md5 != Md5(encrypted)) { return(""); } //截取,移除前8位向量长度 string iv = encrypted.Substring(0, 8); //移除向量值 encrypted = encrypted.Substring(8); //32位整型转换器 System.ComponentModel.Int32Converter _int32 = new System.ComponentModel.Int32Converter(); //字符串转换成数组 byte[] _datas = new byte[encrypted.Length / 2]; for (int i = 0; i < _datas.Length; i++) { _datas[i] = Convert.ToByte(_int32.ConvertFromInvariantString("0x" + encrypted.Substring(i * 2, 2))); } byte[] rgbKey = System.Text.Encoding.ASCII.GetBytes("SolinArt"); byte[] rgbIV = System.Text.Encoding.ASCII.GetBytes(iv); System.Security.Cryptography.DESCryptoServiceProvider _des = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.ICryptoTransform _encrypt = _des.CreateDecryptor(rgbKey, rgbIV); byte[] _result = _encrypt.TransformFinalBlock(_datas, 0, _datas.Length); _encrypt.Dispose(); _des.Clear(); return(System.Text.Encoding.UTF8.GetString(_result)); }
/// <summary> /// DES解密 /// </summary> /// <param name="input">待解密字串</param> /// <param name="key">解密key</param> /// <returns></returns> private static string DESDecrypt(string input, string key) { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); des.Key = Encoding.UTF8.GetBytes(key); des.IV = new byte[8]; System.Security.Cryptography.ICryptoTransform cTransform = des.CreateDecryptor(des.Key, des.IV); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, cTransform, System.Security.Cryptography.CryptoStreamMode.Write); byte[] byt = Convert.FromBase64String(input); cStream.Write(byt, 0, byt.Length); cStream.FlushFinalBlock(); cStream.Close(); return(Encoding.UTF8.GetString(mStream.ToArray())); }
/// <summary> /// 从加密的密钥交换数据中提取机密信息<br/> /// </summary> /// <param name="str">被加密的字符串</param> /// <param name="key">密钥</param> /// <returns>还原的字符串</returns> public static string DecryptString(string str, string key) { try { //义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象. System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //对密钥进行编码. byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //初始化机密适配器.. des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //返回由以 64 为基的二进制数组 byte[] bytesIn = System.Convert.FromBase64String(str); System.IO.MemoryStream msIn = new System.IO.MemoryStream(bytesIn); //定义基本的加密转换运算. System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor(); //定义将数据流链接到加密转换的流。. System.Security.Cryptography.CryptoStream cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); //以UTF8编码从字节流中读取字符. System.IO.StreamReader srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8); //以UTF8编码从字节流中读取字符. string result = srOut.ReadToEnd(); srOut.Close(); cryptStreem.Close(); msIn.Close(); return result; } catch (Exception ep) { throw (ep); // return ""; } }
/// <summary> /// DES加密 /// </summary> /// <param name="encryptString">要加密的字串</param> /// <param name="encryptKey">Key</param> /// <returns>加密后字串</returns> public string DESEncode(string encryptString, string encryptKey) { byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; encryptKey = encryptKey.Length > 8 ? encryptKey.Substring(0, 8) : encryptKey; encryptKey = encryptKey.PadRight(8, ' '); byte[] rgbKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(encryptString); System.Security.Cryptography.DESCryptoServiceProvider dCSP = new System.Security.Cryptography.DESCryptoServiceProvider(); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Convert.ToBase64String(mStream.ToArray())); }
/// <summary> /// 兼容其他语言的 DES 加密算法, 故设置以下配置进行兼容 /// </summary> /// <returns></returns> private static System.Security.Cryptography.DESCryptoServiceProvider initProvider() { System.Security.Cryptography.DESCryptoServiceProvider r = new System.Security.Cryptography.DESCryptoServiceProvider(); r.Padding = System.Security.Cryptography.PaddingMode.PKCS7; // 补位 r.Mode = System.Security.Cryptography.CipherMode.ECB; // 兼容其他语言的Des加密算法 if (sRgbKey == null) { sRgbKey = Encoding.UTF8.GetBytes(sEncryptionKey); } r.Key = sRgbKey; return(r); }
//-----------------------------------------------Encryption&Decryption------------------------------------------------ private string Encrypt(string str) { string EncrptKey = "0806;[pnuLIT)lOFRUtEChnologieS"; byte[] byKey = { }; byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 }; byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8)); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(str); MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return(Convert.ToBase64String(ms.ToArray())); }
public static string Encrypt(string data, string seckey) { byte[] byKey = System.Text.Encoding.ASCII.GetBytes(seckey); byte[] by_IV = System.Text.Encoding.ASCII.GetBytes(seckey); System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); int i = cryptoProvider.KeySize; System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cst = new System.Security.Cryptography.CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, by_IV), System.Security.Cryptography.CryptoStreamMode.Write); System.IO.StreamWriter sw = new System.IO.StreamWriter(cst); sw.Write(data); sw.Flush(); cst.FlushFinalBlock(); sw.Flush(); return(System.Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length)); }
/// <summary> /// 暗号化された文字列を復号化する /// </summary> /// <param name="str">暗号化された文字列</param> /// <param name="key">パスワード</param> /// <returns>復号化された文字列</returns> public static string DecryptString(string str, string key) { if (str == null) { return(""); } if (str.Length % 4 > 0) { while (str.Length % 4 != 0) { str += "="; } } //DESCryptoServiceProviderオブジェクトの作成 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //共有キーと初期化ベクタを決定 //パスワードをバイト配列にする byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //共有キーと初期化ベクタを設定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //Base64で文字列をバイト配列に戻す byte[] bytesIn = System.Convert.FromBase64String(str); //暗号化されたデータを読み込むためのMemoryStream 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 StreamReader srOut = new StreamReader(cryptStreem, System.Text.Encoding.UTF8); //復号化されたデータを取得する string result = srOut.ReadToEnd(); //閉じる srOut.Close(); cryptStreem.Close(); msIn.Close(); return(result); }
public string Cifrar(string Cadena) { byte[] PlainText; PlainText = Encoding.ASCII.GetBytes(Cadena); MemoryStream memdata = new MemoryStream(); System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cryptostream = new System.Security.Cryptography.CryptoStream(memdata, DES.CreateEncryptor(Encoding.ASCII.GetBytes(Default8Key), Encoding.ASCII.GetBytes(Default8VI)), System.Security.Cryptography.CryptoStreamMode.Write); cryptostream.Write(PlainText, 0, PlainText.Length); cryptostream.FlushFinalBlock(); cryptostream.Close(); return(Convert.ToBase64String(memdata.ToArray())); }
public static string DESEncrypt(string originalValue, string key, string IV) { IV += "12345678"; key = key.Substring(0, 8); IV = IV.Substring(0, 8); System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.DESCryptoServiceProvider { Key = System.Text.Encoding.UTF8.GetBytes(key), IV = System.Text.Encoding.UTF8.GetBytes(IV) }.CreateEncryptor(); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(originalValue); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, transform, System.Security.Cryptography.CryptoStreamMode.Write); cryptoStream.Write(bytes, 0, bytes.Length); cryptoStream.FlushFinalBlock(); cryptoStream.Close(); return(System.Convert.ToBase64String(memoryStream.ToArray())); }
/// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> private static string Decrypt(string Text, string sKey) { try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.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(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } catch { return Text; } }
//DecryptFile public static System.IO.StreamReader DecryptFile(string inputfilename, string key) { System.IO.StreamReader streamreader = null; try { System.Security.Cryptography.DESCryptoServiceProvider DESCtoSP = new System.Security.Cryptography.DESCryptoServiceProvider(); DESCtoSP.Key = ASCIIEncoding.ASCII.GetBytes(key); DESCtoSP.IV = ASCIIEncoding.ASCII.GetBytes(key); System.IO.FileStream filestream = new System.IO.FileStream(inputfilename, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.Security.Cryptography.ICryptoTransform ICtoTf = DESCtoSP.CreateDecryptor(); System.Security.Cryptography.CryptoStream cryptostream = new System.Security.Cryptography.CryptoStream(filestream, ICtoTf, System.Security.Cryptography.CryptoStreamMode.Read); streamreader = new System.IO.StreamReader(cryptostream); } catch (System.IO.FileNotFoundException ex) { System.Windows.Forms.MessageBox.Show("Connect error 009: " + ex.Message, "Notifications", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(streamreader); }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public string DecryptDes(string decryptString, string decryptKey) { try { byte[] rgbKey = System.Text.Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = Convert.FromBase64String(decryptString); System.Security.Cryptography.DESCryptoServiceProvider DCSP = new System.Security.Cryptography.DESCryptoServiceProvider(); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(System.Text.Encoding.UTF8.GetString(mStream.ToArray())); } catch { return(decryptString); } }
/// <summary> /// 使用DES解密 /// </summary> /// <param name="encryptedValue">待解密的字符串</param> /// <param name="key">密钥(最大长度8)</param> /// <param name="IV">m初始化向量(最大长度8)</param> /// <returns>解密后的字符串</returns> public static string DESDecrypt(string encryptedValue, string key) { if (string.IsNullOrEmpty(encryptedValue)) { return(""); } try { //将key和IV处理成8个字符 key += "12345678"; string IV = key + "12345678"; key = key.Substring(0, 8); IV = IV.Substring(0, 8); System.Security.Cryptography.SymmetricAlgorithm sa; System.Security.Cryptography.ICryptoTransform ct; System.IO.MemoryStream ms; System.Security.Cryptography.CryptoStream cs; byte[] byt; sa = new System.Security.Cryptography.DESCryptoServiceProvider(); sa.Key = System.Text.Encoding.UTF8.GetBytes(key); sa.IV = System.Text.Encoding.UTF8.GetBytes(IV); ct = sa.CreateDecryptor(); byt = Convert.FromBase64String(encryptedValue); ms = new System.IO.MemoryStream(); cs = new System.Security.Cryptography.CryptoStream(ms, ct, System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return(System.Text.Encoding.UTF8.GetString(ms.ToArray())); } catch { } return(""); }
/// <summary> /// 取得解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public static string Decrypt(string decryptString) { try { Des des = new Des(); byte[] rgbKey = Encoding.UTF8.GetBytes(des.encryptKey.Substring(0, 8)); byte[] rgbIV = Encoding.UTF8.GetBytes(des.encryptIV.Substring(0, 8)); byte[] inputByteArray = Convert.FromBase64String(decryptString); System.Security.Cryptography.DESCryptoServiceProvider DCSP = new System.Security.Cryptography.DESCryptoServiceProvider(); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } catch (Exception) { return(decryptString); } }
/// <summary> /// 字符串加密 /// </summary> /// <param name="password">欲加密的字符串</param> /// <param name="key">密钥</param> /// <returns>加密后的字符串</returns> public static string PasswordEncrypt(string password, string key) { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); byte[] bytInput = System.Text.Encoding.Default.GetBytes(password); des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(key); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(key); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream((System.IO.Stream)ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(bytInput, 0, bytInput.Length); cs.FlushFinalBlock(); System.Text.StringBuilder sbRet = new StringBuilder(); foreach (byte b in ms.ToArray()) { sbRet.AppendFormat("{0:X2}", b); } return(sbRet.ToString()); }
private static string DesDecrypt(string DecryptString) { try { byte[] inputByteArray = Convert.FromBase64String(DecryptString); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); des.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; des.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(System.Text.Encoding.UTF8.GetString(mStream.ToArray())); } catch { return(""); } }
public static string Decrypt_Des(this object value, string desKey, string desIV) { string cryptedString = value as string; byte[] Key = GetBytes(desKey); byte[] IV = GetBytes(desIV); // Check arguments. if (string.IsNullOrEmpty(cryptedString)) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV"); System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString)); System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(Key, IV), System.Security.Cryptography.CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); return reader.ReadToEnd(); }
public WsgFileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) : base(path, mode, access, share) { try { byte[] key = { 77, 7, 77, 7, 77, 7, 77, 7 }; byte[] iv = { 7, 77, 7, 77, 7, 77, 7, 77 }; DES = new System.Security.Cryptography.DESCryptoServiceProvider(); if (this.CanRead) { CryptoReadStream = new System.Security.Cryptography.CryptoStream(this, DES.CreateDecryptor(key, iv), System.Security.Cryptography.CryptoStreamMode.Read); } if (this.CanWrite) { CryptoWriteStream = new System.Security.Cryptography.CryptoStream(this, DES.CreateEncryptor(key, iv), System.Security.Cryptography.CryptoStreamMode.Write); } } catch (System.Exception ex) { throw ex; } }
/// <summary> /// 暗号化された文字列を復号化する /// </summary> /// <param name="str">暗号化された文字列</param> /// <param name="key">パスワード</param> /// <returns>復号化された文字列</returns> public static string DecryptString(string str, string key) { //DESCryptoServiceProviderオブジェクトの作成 var des = new System.Security.Cryptography.DESCryptoServiceProvider(); // 共有キーと初期化ベクタを決定 // パスワードをバイト配列にする var bytesKey = System.Text.Encoding.UTF8.GetBytes(key); // 共有キーと初期化ベクタを設定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); // Base64で文字列をバイト配列に戻す var bytesIn = System.Convert.FromBase64String(str); // 暗号化されたデータを読み込むためのMemoryStream var msIn = new System.IO.MemoryStream(bytesIn); // DES復号化オブジェクトの作成 var desdecrypt = des.CreateDecryptor(); // 読み込むためのCryptoStreamの作成 var cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); // 復号化されたデータを取得するためのStreamReader var srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8); // 復号化されたデータを取得する var result = srOut.ReadToEnd(); // 閉じる srOut.Close(); cryptStreem.Close(); msIn.Close(); return result; }
/// <summary> /// DES解密算法 /// </summary> /// <param name="Source">要解密的字符串</param> /// <param name="SecretKey">解密密钥(8的整数倍字节数的字符串)</param> /// <returns>解密后的结果字符串</returns> public static string DES_Decode(byte[] Source, string SecretKey) { System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider(); provider.Key = System.Text.Encoding.UTF8.GetBytes(SecretKey); provider.IV = System.Text.Encoding.UTF8.GetBytes(SecretKey); System.IO.MemoryStream stream2 = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream stream = new System.Security.Cryptography.CryptoStream(stream2, provider.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); stream.Write(Source, 0, Source.Length); try { stream.FlushFinalBlock(); } catch (System.Security.Cryptography.CryptographicException) { return ""; } return System.Text.Encoding.UTF8.GetString(stream2.ToArray()); }
/// <summary> /// DES加密算法 /// </summary> /// <param name="Source">要加密的字符串</param> /// <param name="SecretKey">加密密钥(8的整数倍字节数的字符串)</param> /// <returns>加密后的结果字符串</returns> public static byte[] DES_Encode(string Source, string SecretKey) { System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider(); try { provider.Key = System.Text.Encoding.UTF8.GetBytes(SecretKey); } catch (System.ArgumentException) { byte[] buffer = new byte[0]; return buffer; } provider.IV = System.Text.Encoding.UTF8.GetBytes(SecretKey); System.IO.MemoryStream stream2 = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream stream = new System.Security.Cryptography.CryptoStream(stream2, provider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); stream.Write(System.Text.Encoding.UTF8.GetBytes(Source), 0, System.Text.Encoding.UTF8.GetBytes(Source).Length); stream.FlushFinalBlock(); return stream2.ToArray(); }
/// <summary> /// 根据密钥进行字符串加密. /// </summary> /// <param name="str">要加密的字符串</param> /// <param name="key">密钥</param> /// <returns>机密后的字符串</returns> public static string EncryptString(string str, string key) { try { //根据UTF8编码规则取得二进制流.. byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str); //访问数据加密标准 (DES) 算法的加密服务提供程序. System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //对密钥进行编码. byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //初始化机密适配器.. des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); System.IO.MemoryStream msOut = new System.IO.MemoryStream(); //基本的加密转换运算. System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateEncryptor(); //定义将数据流链接到加密转换的流. 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(); //生成由以 64 为基的二进制数组. return System.Convert.ToBase64String(bytesOut); } catch (Exception e) { throw (e); // return ""; } }
/// <summary> /// �Í������ꂽ������������� /// </summary> /// <param name="str">�Í������ꂽ������</param> /// <param name="key">�p�X���[�h</param> /// <returns>���������ꂽ������</returns> public static string DecryptString(string str, string key) { if (str == null) { return ""; } if (str.Length % 4 > 0) { while (str.Length % 4 != 0) { str += "="; } } //DESCryptoServiceProvider�I�u�W�F�N�g�̍쐬 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //���L�L�[�Ə������x�N�^����� //�p�X���[�h��o�C�g�z��ɂ��� byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //���L�L�[�Ə������x�N�^��ݒ� des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //Base64�ŕ������o�C�g�z��ɖ߂� byte[] bytesIn = System.Convert.FromBase64String(str); //�Í������ꂽ�f�[�^��ǂݍ��ނ��߂�MemoryStream MemoryStream msIn = new System.IO.MemoryStream(bytesIn); //DES�������I�u�W�F�N�g�̍쐬 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); //���������ꂽ�f�[�^��擾���邽�߂�StreamReader StreamReader srOut = new StreamReader(cryptStreem, System.Text.Encoding.UTF8); //���������ꂽ�f�[�^��擾���� string result = srOut.ReadToEnd(); //���� srOut.Close(); cryptStreem.Close(); msIn.Close(); return result; }
/// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> private static string Encrypt(string Text, string sKey) { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.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(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.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(); }
public static string Encrypt(string toEncrypt, string key) { var des = new System.Security.Cryptography.DESCryptoServiceProvider(); var ms = new System.IO.MemoryStream(); VerifyKey(ref key); des.Key = HashKey(key, des.KeySize / 8); des.IV = HashKey(key, des.KeySize / 8); byte[] inputBytes = Encoding.UTF8.GetBytes(toEncrypt); var cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputBytes, 0, inputBytes.Length); cs.FlushFinalBlock(); return HttpServerUtility.UrlTokenEncode(ms.ToArray()); }
/// <summary> /// 文字列を暗号化する /// </summary> /// <param name="str">暗号化する文字列</param> /// <param name="key">パスワード</param> /// <returns>暗号化された文字列</returns> public static string EncryptString(string str, string key) { // 文字列をバイト型配列にする var bytesIn = System.Text.Encoding.UTF8.GetBytes(str); // DESCryptoServiceProviderオブジェクトの作成 var des = new System.Security.Cryptography.DESCryptoServiceProvider(); // 共有キーと初期化ベクタを決定 // パスワードをバイト配列にする var bytesKey = System.Text.Encoding.UTF8.GetBytes(key); // 共有キーと初期化ベクタを設定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); // 暗号化されたデータを書き出すためのMemoryStream var msOut = new System.IO.MemoryStream(); // DES暗号化オブジェクトの作成 var desdecrypt = des.CreateEncryptor(); // 書き込むためのCryptoStreamの作成 var 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(); // Base64で文字列に変更して結果を返す return System.Convert.ToBase64String(bytesOut); }
public static string Encrypt_Des(this object value, string desKey, string desIV) { string originalString = value as string; byte[] Key = GetBytes(desKey); byte[] IV = GetBytes(desIV); if (String.IsNullOrEmpty(originalString)) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV"); System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(); System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(Key, IV), System.Security.Cryptography.CryptoStreamMode.Write); StreamWriter writer = new StreamWriter(cryptoStream); writer.Write(originalString); writer.Flush(); cryptoStream.FlushFinalBlock(); writer.Flush(); return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); }