private void Rc4Md5_UpdateKey() { Contract.Assert(Key != null); Contract.Assert(IV != null); byte[] temp = new byte[KeySize + IVSize]; Array.Copy(Key, 0, temp, 0, KeySize); Array.Copy(IV, 0, temp, KeySize, IVSize); MbedTLS.MD5(temp, Key); }
public void InitCipher(byte[] key, byte[] iv, bool isCipher) { IntPtr ctx = Marshal.AllocHGlobal(MbedTLS.cipher_get_size_ex()); if (isCipher) { _encryptIV = iv; _encryptCtx = ctx; } else { _decryptIV = iv; _decryptCtx = ctx; } byte[] realkey = key; if (Method == "rc4-md5") { byte[] temp = new byte[KeySize + IVSize]; //realkey = new byte[KeySize]; Array.Copy(key, 0, temp, 0, KeySize); Array.Copy(iv, 0, temp, KeySize, IVSize); realkey = MbedTLS.MD5(temp); } Key = realkey; MbedTLS.cipher_init(ctx); if (MbedTLS.cipher_setup(ctx, MbedTLS.cipher_info_from_string(FormalName)) != 0) { throw new EncryptorException("Cannot initialize mbed TLS cipher context."); } if (MbedTLS.cipher_setkey(ctx, realkey, KeySize * 8, isCipher ? MbedTLS.MbedTLS_Encrypt : MbedTLS.MbedTLS_Decrypt) != 0) { throw new EncryptorException("Cannot set mbed TLS cipher key."); } if (MbedTLS.cipher_set_iv(ctx, iv, IVSize) != 0) { throw new EncryptorException("Cannot set mbed TLS cipher IV."); } if (MbedTLS.cipher_reset(ctx) != 0) { throw new EncryptorException("Cannot finalize mbed TLS cipher context."); } }
public static void BytesToKey(byte[] bytes, byte[] key) { byte[] result = new byte[bytes.Length + 16]; int i = 0; byte[] md5sum = null; while (i < key.Length) { if (i == 0) { md5sum = MbedTLS.MD5(bytes); } else { md5sum.CopyTo(result, 0); bytes.CopyTo(result, md5sum.Length); md5sum = MbedTLS.MD5(result); } md5sum.CopyTo(key, i); i += md5sum.Length; } }