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); }
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); }
/// <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(); } }
protected override void Dispose(bool disposing) { if (_cryptoStream != null) { _cryptoStream.Dispose(); _cryptoStream = null; } if (_cryptoTransform != null) { _cryptoTransform.Dispose(); _cryptoTransform = null; } base.Dispose(disposing); }
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; }
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; }
/// <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(); } }