protected override void initCipher(byte[] iv, bool isEncrypt) { base.initCipher(iv, isEncrypt); IntPtr ctx = Marshal.AllocHGlobal(MbedTLS.cipher_get_size_ex()); if (isEncrypt) { _encryptCtx = ctx; } else { _decryptCtx = ctx; } byte[] realkey; if (_method == "rc4-md5") { byte[] temp = new byte[keyLen + ivLen]; realkey = new byte[keyLen]; Array.Copy(_key, 0, temp, 0, keyLen); Array.Copy(iv, 0, temp, keyLen, ivLen); realkey = MbedTLS.MD5(temp); } else { realkey = _key; } MbedTLS.cipher_init(ctx); if (MbedTLS.cipher_setup(ctx, MbedTLS.cipher_info_from_string(_innerLibName)) != 0) { throw new System.Exception("Cannot initialize mbed TLS cipher context"); } /* * MbedTLS takes key length by bit * cipher_setkey() will set the correct key schedule * and operation * * MBEDTLS_AES_{EN,DE}CRYPT * == MBEDTLS_BLOWFISH_{EN,DE}CRYPT * == MBEDTLS_CAMELLIA_{EN,DE}CRYPT * == MBEDTLS_{EN,DE}CRYPT * */ if (MbedTLS.cipher_setkey(ctx, realkey, keyLen * 8, isEncrypt ? MbedTLS.MBEDTLS_ENCRYPT : MbedTLS.MBEDTLS_DECRYPT) != 0) { throw new System.Exception("Cannot set mbed TLS cipher key"); } if (MbedTLS.cipher_set_iv(ctx, iv, ivLen) != 0) { throw new System.Exception("Cannot set mbed TLS cipher IV"); } if (MbedTLS.cipher_reset(ctx) != 0) { throw new System.Exception("Cannot finalize mbed TLS cipher context"); } }
protected override void initCipher(byte[] iv, bool isCipher) { base.initCipher(iv, isCipher); IntPtr cipherInfo = OpenSSL.GetCipherInfo(_innerLibName); if (cipherInfo == IntPtr.Zero) { throw new System.Exception("openssl: cipher not found"); } IntPtr ctx = OpenSSL.EVP_CIPHER_CTX_new(); if (ctx == IntPtr.Zero) { throw new System.Exception("fail to create ctx"); } if (isCipher) { _encryptCtx = ctx; } else { _decryptCtx = ctx; } byte[] realKey; if (_method.StartsWith(@"rc4-md5")) { byte[] temp = new byte[keyLen + ivLen]; Array.Copy(_key, 0, temp, 0, keyLen); Array.Copy(iv, 0, temp, keyLen, ivLen); realKey = MbedTLS.MD5(temp); } else { realKey = _key; } var ret = OpenSSL.EVP_CipherInit_ex(ctx, cipherInfo, IntPtr.Zero, null, null, isCipher ? OpenSSL.OPENSSL_ENCRYPT : OpenSSL.OPENSSL_DECRYPT); if (ret != 1) { throw new System.Exception("openssl: fail to set key length"); } ret = OpenSSL.EVP_CIPHER_CTX_set_key_length(ctx, keyLen); if (ret != 1) { throw new System.Exception("openssl: fail to set key length"); } ret = OpenSSL.EVP_CipherInit_ex(ctx, IntPtr.Zero, IntPtr.Zero, realKey, _method == "rc4-md5" ? null : iv, isCipher ? OpenSSL.OPENSSL_ENCRYPT : OpenSSL.OPENSSL_DECRYPT); if (ret != 1) { throw new System.Exception("openssl: cannot set key and iv"); } OpenSSL.EVP_CIPHER_CTX_set_padding(ctx, 0); }
public void TestMD5() { for (int len = 1; len < 64; len++) { System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] bytes = new byte[len]; _random.NextBytes(bytes); string md5str = Convert.ToBase64String(md5.ComputeHash(bytes)); string md5str2 = Convert.ToBase64String(MbedTLS.MD5(bytes)); Assert.IsTrue(md5str == md5str2); } }
public static void LegacyDeriveKey(byte[] password, byte[] key, int keylen) { byte[] result = new byte[password.Length + MD5_LEN]; int i = 0; byte[] md5sum = null; while (i < keylen) { if (i == 0) { md5sum = MbedTLS.MD5(password); } else { Array.Copy(md5sum, 0, result, 0, MD5_LEN); Array.Copy(password, 0, result, MD5_LEN, password.Length); md5sum = MbedTLS.MD5(result); } Array.Copy(md5sum, 0, key, i, Math.Min(MD5_LEN, keylen - i)); i += MD5_LEN; } }
public static void LegacyDeriveKey(byte[] password, byte[] key) { byte[] result = new byte[password.Length + 16]; int i = 0; byte[] md5sum = null; while (i < key.Length) { if (i == 0) { md5sum = MbedTLS.MD5(password); } else { md5sum.CopyTo(result, 0); password.CopyTo(result, md5sum.Length); md5sum = MbedTLS.MD5(result); } md5sum.CopyTo(key, i); i += md5sum.Length; } }
public static void bytesToKey(byte[] password, byte[] key) { var result = new byte[password.Length + 16]; var i = 0; byte[] md5Sum = null; while (i < key.Length) { if (i == 0) { md5Sum = MbedTLS.MD5(password); } else { Debug.Assert(md5Sum != null, $@"{nameof(md5Sum)} != null"); md5Sum.CopyTo(result, 0); password.CopyTo(result, md5Sum.Length); md5Sum = MbedTLS.MD5(result); } md5Sum.CopyTo(key, i); i += md5Sum.Length; } }
private static string GetHash(string content) { return(HttpServerUtility.UrlTokenEncode(MbedTLS.MD5(Encoding.ASCII.GetBytes(content)))); }
private static string GetHash(string content) { return(Uri.EscapeUriString(BitConverter.ToString(MbedTLS.MD5(Encoding.ASCII.GetBytes(content))).Replace("-", ""))); }