private static byte[] processKey(byte[] key, SymmetricAlgorithmType algorithmType) { int count; switch (algorithmType) { case SymmetricAlgorithmType.AES: count = 32; break; case SymmetricAlgorithmType.DES: case SymmetricAlgorithmType.RC2: count = 8; break; case SymmetricAlgorithmType.TripleDES: count = 24; break; case SymmetricAlgorithmType.Rijndael: count = key.Length >= 16 ? (key.Length >= 24 ? 32 : 24) : 16; break; case SymmetricAlgorithmType.XTea: case SymmetricAlgorithmType.RC6: count = 16; break; default: count = key.Length >= 8 ? (key.Length >= 16 ? (key.Length >= 24 ? (key.Length >= 32 ? (key.Length >= 40 ? (key.Length >= 64 ? (key.Length >= 128 ? (key.Length >= 192 ? (key.Length >= 256 ? (key.Length >= 448 ? 512 : 448) : 256) : 192) : 128) : 64) : 40) : 32) : 24) : 16) : 8; break; } return(key.Length == count ? key : Enumerable.Range(0, count).Select <int, byte>((Func <int, byte>)(i => i >= key.Length ? Convert.ToByte(i) : key[i])).ToArray <byte>()); }
public SymmetricKey(SymmetricAlgorithmType type, byte[] iv, byte[] key, CipherModePlus cipherMode, PaddingMode paddingMode, bool enableIVShuffle) { _type = type; switch (type) { case SymmetricAlgorithmType.None: _algo = null; return; case SymmetricAlgorithmType.Camellia: _algo = new openCrypto.CamelliaManaged (); break; case SymmetricAlgorithmType.Rijndael: _algo = new openCrypto.RijndaelManaged (); break; default: throw new ArgumentOutOfRangeException (); } _algo.ModePlus = cipherMode; _algo.Padding = paddingMode; _algo.KeySize = key.Length << 3; _algo.BlockSize = iv.Length << 3; _algo.FeedbackSize = iv.Length << 3; _iv = iv; _key = key; _ivShuffle = enableIVShuffle; }
private void InvokeSymmetricAlgorithm(SymmetricAlgorithmType symmetricAlgorithmType, Action <SymmetricAlgorithm> invoke) { using (var symmetricAlgorithm = GetSymmetricAlgorithm(symmetricAlgorithmType)) { invoke(symmetricAlgorithm); } }
protected object CreateAlgorithm(SymmetricAlgorithmType algorithmType) { switch (algorithmType) { case SymmetricAlgorithmType.AES: return((object)new AesManaged()); case SymmetricAlgorithmType.DES: return((object)new DESCryptoServiceProvider()); case SymmetricAlgorithmType.TripleDES: return((object)new TripleDESCryptoServiceProvider()); case SymmetricAlgorithmType.RC2: return((object)new RC2CryptoServiceProvider()); case SymmetricAlgorithmType.Rijndael: return((object)new RijndaelManaged()); case SymmetricAlgorithmType.BlowFish: return((object)new BlowFish()); case SymmetricAlgorithmType.XTea: return((object)new XTea()); default: throw new Exception(string.Empty); } }
private async Task InvokeSymmetricAlgorithmAsync(SymmetricAlgorithmType symmetricAlgorithmType, Func <SymmetricAlgorithm, Task> invokeAsync) { using (var symmetricAlgorithm = GetSymmetricAlgorithm(symmetricAlgorithmType)) { await invokeAsync(symmetricAlgorithm).ConfigureAwait(false); } }
public static byte[] GenerateIV(SymmetricAlgorithmType yourType) { byte[] outBytes; SymmetricAlgorithm key; switch (yourType) { case SymmetricAlgorithmType.DES: key = new DESCryptoServiceProvider(); break; case SymmetricAlgorithmType.TripleDES: key = new TripleDESCryptoServiceProvider(); break; case SymmetricAlgorithmType.AES: key = new AesCryptoServiceProvider(); break; default: throw new Exception("not support this encrypt type"); } outBytes = key.IV; key.Clear(); return(outBytes); }
public static SymmetricAlgorithm Create(SymmetricAlgorithmType algorithmType, string key, string iv) { CryptoAlgorithmAttribute attr = algorithmType.GetCustomeAttributes <CryptoAlgorithmAttribute>().FirstOrDefault(); Check.NotNull(attr); return(Create(attr.ConfigName, key, iv)); }
/// <summary> /// Cryptography Symmetric Algorithm /// </summary> /// <param name="NetSelected">SymmetricAlgorithmType</param> public CryptographySymmetricAlgorithm(SymmetricAlgorithmType NetSelected) { switch (NetSelected) { case SymmetricAlgorithmType.DES: { objCryptoService = new DESCryptoServiceProvider(); SymmetricAlgorithmKey = 1; break; } case SymmetricAlgorithmType.TripleDES: { objCryptoService = new TripleDESCryptoServiceProvider(); SymmetricAlgorithmKey = 2; break; } case SymmetricAlgorithmType.RC2: { objCryptoService = new RC2CryptoServiceProvider(); SymmetricAlgorithmKey = 3; break; } case SymmetricAlgorithmType.Rijndael: { objCryptoService = new RijndaelManaged(); SymmetricAlgorithmKey = 4; break; } } }
public SymmetricCryto(string key, SymmetricAlgorithmType symmetricAlgorithmType) { if (key != null) { _Key = key; } symmetricAlgorithmProvider = CreateSymmetricAlgorithmProvider(symmetricAlgorithmType); }
public CryptographicInfo(SymmetricAlgorithmType symmetricAlgorithmType, IEnumerable <byte> key, IEnumerable <byte> salt, IEnumerable <byte> initialVector, int iterations) { SymmetricAlgorithmType = symmetricAlgorithmType; Key = key; Salt = salt; InitialVector = initialVector; Iterations = iterations; }
public static SymmetricKey GenerateKey(SymmetricAlgorithmType symmetricAlgorithmType, SymmetricKeySize keySize = defaultKeySize, SymmetricBlockSize blockSize = defaultBlockSize) { var(symmetricAlgorithm, _) = GetAlgorithm(symmetricAlgorithmType); symmetricAlgorithm.KeySize = (int)keySize; symmetricAlgorithm.BlockSize = (int)blockSize; symmetricAlgorithm.GenerateKey(); symmetricAlgorithm.GenerateIV(); var symmetricKey = new SymmetricKey(symmetricAlgorithm.Key, symmetricAlgorithm.IV); return(symmetricKey); }
private static string InternalDecrypt(string ciphertext, SymmetricAlgorithmType algorithmType, byte[] key, byte[] iv, char?fillChar = null) { Check.NotNull(ciphertext); Check.NotEmpty(key); Check.NotEmpty(iv); using (SymmetricAlgorithm algorithm = Create(algorithmType)) { string ret = InternalDecrypt(ciphertext, algorithm, key, iv, fillChar); algorithm.Clear(); return(ret); } }
public byte[] GenerateKey(SymmetricAlgorithmType symmetricAlgorithmType) { var key = Array.Empty <byte>(); InvokeSymmetricAlgorithm(symmetricAlgorithmType, invoke: symmetricAlgorithm => { symmetricAlgorithm.GenerateKey(); key = symmetricAlgorithm.Key; }); return(key); }
public byte[] GenerateIv(SymmetricAlgorithmType symmetricAlgorithmType) { var iv = Array.Empty <byte>(); InvokeSymmetricAlgorithm(symmetricAlgorithmType, invoke: symmetricAlgorithm => { symmetricAlgorithm.GenerateIV(); iv = symmetricAlgorithm.IV; }); return(iv); }
public async Task EncryptAsync(Stream @in, Stream @out, ISymmetricKey symmetricKey, SymmetricAlgorithmType type) { if (@in == null) throw new ArgumentNullException(nameof(@in)); if (@out == null) throw new ArgumentNullException(nameof(@out)); if (symmetricKey == null) throw new ArgumentNullException(nameof(symmetricKey)); using (var rm = _symmetricAlgorithmFactory.Create(type)) using (var cs = new CryptoStream(@out, rm.CreateEncryptor(symmetricKey.Key, symmetricKey.IV), CryptoStreamMode.Write)) { await @in.CopyToAsync(cs); } }
public SymmetricAlgorithm Create(SymmetricAlgorithmType algorithmType) { switch (algorithmType) { case SymmetricAlgorithmType.Rijndael: return new RijndaelManaged(); case SymmetricAlgorithmType.AES: return new AesManaged(); default: throw new ArgumentOutOfRangeException(nameof(algorithmType), algorithmType, null); } }
public static async Task<byte[]> DecryptAsync([NotNull] this ISymmetricCryptoService service, byte[] s, [NotNull] ISymmetricKey symmetricKey, SymmetricAlgorithmType type) { if (service == null) throw new ArgumentNullException(nameof(service)); if (symmetricKey == null) throw new ArgumentNullException(nameof(symmetricKey)); using (var source = new MemoryStream(s)) using (var decrypted = new MemoryStream()) { await service.DecryptAsync(source, decrypted, symmetricKey, type); return decrypted.ToArray(); } }
public static AsymmetricAlgorithm Create(SymmetricAlgorithmType type) { switch (type) { case SymmetricAlgorithmType.AES: return(new DSACryptoServiceProvider()); case SymmetricAlgorithmType.Rijndael: return(new RSACryptoServiceProvider()); default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } }
public void TripleDesEncryptionDecryptionTest() { SymmetricAlgorithmType expectedAlgorithm = SymmetricAlgorithmType.TripleDes; // Create an encryptor with the default configuration. var encryptor = new Encryptor("MyC0013ncrypti0nP@$$w0rd!", "RDBST2345asertf") { EncryptionAlgorithm = expectedAlgorithm, CharacterEncoding = "utf-8" }; Assert.Equal(expectedAlgorithm, encryptor.EncryptionAlgorithm); RunEncryptionDecryptionTest(encryptor); }
public static IReturnsResult<ISymmetricCryptoService> MockDecryptAsync(this Mock<ISymmetricCryptoService> service, ISymmetricKey key, SymmetricAlgorithmType symmetricAlgorithmType, byte[] source, byte[] exp) { return service.Setup(x => x.DecryptAsync(It.IsAny<Stream>(), It.IsAny<Stream>(), key, symmetricAlgorithmType)) .Returns((Stream i, Stream o, ISymmetricKey sk, SymmetricAlgorithmType type) => { var msi = (MemoryStream) i; var mso = (MemoryStream) o; Assert.AreEqual(0, o.Length); mso.Write(exp, 0, exp.Length); CollectionAssert.AreEqual(source, msi.ToArray()); return Task.CompletedTask; }); }
public SymmetricKey(SymmetricAlgorithmPlus algo, byte[] iv, byte[] key, bool enableIVShuffle) { if (algo == null) _type = SymmetricAlgorithmType.None; else if (algo is openCrypto.CamelliaManaged) _type = SymmetricAlgorithmType.Camellia; else if (algo is openCrypto.RijndaelManaged) _type = SymmetricAlgorithmType.Rijndael; else throw new NotSupportedException (); _algo = algo; _iv = iv; _key = key; _ivShuffle = enableIVShuffle; }
public void Test1() { SymmetricAlgorithmType[] types = new SymmetricAlgorithmType[] { SymmetricAlgorithmType.None, SymmetricAlgorithmType.Camellia, SymmetricAlgorithmType.Rijndael }; byte[][][] key_iv_list = new byte[][][] { new byte[][] { null, null }, new byte[][] { RNG.GetRNGBytes (16), RNG.GetRNGBytes (16), RNG.GetRNGBytes (24), RNG.GetRNGBytes (16), RNG.GetRNGBytes (32), RNG.GetRNGBytes (16) }, new byte[][] { RNG.GetRNGBytes (16), RNG.GetRNGBytes (16), RNG.GetRNGBytes (24), RNG.GetRNGBytes (16), RNG.GetRNGBytes (32), RNG.GetRNGBytes (16) }, }; int[] data_length_list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 64, 128, 129, 130 }; bool[] shuffle_list = new bool[] {true, false}; for (int i = 0; i < types.Length; i ++) { byte[][] key_ivs = key_iv_list[i]; for (int k = 0; k < key_ivs.Length - 1; k += 2) { int[] ivShuffleSize = new int[data_length_list.Length]; foreach (bool enableShuffle in shuffle_list) { SymmetricKey key = new SymmetricKey (types[i], key_ivs[k + 1], key_ivs[k], CipherModePlus.CBC, System.Security.Cryptography.PaddingMode.ISO10126, enableShuffle); for (int idx = 0; idx < data_length_list.Length; idx ++) { byte[] data = RNG.GetRNGBytes (data_length_list[idx]); byte[] e1 = key.Encrypt (data, 0, data.Length); byte[] p1 = key.Decrypt (e1, 0, e1.Length); Assert.AreEqual (data, p1); if (key.IV != null) { if (enableShuffle) ivShuffleSize[idx] = e1.Length; else Assert.AreEqual (ivShuffleSize[idx] - key.IV.Length, e1.Length); } } } } } }
/// <summary> /// 对称加解密构造函数 /// </summary> /// <param name="key">密钥(至少8位)</param> /// <param name="keyVector">密钥向量(至少8位)</param> /// <param name="saType">对称加密类型</param> public SymmetricEncryption(string key, string keyVector, SymmetricAlgorithmType saType) { if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(keyVector)) { // ReSharper disable once NotResolvedInText throw new ArgumentNullException("密钥或者密钥向量不能为空!"); } if (key.Length < 8 || keyVector.Length < 8) { throw new ArgumentException("密钥或者密钥向量长度至少8位!"); } this.key = key; this.keyVector = keyVector; this.InitCryptoServiceProvider(saType); }
private static SymmetricAlgorithm GetSymmetricAlgorithm(SymmetricAlgorithmType type) { switch (type) { case SymmetricAlgorithmType.DES: return(DES.Create()); case SymmetricAlgorithmType.RC2: return(RC2.Create()); case SymmetricAlgorithmType.Rijindael: return(Rijndael.Create()); case SymmetricAlgorithmType.TripleDES: return(TripleDES.Create()); //case SymmetricAlgorithmType.RC4: return Org.Mentalis.SecurityServices.Cryptography.RC4.Create(); default: return(Rijndael.Create()); } }
private static (SymmetricAlgorithm, bool) GetAlgorithm(SymmetricAlgorithmType symmetricAlgorithmType) { return(symmetricAlgorithmType switch { SymmetricAlgorithmType.AES => (Aes.Create(), false), SymmetricAlgorithmType.DES => (DES.Create(), false), SymmetricAlgorithmType.TripleDES => (TripleDES.Create(), false), SymmetricAlgorithmType.RC2 => (RC2.Create(), false), SymmetricAlgorithmType.AESwithShift => (Aes.Create(), true), SymmetricAlgorithmType.DESwithShift => (DES.Create(), true), SymmetricAlgorithmType.TripleDESwithShift => (TripleDES.Create(), true), SymmetricAlgorithmType.RC2withShift => (RC2.Create(), true), _ => throw new NotImplementedException(), });
private static byte[] processVI(byte[] vi, SymmetricAlgorithmType algorithmType) { int count; switch (algorithmType) { case SymmetricAlgorithmType.AES: case SymmetricAlgorithmType.Rijndael: count = 16; break; default: count = 8; break; } return(vi.Length == count ? vi : Enumerable.Range(0, count).Select <int, byte>((Func <int, byte>)(i => i >= vi.Length ? Convert.ToByte(i) : vi[i])).ToArray <byte>()); }
public static async Task<byte[]> EncryptAsync([NotNull] this ISymmetricCryptoService service, string s, [NotNull] ISymmetricKey symmetricKey, SymmetricAlgorithmType type, Encoding encoding = null) { if (service == null) throw new ArgumentNullException(nameof(service)); if (symmetricKey == null) throw new ArgumentNullException(nameof(symmetricKey)); if (encoding == null) { encoding = Encoding.UTF8; } using (var source = new MemoryStream(encoding.GetBytes(s))) using (var encrypted = new MemoryStream()) { await service.EncryptAsync(source, encrypted, symmetricKey, type); return encrypted.ToArray(); } }
public async Task <string> DecryptBytes(SymmetricAlgorithmType symmetricAlgorithmType, byte[] bytes, byte[] key, byte[] iV) { if (bytes == null || bytes.Length <= 0) { throw new ArgumentNullException(nameof(bytes)); } if (key == null || key.Length <= 0) { throw new ArgumentNullException(nameof(key)); } if (iV == null || iV.Length <= 0) { throw new ArgumentNullException(nameof(key)); } string plaintext = null; // Create an AesCryptoServiceProvider object // with the specified key and IV. await InvokeSymmetricAlgorithmAsync(symmetricAlgorithmType, async (symmetricAlgorithm) => { symmetricAlgorithm.Key = key; symmetricAlgorithm.IV = iV; // Create a decryptor to perform the stream transform. using (var decryptor = symmetricAlgorithm.CreateDecryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV)) // Create the streams used for decryption. using (var msDecrypt = memoryStreamManager.GetStream(bytes, false)) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = await srDecrypt.ReadToEndAsync().ConfigureAwait(false); } } } }).ConfigureAwait(false); return(plaintext); }
public static SymmetricCrypto Create( byte[] vi, byte[] key, SymmetricAlgorithmType algorithmType) { SymmetricCrypto symmetricCrypto = new SymmetricCrypto(); symmetricCrypto.Algorithm = symmetricCrypto.CreateAlgorithm(algorithmType); if (symmetricCrypto.Algorithm is SymmetricAlgorithm) { (symmetricCrypto.Algorithm as SymmetricAlgorithm).IV = SymmetricCrypto.processVI(vi, algorithmType); (symmetricCrypto.Algorithm as SymmetricAlgorithm).Key = SymmetricCrypto.processKey(key, algorithmType); } else if (symmetricCrypto.Algorithm is CSymmetricAlgorithm) { (symmetricCrypto.Algorithm as CSymmetricAlgorithm).IV = SymmetricCrypto.processVI(vi, algorithmType); (symmetricCrypto.Algorithm as CSymmetricAlgorithm).Key = SymmetricCrypto.processKey(key, algorithmType); } return(symmetricCrypto); }
public async Task <byte[]> EncryptString(SymmetricAlgorithmType symmetricAlgorithmType, string plainText, byte[] key, byte[] iV) { if (plainText == null || plainText.Length <= 0) { throw new ArgumentNullException(nameof(plainText)); } if (key == null || key.Length == 0) { throw new ArgumentNullException(nameof(key)); } if (iV == null || iV.Length == 0) { throw new ArgumentNullException(nameof(iV)); } byte[] encrypted = Array.Empty <byte>(); await InvokeSymmetricAlgorithmAsync(symmetricAlgorithmType, async (symmetricAlgorithm) => { symmetricAlgorithm.Key = key; symmetricAlgorithm.IV = iV; // Create an encryptor to perform the stream transform. using (var encryptor = symmetricAlgorithm.CreateEncryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV)) using (var msEncrypt = memoryStreamManager.GetStream(false)) { using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) using (var swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. await swEncrypt.WriteAsync(plainText).ConfigureAwait(false); } encrypted = msEncrypt.ToArray(); } }).ConfigureAwait(false); return(encrypted); }
/// <summary> /// 构建对称算法对象,并设置属性 /// </summary> /// <param name="saType">对称算法类型</param> private void InitCryptoServiceProvider(SymmetricAlgorithmType saType) { sa = this.GetCryptoServiceProvider(saType); // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数) // 通过 密码 和 salt 派生密钥 byte[] keyVectorBytes = Encoding.UTF8.GetBytes(keyVector); var rfc = new Rfc2898DeriveBytes(key, keyVectorBytes); /* * AesManaged.BlockSize - 加密操作的块大小(单位:bit) * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit) * AesManaged.KeySize - 对称算法的密钥大小(单位:bit) * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit) * AesManaged.Key - 对称算法的密钥 * AesManaged.IV - 对称算法的密钥大小 * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥 */ sa.BlockSize = sa.LegalBlockSizes[0].MaxSize; sa.KeySize = sa.LegalKeySizes[0].MaxSize; sa.Key = rfc.GetBytes(sa.KeySize / 8); sa.IV = rfc.GetBytes(sa.BlockSize / 8); }
public static SymmetricAlgorithm Create(SymmetricAlgorithmType type) { switch (type) { case SymmetricAlgorithmType.AES: return(new AesCryptoServiceProvider()); case SymmetricAlgorithmType.Rijndael: return(new RijndaelManaged()); case SymmetricAlgorithmType.DES: return(new DESCryptoServiceProvider()); case SymmetricAlgorithmType.RC2: return(new RC2CryptoServiceProvider()); case SymmetricAlgorithmType.TripleDES: return(new TripleDESCryptoServiceProvider()); default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } }
/// <summary> /// 根据对称加密类型获取需要的对称加密对象实例 /// </summary> /// <param name="saType">对称加密类型</param> /// <returns></returns> private SymmetricAlgorithm GetCryptoServiceProvider(SymmetricAlgorithmType saType) { SymmetricAlgorithm tmpSa = null; switch (saType) { case SymmetricAlgorithmType.AES: tmpSa = new AesCryptoServiceProvider(); break; case SymmetricAlgorithmType.DES: tmpSa = new DESCryptoServiceProvider(); break; case SymmetricAlgorithmType.RC2: tmpSa = new RC2CryptoServiceProvider(); break; case SymmetricAlgorithmType.TripleDES: tmpSa = new TripleDESCryptoServiceProvider(); break; } return(tmpSa); }
public SymmetricCryto(SymmetricAlgorithmType symmetricAlgorithmType) { symmetricAlgorithmProvider = CreateSymmetricAlgorithmProvider(symmetricAlgorithmType); }
private static SymmetricAlgorithm GetSymmetricAlgorithm(SymmetricAlgorithmType type) { switch(type) { case SymmetricAlgorithmType.DES:return DES.Create(); case SymmetricAlgorithmType.RC2:return RC2.Create(); case SymmetricAlgorithmType.Rijindael:return Rijndael.Create(); case SymmetricAlgorithmType.TripleDES:return TripleDES.Create(); //case SymmetricAlgorithmType.RC4: return Org.Mentalis.SecurityServices.Cryptography.RC4.Create(); default: return Rijndael.Create(); } }
public Cryptographer(string key,string iv,SymmetricAlgorithmType symmetricAlgorithmType) { this.key = key; this.iv = iv; this.symmetricAlgorithmType = symmetricAlgorithmType; }
private static bool InternalCompareCrypto(string plaintext, string ciphertext, SymmetricAlgorithmType algorithmType, byte[] key, byte[] iv, char? fillChar = null) { if (plaintext == null || ciphertext == null) return false; string algtext = InternalEncrypt(plaintext, algorithmType, key, iv, fillChar); return algtext.Equals(ciphertext); }
public static bool CompareCrypto(string plaintext, string ciphertext, SymmetricAlgorithmType algorithmType, byte[] key, byte[] iv, char?fillChar = null) { return(InternalCompareCrypto(plaintext, ciphertext, algorithmType, key, iv, fillChar)); }
public static string SymmetricDecrypt(this string str, string key, string keyVector = null, SymmetricAlgorithmType saType = SymmetricAlgorithmType.AES, Encoding encoding = null) { var sa = new SymmetricEncryption(key, keyVector ?? key, saType); return(sa.Decrypt(str, encoding)); }
/// <summary> /// 创建一个对称加密算法提供者实例 /// </summary> /// <param name="symmetricAlgorithmType"></param> /// <returns></returns> SymmetricAlgorithm CreateSymmetricAlgorithmProvider(SymmetricAlgorithmType symmetricAlgorithmType) { SymmetricAlgorithm symmetricAlgorithm = null; switch (symmetricAlgorithmType) { case SymmetricAlgorithmType.DES: //<key:64,block:64,feedback:8>,key[64,64<skip:0>],block[64,64<skip:0>] ; IV[8byte],KEY[8byte] symmetricAlgorithm = new DESCryptoServiceProvider(); multiKey = 8; multiIV = 8; break; case SymmetricAlgorithmType.RC2_40: //<key:128,block:64,feedback:8>,key[40,128<skip:8>],block[64,64<skip:0>] ; IV[8byte],KEY[16byte] symmetricAlgorithm = new RC2CryptoServiceProvider(); multiKey = 5; multiIV = 8; break; case SymmetricAlgorithmType.RC2_64: //<key:128,block:64,feedback:8>,key[40,128<skip:8>],block[64,64<skip:0>] ; IV[8byte],KEY[16byte] symmetricAlgorithm = new RC2CryptoServiceProvider(); multiKey = 8; multiIV = 8; break; case SymmetricAlgorithmType.RC2_96: //<key:128,block:64,feedback:8>,key[40,128<skip:8>],block[64,64<skip:0>] ; IV[8byte],KEY[16byte] symmetricAlgorithm = new RC2CryptoServiceProvider(); multiKey = 12; multiIV = 8; break; case SymmetricAlgorithmType.RC2_128: //<key:128,block:64,feedback:8>,key[40,128<skip:8>],block[64,64<skip:0>] ; IV[8byte],KEY[16byte] symmetricAlgorithm = new RC2CryptoServiceProvider(); multiKey = 16; multiIV = 8; break; case SymmetricAlgorithmType.Rijndael_128: //<key:256,block:128,feedback:128>,key[128,256<skip:64>],block[128,256<skip:64>] ; IV[16byte],KEY[32byte] symmetricAlgorithm = new RijndaelManaged(); multiKey = 16; multiIV = 16; break; case SymmetricAlgorithmType.Rijndael_192: //<key:256,block:128,feedback:128>,key[128,256<skip:64>],block[128,256<skip:64>] ; IV[16byte],KEY[32byte] symmetricAlgorithm = new RijndaelManaged(); multiKey = 24; multiIV = 16; break; case SymmetricAlgorithmType.Rijndael_256: //<key:256,block:128,feedback:128>,key[128,256<skip:64>],block[128,256<skip:64>] ; IV[16byte],KEY[32byte] symmetricAlgorithm = new RijndaelManaged(); multiKey = 32; multiIV = 16; break; case SymmetricAlgorithmType.TripleDES_128: //<key:192,block:64,feedback:8>,key[128,192<skip:64>],block[64,64<skip:0>] ; IV[8byte],KEY[24byte] symmetricAlgorithm = new TripleDESCryptoServiceProvider(); multiKey = 16; multiIV = 8; break; case SymmetricAlgorithmType.TripleDES_192: //<key:192,block:64,feedback:8>,key[128,192<skip:64>],block[64,64<skip:0>] ; IV[8byte],KEY[24byte] symmetricAlgorithm = new TripleDESCryptoServiceProvider(); multiKey = 24; multiIV = 8; break; default: break; } symmetricAlgorithm.KeySize = factorKey * multiKey; symmetricAlgorithm.BlockSize = factorIV * multiIV; return symmetricAlgorithm; }
public SymmetricKey(SymmetricAlgorithmType type, byte[] iv, byte[] key) : this(type, iv, key, CipherModePlus.CBC, PaddingMode.ISO10126, true) { }
public static bool CompareCrypto(string plaintext, string ciphertext, SymmetricAlgorithmType algorithmType, string key, string iv, char? fillChar = null) { return InternalCompareCrypto(plaintext, ciphertext, algorithmType, StringToBytes(key), StringToBytes(iv), fillChar); }
public static bool CompareCrypto(string plaintext, string ciphertext, SymmetricAlgorithmType algorithmType, byte[] key, byte[] iv, char? fillChar = null) { return InternalCompareCrypto(plaintext, ciphertext, algorithmType, key, iv, fillChar); }
public static IJasilySymmetricKeyAlgorithmProvider CreateSymmetric(SymmetricAlgorithmType algorithmType) { throw new NotSupportedException(); }
public static string Encrypt(SymmetricAlgorithmType algorithmType, string plaintext, byte[] key, byte[] iv, char? fillChar = null) { return InternalEncrypt(plaintext, algorithmType, key, iv, fillChar); }
public SymmetricCryto(string key, string iv, SymmetricAlgorithmType symmetricAlgorithmType) { if (key != null) _Key = key; if (iv != null) _IV = iv; symmetricAlgorithmProvider = CreateSymmetricAlgorithmProvider(symmetricAlgorithmType); }
public static bool CompareCrypto(string plaintext, string ciphertext, SymmetricAlgorithmType algorithmType, string key, string iv, char?fillChar = null) { return(InternalCompareCrypto(plaintext, ciphertext, algorithmType, StringToBytes(key), StringToBytes(iv), fillChar)); }
public static SymmetricAlgorithm Create(SymmetricAlgorithmType algorithmType, string key, string iv) { CryptoAlgorithmAttribute attr = algorithmType.GetCustomeAttributes<CryptoAlgorithmAttribute>().FirstOrDefault(); Check.NotNull(attr); return Create(attr.ConfigName, key, iv); }
public Cryptographer(string key, string iv, SymmetricAlgorithmType symmetricAlgorithmType) { this.key = key; this.iv = iv; this.symmetricAlgorithmType = symmetricAlgorithmType; }
public static string Encrypt(SymmetricAlgorithmType algorithmType, string plaintext, string key, string iv, char? fillChar = null) { return InternalEncrypt(plaintext, algorithmType, StringToBytes(key), StringToBytes(iv), fillChar); }
public static byte[] SymmetricDecrypt(byte[] CypherText, byte[] yourKey, byte[] yourIV, CipherMode yourCipherMode, PaddingMode yourPaddingMode, SymmetricAlgorithmType yourType) { byte[] outBytes; SymmetricAlgorithm key; switch (yourType) { case SymmetricAlgorithmType.DES: key = new DESCryptoServiceProvider(); break; case SymmetricAlgorithmType.TripleDES: key = new TripleDESCryptoServiceProvider(); break; case SymmetricAlgorithmType.AES: key = new AesCryptoServiceProvider(); break; default: throw new Exception("not support this encrypt type"); } key.Key = yourKey; key.Mode = yourCipherMode; key.Padding = yourPaddingMode; if (key.Mode != CipherMode.ECB) { key.IV = yourIV; } outBytes = SymmetricDecrypt(CypherText, key); key.Clear(); return(outBytes); }
public static string Decrypt(SymmetricAlgorithmType algorithmType, string cipherText, byte[] key, byte[] iv, char? fillChar = null) { return InternalDecrypt(cipherText, algorithmType, key, iv, fillChar); }
public static IJasilySymmetricKeyAlgorithmProvider CreateSymmetric(SymmetricAlgorithmType algorithmType) => new UAPSymmetricKeyAlgorithmProvider( SymmetricKeyAlgorithmProvider.OpenAlgorithm( ConvertToSymmetricKeyAlgorithmName(algorithmType)));
public static string Decrypt(SymmetricAlgorithmType algorithmType, string cipherText, string key, string iv, char? fillChar = null) { return InternalDecrypt(cipherText, algorithmType, StringToBytes(key), StringToBytes(iv), fillChar); }
private static string ConvertToSymmetricKeyAlgorithmName(SymmetricAlgorithmType algorithmType) { switch (algorithmType) { case SymmetricAlgorithmType.AesCbc: return SymmetricAlgorithmNames.AesCbc; case SymmetricAlgorithmType.AesCbcPkcs7: return SymmetricAlgorithmNames.AesCbcPkcs7; case SymmetricAlgorithmType.AesCcm: return SymmetricAlgorithmNames.AesCcm; case SymmetricAlgorithmType.AesEcb: return SymmetricAlgorithmNames.AesEcb; case SymmetricAlgorithmType.AesEcbPkcs7: return SymmetricAlgorithmNames.AesEcbPkcs7; case SymmetricAlgorithmType.AesGcm: return SymmetricAlgorithmNames.AesGcm; case SymmetricAlgorithmType.DesCbc: return SymmetricAlgorithmNames.DesCbc; case SymmetricAlgorithmType.DesCbcPkcs7: return SymmetricAlgorithmNames.DesCbcPkcs7; case SymmetricAlgorithmType.DesEcb: return SymmetricAlgorithmNames.DesEcb; case SymmetricAlgorithmType.DesEcbPkcs7: return SymmetricAlgorithmNames.DesEcbPkcs7; case SymmetricAlgorithmType.Rc2Cbc: return SymmetricAlgorithmNames.Rc2Cbc; case SymmetricAlgorithmType.Rc2CbcPkcs7: return SymmetricAlgorithmNames.Rc2CbcPkcs7; case SymmetricAlgorithmType.Rc2Ecb: return SymmetricAlgorithmNames.Rc2Ecb; case SymmetricAlgorithmType.Rc2EcbPkcs7: return SymmetricAlgorithmNames.Rc2EcbPkcs7; case SymmetricAlgorithmType.Rc4: return SymmetricAlgorithmNames.Rc4; case SymmetricAlgorithmType.TripleDesCbc: return SymmetricAlgorithmNames.TripleDesCbc; case SymmetricAlgorithmType.TripleDesCbcPkcs7: return SymmetricAlgorithmNames.TripleDesCbcPkcs7; case SymmetricAlgorithmType.TripleDesEcb: return SymmetricAlgorithmNames.TripleDesEcb; case SymmetricAlgorithmType.TripleDesEcbPkcs7: return SymmetricAlgorithmNames.TripleDesEcbPkcs7; default: throw new NotSupportedException(); } }
private static string InternalDecrypt(string ciphertext, SymmetricAlgorithmType algorithmType, byte[] key, byte[] iv, char? fillChar = null) { Check.NotNull(ciphertext); Check.NotEmpty(key); Check.NotEmpty(iv); using (SymmetricAlgorithm algorithm = Create(algorithmType)) { string ret = InternalDecrypt(ciphertext, algorithm, key, iv, fillChar); algorithm.Clear(); return ret; } }