public CipherParams(string algorithm, byte[] key, CipherMode?mode = null, byte[] iv = null) { Algorithm = algorithm.IsEmpty() ? Crypto.DefaultAlgorithm : algorithm; Key = key; Mode = mode ?? Crypto.DefaultMode; Iv = iv; }
public static System.Security.Cryptography.CipherMode MapAblyMode(CipherMode?mode) { if (mode == null) { return(System.Security.Cryptography.CipherMode.CBC); } return(ModesMap[mode.Value]); }
public static byte[] GenerateRandomKey(int?keyLength = null, CipherMode?mode = null) { if (keyLength.HasValue) { ValidateKeyLength(keyLength.Value); } return(AesCipher.GenerateKey(mode, keyLength)); }
public static byte[] GenerateKey(CipherMode?mode, int?keyLength) { using (var aes = Aes.Create()) { aes.KeySize = keyLength ?? Crypto.DefaultKeylength; aes.Mode = MapAblyMode(mode); aes.Padding = PaddingMode.PKCS7; aes.BlockSize = Crypto.DefaultBlocklength * 8; aes.GenerateKey(); return(aes.Key); } }
public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv, int?keySize = null, int?blockSize = null, CipherMode?mode = null, PaddingMode?padding = null) { CheckKeySize(keySize); return(__Encrypt__ <AesCryptoServiceProvider>(data: data, key: key, iv: iv, keySize: keySize, blockSize: blockSize, mode: mode, padding: padding)); }
/// <summary> /// 对称加密算法 - 解密 /// </summary> /// <param name="algorithm">具体对称加密算法实现类.</param> /// <param name="base64Text">base64字符串密文</param> /// <param name="key">密钥(加解密须保持一直)</param> /// <param name="iv">算法初始化向量IV(加解密须保持一直)</param> /// <param name="cipherMode">加密模式</param> /// <param name="paddingMode">加密数据填充方式</param> /// <returns>返回明文</returns> public static string Decrypt(SymmetricAlgorithm algorithm, string base64Text, byte[] key, byte[] iv, CipherMode?cipherMode = null, PaddingMode?paddingMode = null) { byte[] plainBytes; // Convert the base64 string to byte array. byte[] cipherBytes = Convert.FromBase64String(base64Text); algorithm.Key = key; algorithm.IV = iv; if (cipherMode.HasValue) { algorithm.Mode = cipherMode.Value; } if (paddingMode.HasValue) { algorithm.Padding = paddingMode.Value; } using (MemoryStream memoryStream = new MemoryStream(cipherBytes)) { using (CryptoStream cs = new CryptoStream(memoryStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read)) { plainBytes = new byte[cipherBytes.Length]; cs.Read(plainBytes, 0, cipherBytes.Length); } } string recoveredMessage; using (MemoryStream stream = new MemoryStream(plainBytes, false)) { BinaryFormatter bf = new BinaryFormatter(); recoveredMessage = bf.Deserialize(stream).ToString(); } return(recoveredMessage); }
/// <summary> /// 对称加密算法 - 加密, 如: DES, TripleDes /// </summary> /// <param name="algorithm">具体对称加密算法实现类</param> /// <param name="plainText">明文</param> /// <param name="key">密钥(加解密须保持一直)</param> /// <param name="iv">算法初始化向量IV(加解密须保持一直)</param> /// <param name="cipherMode">加密模式</param> /// <param name="paddingMode">加密数据填充方式</param> /// <returns>base64加密字符串</returns> public static string Encrypt(SymmetricAlgorithm algorithm, string plainText, byte[] key, byte[] iv, CipherMode?cipherMode = null, PaddingMode?paddingMode = null) { byte[] plainBytes; byte[] cipherBytes; algorithm.Key = key; algorithm.IV = iv; if (cipherMode.HasValue) { algorithm.Mode = cipherMode.Value; } if (paddingMode.HasValue) { algorithm.Padding = paddingMode.Value; } BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream()) { bf.Serialize(stream, plainText); plainBytes = stream.ToArray(); } using (MemoryStream ms = new MemoryStream()) { // Defines a stream for cryptographic transformations CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write); // Writes a sequence of bytes for encrption cs.Write(plainBytes, 0, plainBytes.Length); // Closes the current stream and releases any resources cs.Close(); // Save the ciphered message into one byte array cipherBytes = ms.ToArray(); // Closes the memorystream object ms.Close(); } string base64Text = Convert.ToBase64String(cipherBytes); return(base64Text); //return Encoding.Default.GetString(cipherBytes); }
public static CipherParams GetDefaultParams(byte[] key = null, byte[] iv = null, CipherMode?mode = null) { if (key != null && key.Any()) { ValidateKeyLength(key.Length * 8); return(new CipherParams(DefaultAlgorithm, key, mode, iv)); } return(new CipherParams(GenerateRandomKey(mode: mode))); }
public static CipherParams GetDefaultParams(string base64EncodedKey, string base64Iv = null, CipherMode?mode = null) { if (base64EncodedKey == null) { throw new ArgumentNullException(nameof(base64EncodedKey), "Base64Encoded key cannot be null"); } return(GetDefaultParams(base64EncodedKey.FromBase64(), base64Iv?.FromBase64(), mode)); }
/// <summary> /// AES加密 /// </summary> /// <param name="aes"></param> /// <param name="data">待加密的数据</param> /// <param name="key">加密密钥,为空则使用实体属性Key</param> /// <param name="mode">加密模式,为空则使用实体属性Mode</param> /// <param name="padding">填充算法,为空则使用实体属性Padding</param> /// <returns>加密后数据</returns> public static byte[] Encrypt(this System.Security.Cryptography.Aes aes, byte[] data, byte[] key = null, CipherMode?mode = null, PaddingMode?padding = null) { return(aes.Crypto(true, data, key, mode, padding)); }
/// <summary> /// AES加密 /// </summary> /// <param name="aes"></param> /// <param name="str">待加密的字符串</param> /// <param name="encoding">待加密的字符串编码格式</param> /// <param name="key">加密密钥,为空则使用实体属性Key</param> /// <param name="mode">加密模式,为空则使用实体属性Mode</param> /// <param name="padding">填充算法,为空则使用实体属性Padding</param> /// <returns>base64编码格式加密字符串</returns> public static string Encrypt(this System.Security.Cryptography.Aes aes, string str, Encoding encoding, byte[] key = null, CipherMode?mode = null, PaddingMode?padding = null) { var data = encoding.GetBytes(str); var encData = aes.Encrypt(data, key, mode, padding); return(Convert.ToBase64String(encData)); }
/// <summary> /// AES加解密 /// </summary> /// <param name="aes"></param> /// <param name="forEncryption">是否加密(false为解密)</param> /// <param name="data">待加解密的数据</param> /// <param name="key">加解密密钥,为空则使用实体属性Key</param> /// <param name="mode">加解密模式,为空则使用实体属性Mode</param> /// <param name="padding">填充算法,为空则使用实体属性Padding</param> /// <returns>加解密后数据</returns> public static byte[] Crypto(this System.Security.Cryptography.Aes aes, bool forEncryption, byte[] data, byte[] key = null, CipherMode?mode = null, PaddingMode?padding = null) { aes.Key = key ?? aes.Key; aes.Mode = mode ?? aes.Mode; aes.Padding = padding ?? aes.Padding; var cryptor = forEncryption ? aes.CreateEncryptor() : aes.CreateDecryptor(); return(cryptor.TransformFinalBlock(data, 0, data.Length)); }
/// <summary> /// AES加密 /// </summary> /// <param name="aes"></param> /// <param name="str">待加密的字符串(UTF8编码)</param> /// <param name="key">加密密钥,为空则使用实体属性Key</param> /// <param name="mode">加密模式,为空则使用实体属性Mode</param> /// <param name="padding">填充算法,为空则使用实体属性Padding</param> /// <returns>base64编码格式加密字符串</returns> public static string Encrypt(this System.Security.Cryptography.Aes aes, string str, byte[] key = null, CipherMode?mode = null, PaddingMode?padding = null) { return(aes.Encrypt(str, Encoding.UTF8, key, mode, padding)); }
/// <summary> /// DES加解密 /// </summary> /// <param name="deskey">ASCII编码密钥</param> /// <param name="cipherMode">密文加密模式</param> /// <param name="paddingMode">填充模式</param> public DesEncodeDecode(string deskey, CipherMode cipherMode, PaddingMode paddingMode) : this(deskey) { this._cipherMode = cipherMode; this._paddingMode = paddingMode; }