public byte[] sign() { //byte[] sig=signature.sign(); cs.Close(); System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider(); DSA.ImportParameters(DSAKeyInfo); System.Security.Cryptography.DSASignatureFormatter DSAFormatter = new System.Security.Cryptography.DSASignatureFormatter(DSA); DSAFormatter.SetHashAlgorithm("SHA1"); byte[] sig = DSAFormatter.CreateSignature(sha1); return(sig); }
public static string Encrypt(string input, string pwdValue) { try { byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input); byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue); // AesManaged - 高级加密标准(AES) 对称算法的管理类 System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged(); // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数) // 通过 密码 和 salt 派生密钥 System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt); aes.BlockSize = aes.LegalBlockSizes[0].MaxSize; aes.KeySize = aes.LegalKeySizes[0].MaxSize; aes.Key = rfc.GetBytes(aes.KeySize / 8); aes.IV = rfc.GetBytes(aes.BlockSize / 8); // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象 System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor(); // 加密后的输出流 System.IO.MemoryStream encryptStream = new System.IO.MemoryStream(); // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接 System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream(encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write); // 将一个字节序列写入当前 CryptoStream (完成加密的过程) encryptor.Write(data, 0, data.Length); encryptor.Close(); // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串 string encryptedString = Convert.ToBase64String(encryptStream.ToArray()); return(encryptedString); } catch { } return(input); }
//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="originalData">加密的字节数据</param> /// <param name="keyData">解密密钥</param> /// <param name="ivData"></param> /// <returns>原始文本</returns> private static byte[] Decrypt(byte[] encryptedData, byte[] keyData, byte[] ivData) { MemoryStream memoryStream = new MemoryStream(); //创建Rijndael加密算法 System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create(); rijndael.Key = keyData; rijndael.IV = ivData; System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream( memoryStream, rijndael.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); try { cryptoStream.Write(encryptedData, 0, encryptedData.Length); cryptoStream.Close(); cryptoStream.Dispose(); return(memoryStream.ToArray()); } catch (Exception ex) { LogManager.Instance.WriteLog("GlobalMethods.Decrypt", ex); return(null); } finally { memoryStream.Close(); memoryStream.Dispose(); } }
// ======================================== // static field // ======================================== public static string EncryptString(string str, string password) { var aes = new System.Security.Cryptography.AesCryptoServiceProvider(); var data = System.Text.Encoding.UTF8.GetBytes(str); var passwordBytes = System.Text.Encoding.UTF8.GetBytes(password); aes.Key = ResizeBytesArray(passwordBytes, aes.Key.Length); aes.IV = ResizeBytesArray(passwordBytes, aes.IV.Length); var stream = new System.IO.MemoryStream(); var encryptor = aes.CreateEncryptor(); var cryptStream = new System.Security.Cryptography.CryptoStream( stream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write ); try { cryptStream.Write(data, 0, data.Length); cryptStream.FlushFinalBlock(); var encrypted = stream.ToArray(); return(System.Convert.ToBase64String(encrypted)); } finally { cryptStream.Close(); stream.Close(); } }
/// <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; } }
public static string Decrypt(string decryptValue) { string[] csbytes = decryptValue.Split('.'); byte[] temp = new byte[csbytes.Length]; StringBuilder sUC = new StringBuilder(); for (int ictr = 0; ictr < csbytes.Length; ictr++) { temp[ictr] = Convert.ToByte(csbytes[ictr].ToString()); } string key = "12345678912345678912345678912345"; string Indicator = "mdhyadhsddmyghjq"; byte[] Key = Encoding.ASCII.GetBytes(key); byte[] IV = Encoding.ASCII.GetBytes(Indicator); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create(); alg.Key = Key; alg.IV = IV; System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, alg.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(temp, 0, temp.Length); cs.Close(); byte[] decryptedData = ms.ToArray(); string org; org = (Encoding.UTF8.GetString(decryptedData)); return(org); }
private byte[] Decrypt(byte[] EncData) { byte[] Result = null; try { System.Security.Cryptography.RijndaelManaged Enc = new System.Security.Cryptography.RijndaelManaged(); Enc.KeySize = 256; Enc.Key = this.Encryption_Key(); Enc.IV = this.Encryption_IV(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(EncData); System.Security.Cryptography.CryptoStream cryptoStream = null; cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, Enc.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read); byte[] TempDecryptArr = null; TempDecryptArr = new byte[EncData.Length + 1]; int decryptedByteCount = 0; decryptedByteCount = cryptoStream.Read(TempDecryptArr, 0, EncData.Length); cryptoStream.Close(); memoryStream.Close(); cryptoStream.Dispose(); memoryStream.Dispose(); Result = new byte[decryptedByteCount + 1]; Array.Copy(TempDecryptArr, Result, decryptedByteCount); } catch (Exception) { Result = null; } return(Result); }
public static string Encrypt(string encryptValue) { string key = "12345678912345678912345678912345"; string Indicator = "mdhyadhsddmyghjq"; byte[] password = Encoding.ASCII.GetBytes(encryptValue); byte[] Key = Encoding.ASCII.GetBytes(key); byte[] IV = Encoding.ASCII.GetBytes(Indicator); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create(); alg.Key = Key; alg.IV = IV; System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, alg.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(password, 0, password.Length); cs.Close(); byte[] encryptedData = ms.ToArray(); string str = null; //convert byte into comma seprate string for (int i = 0; i < encryptedData.Length; i++) { str = str + encryptedData[i] + '.'; } //trim , at the end str = str.TrimEnd('.'); return(str); }
private byte[] Encrypt(byte[] PlainData) { byte[] Result = null; try { System.Security.Cryptography.RijndaelManaged Enc = new System.Security.Cryptography.RijndaelManaged(); Enc.KeySize = 256; Enc.Key = this.Encryption_Key(); Enc.IV = this.Encryption_IV(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cryptoStream = null; cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, Enc.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cryptoStream.Write(PlainData, 0, PlainData.Length); cryptoStream.FlushFinalBlock(); Result = memoryStream.ToArray(); cryptoStream.Close(); memoryStream.Close(); cryptoStream.Dispose(); memoryStream.Dispose(); } catch (Exception) { Result = null; } return(Result); }
public string Decrypt(string inVal) { try { System.IO.MemoryStream MSout = new System.IO.MemoryStream(); byte[] bin; byte[] retArr; //Create variables to help with read and write. System.Security.Cryptography.SymmetricAlgorithm encAlg = System.Security.Cryptography.SymmetricAlgorithm.Create("RC2"); System.Security.Cryptography.CryptoStream DecStream = new System.Security.Cryptography.CryptoStream(MSout, encAlg.CreateDecryptor(bKey, bIV), System.Security.Cryptography.CryptoStreamMode.Write); bin = DeformatHexString(inVal); DecStream.Write(bin, 0, bin.Length); DecStream.Close(); retArr = MSout.ToArray(); MSout.Close(); System.Text.ASCIIEncoding getStr = new ASCIIEncoding(); return(getStr.GetString(retArr)); } catch (System.Exception ex) { // Log Error throw ex; } }
public string Encrypt(string inVal) { try { //System.Text.Encoder encoding; //System.Text.Encoder encoding = System.Text.Encoding.ASCII System.IO.MemoryStream MSout = new System.IO.MemoryStream(); //Create variables to help with read and write. byte[] bin; //This is intermediate storage for the encryption. System.Security.Cryptography.SymmetricAlgorithm encAlg = System.Security.Cryptography.SymmetricAlgorithm.Create("RC2"); System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(MSout, encAlg.CreateEncryptor(bKey, bIV), System.Security.Cryptography.CryptoStreamMode.Write); bin = ConvertStringToByteArray(inVal); encStream.Write(bin, 0, inVal.Length); encStream.Close(); bin = MSout.ToArray(); MSout.Close(); return(formatHexString(bin)); } catch (System.Exception ex) { // Log Error throw ex; } }
public static string Decrypt(string CipherText, string Password, string Salt = "Kosher", string HashAlgorithm = "SHA1", int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(CipherText)) { return(""); } byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); byte[] CipherTextBytes = Convert.FromBase64String(CipherText); System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged(); SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; byte[] PlainTextBytes = new byte[CipherTextBytes.Length]; int ByteCount = 0; using (System.Security.Cryptography.ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)) { using (System.IO.MemoryStream MemStream = new System.IO.MemoryStream(CipherTextBytes)) { using (System.Security.Cryptography.CryptoStream CryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read)) { ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); return(Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount)); }
public static string Encrypt(string[] keyArray) { string key = ""; foreach (string s in keyArray) { key = key + s + ";"; } byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); byte[] PlainTextBytes = Encoding.UTF8.GetBytes(key); System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged(); SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; byte[] CipherTextBytes = null; using (System.Security.Cryptography.ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)) { using (System.IO.MemoryStream MemStream = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream CryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Encryptor, System.Security.Cryptography.CryptoStreamMode.Write)) { CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length); CryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); return(Convert.ToBase64String(CipherTextBytes)); }
private string Decrypt(string cipher, string type) { string EncryptionKey; byte[] cipherBytes; EncryptionKey = string.Format(this.Core.GetAttribute("DecryptKey"), type); cipherBytes = Convert.FromBase64String(cipher); using (System.Security.Cryptography.Rijndael encryptor = System.Security.Cryptography.Rijndael.Create()) { System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(EncryptionKey, Convert.FromBase64String(this.Core.GetAttribute("DecryptSalt"))); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipher = System.Text.Encoding.Unicode.GetString(ms.ToArray()); } } return(cipher); }
//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("=", ""); }
public string EncryptPassword(string clearText) { string encryptedText = ""; string EncryptionKey = "Xavier"; byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create()) { System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } encryptedText = Convert.ToBase64String(ms.ToArray()); } } return(encryptedText); }
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); } }
/// <summary> /// 暗号化された文字列を復号化する /// </summary> public static string DecryptString(string str, string password) { var aes = new System.Security.Cryptography.AesCryptoServiceProvider(); var passwordBytes = System.Text.Encoding.UTF8.GetBytes(password); aes.Key = ResizeBytesArray(passwordBytes, aes.Key.Length); aes.IV = ResizeBytesArray(passwordBytes, aes.IV.Length); var data = System.Convert.FromBase64String(str); var stream = new System.IO.MemoryStream(data); var decryptor = aes.CreateDecryptor(); var cryptStreem = new System.Security.Cryptography.CryptoStream( stream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read ); var reader = new System.IO.StreamReader( cryptStreem, System.Text.Encoding.UTF8 ); try { var result = reader.ReadToEnd(); return(result); } finally { reader.Close(); cryptStreem.Close(); stream.Close(); } }
static public byte[] Decode(byte[] Data, System.Security.Cryptography.ICryptoTransform Decoder) { byte[] Outer = new byte[] { }; int rdlen = 0; if (Data.Length > 0) { using (System.IO.MemoryStream memStream = new System.IO.MemoryStream()) { byte[] bin = new byte[128]; int len; System.IO.MemoryStream InputStr = new System.IO.MemoryStream(); InputStr.Write(Data, 0, Data.Length); InputStr.Position = 0; System.Security.Cryptography.CryptoStream CryptStream = new System.Security.Cryptography.CryptoStream(memStream, Decoder, System.Security.Cryptography.CryptoStreamMode.Write); while (rdlen < Data.Length) { len = InputStr.Read(bin, 0, 128); CryptStream.Write(bin, 0, len); rdlen = rdlen + len; } CryptStream.FlushFinalBlock(); Outer = memStream.ToArray(); CryptStream.Close(); } } return(Outer); }
/// <summary> /// Criptografa um array de bytes em outro array de bytes. /// </summary> /// <returns>Array de bytes criptografado.</returns> /// <param name="p_plaintextbytes">Array de bytes.</param> public byte[] EncryptToBytes(byte[] p_plaintextbytes) { byte[] v_ciphertextbytes; byte[] v_plaintextbyteswithsalt; System.IO.MemoryStream v_memory; System.Security.Cryptography.CryptoStream v_crypto; this.Initialize(); v_plaintextbyteswithsalt = this.AddSalt(p_plaintextbytes); v_memory = new System.IO.MemoryStream(); lock (this) { v_crypto = new System.Security.Cryptography.CryptoStream(v_memory, this.v_encryptor, System.Security.Cryptography.CryptoStreamMode.Write); v_crypto.Write(v_plaintextbyteswithsalt, 0, v_plaintextbyteswithsalt.Length); v_crypto.FlushFinalBlock(); v_ciphertextbytes = v_memory.ToArray(); v_memory.Close(); v_crypto.Close(); return(v_ciphertextbytes); } }
public static void KriptoFile(string dosyalar, string password = "******", string uzanti = ".uzanti") { System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(password, @byte); System.IO.FileStream fs = new System.IO.FileStream(dosyalar + uzanti, System.IO.FileMode.Create); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(fs, rm.CreateEncryptor(rfc.GetBytes(32), rfc.GetBytes(16)), System.Security.Cryptography.CryptoStreamMode.Write); System.IO.FileStream fs2 = new System.IO.FileStream(dosyalar, System.IO.FileMode.Open, System.IO.FileAccess.Read); int temp; temp = fs2.ReadByte(); while (temp != -1) { cs.WriteByte((byte)temp); temp = fs2.ReadByte(); } //Close işlemleri , silmeyin. Mümkünse hiç bi' yeri ellemeyin. cs.Close(); fs.Close(); fs2.Close(); System.IO.File.Delete(dosyalar); //Bu biraz farklı , ilk önce dosyaların kopyasını oluşturup şifreler. Daha sonra siler. }
public static string DecryptPAN(string encryptedPAN) { // Log log = new Log(LogPath); System.Security.Cryptography.SymmetricAlgorithm alg = System.Security.Cryptography.TripleDES.Create(); alg.KeySize = 128; alg.Key = Hex2Bin(PEncKey); alg.IV = Hex2Bin(PEncIV); alg.Padding = System.Security.Cryptography.PaddingMode.None; alg.Mode = System.Security.Cryptography.CipherMode.CBC; byte[] buf = new byte[16]; Hex2Bin(encryptedPAN, buf); try { MemoryStream outs = new MemoryStream(); System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(outs, alg.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); encStream.Write(buf, 0, 16); encStream.FlushFinalBlock(); Buffer.BlockCopy(outs.GetBuffer(), 0, buf, 0, 16); encStream.Close(); return(Bin2Hex(buf).Trim('A')); } catch { } return(null); }
public string EncryptString(string value, string keyString) { try { byte[] resultBA = new byte[value.Length], valueBA = new byte[value.Length]; byte[] iv = new byte[] { 0x14, 0xD7, 0x5B, 0xA2, 0x47, 0x83, 0x0F, 0xC4 }; System.Text.ASCIIEncoding ascEncoding = new System.Text.ASCIIEncoding(); byte[] key = new byte[24]; ascEncoding.GetBytes(keyString, 0, keyString.Length < 24?keyString.Length:24, key, 0); MemoryStream memStream = new MemoryStream(); byte[] tempBA = new byte[value.Length]; ascEncoding.GetBytes(value, 0, value.Length, tempBA, 0); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(memStream, System.Security.Cryptography.TripleDESCryptoServiceProvider.Create().CreateEncryptor(key, iv), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(tempBA, 0, tempBA.Length); cStream.FlushFinalBlock(); resultBA = memStream.ToArray(); cStream.Close(); return(InternalMethods.BytesToHexString(resultBA)); } catch (Exception exc) { LogEvent(exc.Source, "EncryptString()", exc.ToString(), 4); return(""); } }
public static string Encrypt(string plainText, string encryptionKey) { //string EncryptionKey = "tahaahat"; if (plainText == null) { return(string.Empty); } byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(plainText); using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create()) { System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } plainText = HttpServerUtility.UrlTokenEncode(ms.ToArray()); } } return(plainText); }
public byte[] doFinal() { // System.arraycopy(mac.doFinal(), 0, buf, 0, 12); // return buf; cs.Close(); Array.Copy(mentalis_mac.Hash, 0, buf, 0, 12); return(buf); }
public virtual byte[] doFinal() { //return mac.doFinal(); cs.Close(); byte[] result = hmac.Hash; hmac.Initialize(); cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, hmac, System.Security.Cryptography.CryptoStreamMode.Write); return(result); }
public override byte[] digest() { cs.Close(); byte[] result = md.Hash; md.Clear(); //Reinitiazing hash objects init(); return(result); }
public byte[] digest() { //return md.digest(); cs.Close(); byte[] result = md.Hash; md.Clear(); Init(); //Reinitiazing hash objects return(result); }
// Decrypt a file into another file using a password. public static void Decrypt(string sInputFilepath, string sOutputFilepath, string sPassword) { System.IO.FileStream fsIn = null; System.IO.FileStream fsOut = null; //System.Security.Cryptography.PasswordDeriveBytes pdb = null; System.Security.Cryptography.Rfc2898DeriveBytes pdb2 = null; System.Security.Cryptography.Rijndael alg = null; System.Security.Cryptography.CryptoStream cs = null; try { // First we are going to open the file streams. fsIn = new System.IO.FileStream(sInputFilepath, System.IO.FileMode.Open, System.IO.FileAccess.Read); fsOut = new System.IO.FileStream(sOutputFilepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); // Then we are going to derive a Key and an IV from the Password and create an algorithm. //pdb = new System.Security.Cryptography.PasswordDeriveBytes(sPassword, new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76}); pdb2 = new System.Security.Cryptography.Rfc2898DeriveBytes(sPassword, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); alg = System.Security.Cryptography.Rijndael.Create(); //alg.Key = pdb.GetBytes(32); alg.Key = pdb2.GetBytes(32); //alg.IV = pdb.GetBytes(16); alg.IV = pdb2.GetBytes(16); // Now create a crypto stream through which we are going to be pumping data. // Our fileOut is going to be receiving the Decrypted bytes. cs = new System.Security.Cryptography.CryptoStream(fsOut, alg.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); // Now will will initialize a buffer and will be processing the input file in chunks. // This is done to avoid reading the whole file (which can be huge) into memory. int iBufferLen = 4096; byte[] buffer = new byte[iBufferLen]; int iRead; do { // read a chunk of data from the input file. iRead = fsIn.Read(buffer, 0, iBufferLen); // Decrypt it. cs.Write(buffer, 0, iRead); } while (iRead != 0); } catch (Exception ex) { Logger?.Error(ex); } finally { // close everything. if (cs != null) { cs.Close(); cs = null; } // this will also close the unrelying fsOut stream if (fsIn != null) { fsIn.Close(); fsIn = null; } } }
public byte[] doFinal() { //return mac.doFinal(); cs.Close(); byte[] result = mentalis_mac.Hash; byte[] key = mentalis_mac.Key; mentalis_mac.Clear(); init(key); return(result); }
/// <summary> /// 根据给定的字符串对其进行加密 /// </summary> /// <param name="input">需要加密的字符串</param> /// <returns>加密后的字符串</returns> public static string Encrypt(string input) { // 盐值 string saltValue = "saltValue"; // 密码值 string pwdValue = "pwdValue"; byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input); byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue); // AesManaged - 高级加密标准(AES) 对称算法的管理类 System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged(); // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数) // 通过 密码 和 salt 派生密钥 System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt); /**/ /* * AesManaged.BlockSize - 加密操作的块大小(单位:bit) * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit) * AesManaged.KeySize - 对称算法的密钥大小(单位:bit) * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit) * AesManaged.Key - 对称算法的密钥 * AesManaged.IV - 对称算法的密钥大小 * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥 */ aes.BlockSize = aes.LegalBlockSizes[0].MaxSize; aes.KeySize = aes.LegalKeySizes[0].MaxSize; aes.Key = rfc.GetBytes(aes.KeySize / 8); aes.IV = rfc.GetBytes(aes.BlockSize / 8); // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象 System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor(); // 加密后的输出流 System.IO.MemoryStream encryptStream = new System.IO.MemoryStream(); // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接 System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream (encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write); // 将一个字节序列写入当前 CryptoStream (完成加密的过程) encryptor.Write(data, 0, data.Length); encryptor.Close(); // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串 string encryptedString = Convert.ToBase64String(encryptStream.ToArray()); return encryptedString; }
/// <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> /// Encripcion Byte Key IV /// </summary> /// <param name="clearData">Datos en limpio</param> /// <param name="Key">Llave</param> /// <param name="IV">IV</param> /// <returns>Arreglo de byte's.</returns> private static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.Rijndael alg; alg = System.Security.Cryptography.Rijndael.Create(); alg.Key = Key; alg.IV = IV; System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, alg.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(clearData, 0, clearData.Length); cs.Close(); byte[] encryptedData = ms.ToArray(); return encryptedData; }
public static MemoryStream EncryptStream(string key, byte[] content) { System.Security.Cryptography.SymmetricAlgorithm rijn = System.Security.Cryptography.SymmetricAlgorithm.Create(); using (MemoryStream ms = new MemoryStream()) { byte[] rgbIV = Encoding.ASCII.GetBytes("polychorepolycho"); byte[] rgbKey = Encoding.ASCII.GetBytes(key); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, rijn.CreateEncryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(content, 0, content.Length); cs.Close(); return ms; } }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <returns></returns> public static string Encrypt(string input) { byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input); byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue); System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged(); System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt); aes.BlockSize = aes.LegalBlockSizes[0].MaxSize; aes.KeySize = aes.LegalKeySizes[0].MaxSize; aes.Key = rfc.GetBytes(aes.KeySize / 8); aes.IV = rfc.GetBytes(aes.BlockSize / 8); System.Security.Cryptography.ICryptoTransform encrypt = aes.CreateEncryptor(); System.IO.MemoryStream stream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream (stream, encrypt, System.Security.Cryptography.CryptoStreamMode.Write); encryptor.Write(data, 0, data.Length); encryptor.Close(); return Convert.ToBase64String(stream.ToArray()); }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <returns></returns> public static string Decrypt(string input) { byte[] encryptBytes = Convert.FromBase64String(input); byte[] salt = Encoding.UTF8.GetBytes(saltValue); System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged(); System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt); aes.BlockSize = aes.LegalBlockSizes[0].MaxSize; aes.KeySize = aes.LegalKeySizes[0].MaxSize; aes.Key = rfc.GetBytes(aes.KeySize / 8); aes.IV = rfc.GetBytes(aes.BlockSize / 8); System.Security.Cryptography.ICryptoTransform transform = aes.CreateDecryptor(); System.IO.MemoryStream stream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream (stream, transform, System.Security.Cryptography.CryptoStreamMode.Write); decryptor.Write(encryptBytes, 0, encryptBytes.Length); decryptor.Close(); byte[] decryptBytes = stream.ToArray(); return UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length); }
/// <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> /// �Í������ꂽ������������� /// </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; }
private byte[] Encrypt(byte[] PlainData) { byte[] Result = null; try { System.Security.Cryptography.RijndaelManaged Enc = new System.Security.Cryptography.RijndaelManaged(); Enc.KeySize = 256; Enc.Key = this.Encryption_Key(); Enc.IV = this.Encryption_IV(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cryptoStream = null; cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, Enc.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cryptoStream.Write(PlainData, 0, PlainData.Length); cryptoStream.FlushFinalBlock(); Result = memoryStream.ToArray(); cryptoStream.Close(); memoryStream.Close(); cryptoStream.Dispose(); memoryStream.Dispose(); } catch (Exception) { Result = null; } return Result; }
public static string Decrypt(string CipherText, string Password, string Salt, string HashAlgorithm, int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(CipherText)) return ""; byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); byte[] CipherTextBytes = Convert.FromBase64String(CipherText); System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged(); SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; byte[] PlainTextBytes = new byte[CipherTextBytes.Length]; int ByteCount = 0; using (System.Security.Cryptography.ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)) { using (MemoryStream MemStream = new MemoryStream(CipherTextBytes)) { using (System.Security.Cryptography.CryptoStream CryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read)) { ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount); }
/// <summary> /// Descriptografa um array de bytes em outro array de bytes. /// </summary> /// <returns>Array de bytes descriptografado.</returns> /// <param name="p_ciphertextbytes">Array de bytes criptografado.</param> public byte[] DecryptToBytes(byte[] p_ciphertextbytes) { byte[] v_plaintextbytes; byte[] v_decryptedbytes; int v_decryptedbytecount; int v_saltlength; System.IO.MemoryStream v_memory; System.Security.Cryptography.CryptoStream v_crypto; try { this.Initialize(); v_memory = new System.IO.MemoryStream(p_ciphertextbytes); v_decryptedbytes = new byte[p_ciphertextbytes.Length]; lock (this) { v_crypto = new System.Security.Cryptography.CryptoStream(v_memory, this.v_decryptor, System.Security.Cryptography.CryptoStreamMode.Read); v_decryptedbytecount = v_crypto.Read(v_decryptedbytes, 0, v_decryptedbytes.Length); v_memory.Close(); v_crypto.Close(); } v_saltlength = (v_decryptedbytes[0] & 0x03) | (v_decryptedbytes[1] & 0x0c) | (v_decryptedbytes[2] & 0x30) | (v_decryptedbytes[3] & 0xc0); v_plaintextbytes = new byte[v_decryptedbytecount - v_saltlength]; System.Array.Copy(v_decryptedbytes, v_saltlength, v_plaintextbytes, 0, v_decryptedbytecount - v_saltlength); return v_plaintextbytes; } catch (System.Security.Cryptography.CryptographicException e) { throw new Spartacus.Utils.Exception(e); } }
/// <summary> /// Criptografa um array de bytes em outro array de bytes. /// </summary> /// <returns>Array de bytes criptografado.</returns> /// <param name="p_plaintextbytes">Array de bytes.</param> public byte[] EncryptToBytes(byte[] p_plaintextbytes) { byte[] v_ciphertextbytes; byte[] v_plaintextbyteswithsalt; System.IO.MemoryStream v_memory; System.Security.Cryptography.CryptoStream v_crypto; this.Initialize(); v_plaintextbyteswithsalt = this.AddSalt(p_plaintextbytes); v_memory = new System.IO.MemoryStream(); lock (this) { v_crypto = new System.Security.Cryptography.CryptoStream(v_memory, this.v_encryptor, System.Security.Cryptography.CryptoStreamMode.Write); v_crypto.Write(v_plaintextbyteswithsalt, 0, v_plaintextbyteswithsalt.Length); v_crypto.FlushFinalBlock(); v_ciphertextbytes = v_memory.ToArray(); v_memory.Close(); v_crypto.Close(); return v_ciphertextbytes; } }
/// <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="originalData">ԭʼ�ֽ�����</param> /// <param name="keyData">������Կ</param> /// <param name="ivData"></param> /// <returns>���ܺ������</returns> private static byte[] Encrypt(byte[] originalData, byte[] keyData, byte[] ivData) { MemoryStream memoryStream = new MemoryStream(); //����Rijndael�����㷨 System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create(); rijndael.Key = keyData; rijndael.IV = ivData; System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream( memoryStream, rijndael.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); try { cryptoStream.Write(originalData, 0, originalData.Length); cryptoStream.Close(); cryptoStream.Dispose(); return memoryStream.ToArray(); } catch (Exception ex) { LogManager.Instance.WriteLog("GlobalMethods.Encrypt", ex); return null; } finally { memoryStream.Close(); memoryStream.Dispose(); } }
/// <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); }
private byte[] Decrypt(byte[] EncData) { byte[] Result = null; try { System.Security.Cryptography.RijndaelManaged Enc = new System.Security.Cryptography.RijndaelManaged(); Enc.KeySize = 256; Enc.Key = this.Encryption_Key(); Enc.IV = this.Encryption_IV(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(EncData); System.Security.Cryptography.CryptoStream cryptoStream = null; cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, Enc.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read); byte[] TempDecryptArr = null; TempDecryptArr = new byte[EncData.Length + 1]; int decryptedByteCount = 0; decryptedByteCount = cryptoStream.Read(TempDecryptArr, 0, EncData.Length); cryptoStream.Close(); memoryStream.Close(); cryptoStream.Dispose(); memoryStream.Dispose(); Result = new byte[decryptedByteCount + 1]; Array.Copy(TempDecryptArr, Result, decryptedByteCount); } catch (Exception) { Result = null; } return Result; }