public Encryptor(CryptoAlgorithm algorithm, byte[] key) { SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithm.ToString()); provider.Key = key; provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; encryptor = provider.CreateEncryptor(); decryptor = provider.CreateDecryptor(); }
public static AsymmetricCrypto CreateAsymmetric(CryptoAlgorithm algorithm) { if (algorithm != CryptoAlgorithm.RSA && algorithm != CryptoAlgorithm.DSA) { throw new NotSupportedException(SR.GetString(SRKind.AlgorithmNotSupported, algorithm)); } return(Create(algorithm.ToString()) as AsymmetricCrypto); }
public static HashCrypto CreateHash(CryptoAlgorithm algorithm) { if (algorithm != CryptoAlgorithm.MD5 && algorithm != CryptoAlgorithm.SHA1 && algorithm != CryptoAlgorithm.SHA256 && algorithm != CryptoAlgorithm.SHA384 && algorithm != CryptoAlgorithm.SHA512) { throw new NotSupportedException(SR.GetString(SRKind.AlgorithmNotSupported, algorithm)); } return(Create(algorithm.ToString()) as HashCrypto); }
public static SymmetricCrypto CreateSymmetric(CryptoAlgorithm algorithm) { if (algorithm != CryptoAlgorithm.DES && algorithm != CryptoAlgorithm.AES && algorithm != CryptoAlgorithm.RC2 && algorithm != CryptoAlgorithm.RC4 && algorithm != CryptoAlgorithm.Rijndael && algorithm != CryptoAlgorithm.TripleDES) { throw new NotSupportedException(SR.GetString(SRKind.AlgorithmNotSupported, algorithm)); } return(Create(algorithm.ToString()) as SymmetricCrypto); }
/// <summary> /// 校验加密算法数据模式。 /// </summary> /// <param name="dataMode"></param> protected void ValidateDataMode(DataMode dataMode) { if (!AvailableDataModes.Contains(dataMode)) { throw new NotSupportedException(string.Format("DataMode {0} & CrytoAlgorithm {1} not support!", dataMode.ToString(), CryptoAlgorithm.ToString())); } }
/// <summary> /// 根据密码算法创建一个 <see cref="ICryptoProvider"/> 实例。 /// </summary> /// <param name="algorithm">密码算法的类型。</param> /// <returns>一个 <see cref="ICryptoProvider"/> 对象。</returns> /// <exception cref="NotSupportedException">指定的 <paramref name="algorithm"/> 不支持。</exception> public static ICryptoProvider Create(CryptoAlgorithm algorithm) { return(Create(algorithm.ToString())); }