private void InitCipher(ref IntPtr ctx, byte[] iv, bool isCipher) { ctx = Marshal.AllocHGlobal(_cipherInfo[3]); 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 = MD5.Create().ComputeHash(temp); } else { realkey = _key; } if (_cipher == CIPHER_AES) { PolarSSL.aes_init(ctx); // PolarSSL takes key length by bit // since we'll use CFB mode, here we both do enc, not dec PolarSSL.aes_setkey_enc(ctx, realkey, keyLen * 8); if (isCipher) { _encryptIV = new byte[ivLen]; Array.Copy(iv, _encryptIV, ivLen); } else { _decryptIV = new byte[ivLen]; Array.Copy(iv, _decryptIV, ivLen); } } else if (_cipher == CIPHER_BF) { PolarSSL.blowfish_init(ctx); // PolarSSL takes key length by bit PolarSSL.blowfish_setkey(ctx, realkey, keyLen * 8); if (isCipher) { _encryptIV = new byte[ivLen]; Array.Copy(iv, _encryptIV, ivLen); } else { _decryptIV = new byte[ivLen]; Array.Copy(iv, _decryptIV, ivLen); } } else if (_cipher == CIPHER_RC4) { PolarSSL.arc4_init(ctx); // PolarSSL RC4 takes key length by byte PolarSSL.arc4_setup(ctx, realkey, keyLen); } }
protected override void initCipher(byte[] iv, bool isCipher) { base.initCipher(iv, isCipher); IntPtr ctx; ctx = Marshal.AllocHGlobal(_cipherInfo[3]); if (isCipher) { _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 = MD5.Create().ComputeHash(temp); } else { realkey = _key; } if (_cipher == CIPHER_AES) { PolarSSL.aes_init(ctx); // PolarSSL takes key length by bit // since we'll use CFB mode, here we both do enc, not dec PolarSSL.aes_setkey_enc(ctx, realkey, keyLen * 8); } else if (_cipher == CIPHER_RC4) { PolarSSL.arc4_init(ctx); // PolarSSL RC4 takes key length by byte PolarSSL.arc4_setup(ctx, realkey, keyLen); } }