public byte[] Decrypt(byte[] value) { BufferedBlockCipher aes = new BufferedBlockCipher(new OfbBlockCipher(new AesEngine(), AesBlockSizeInBytes * 8)); ArraySegment <byte> iv = new ArraySegment <byte>(value, 0, AesBlockSizeInBytes); ArraySegment <byte> secret = new ArraySegment <byte>(value, AesBlockSizeInBytes, value.Length - AesBlockSizeInBytes); ParametersWithIV ivAndKey = new ParametersWithIV(new KeyParameter(aesKey), iv.ToArray()); aes.Init(false, ivAndKey); int maximumSize = aes.GetOutputSize(secret.Count); byte[] outputBuffer = new byte[maximumSize]; int length1 = aes.ProcessBytes(secret.ToArray(), 0, secret.Count, outputBuffer, 0); int length2 = aes.DoFinal(outputBuffer, length1); return(new ArraySegment <byte>(outputBuffer, 0, length1 + length2).ToArray()); }
/// <summary> /// decrypt data using rijdael /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public byte[] Decrypt(byte[] data, byte[] key, byte[] iv) { this.Check(key, iv); //Set up //AesEngine engine = new AesEngine(); RijndaelEngine engine = new RijndaelEngine(256); CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding()); //Default scheme is PKCS5/PKCS7 KeyParameter keyParam = new KeyParameter(key.SubByte(0, 32)); ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 32); cipher.Init(false, keyParamWithIV); byte[] outputBytes = new byte[cipher.GetOutputSize(data.Length)]; int length = cipher.ProcessBytes(data, outputBytes, 0); cipher.DoFinal(outputBytes, length); //Do the final block return(outputBytes); }
/// <summary> /// Decloak the given buffer and return the valid Packet from it /// </summary> /// <param name="buffer">A cloaked packet buffer.</param> static public Packet Decloak(byte[] buffer) { if (buffer.Length < 8 || buffer [0] == 0) { return(Packet.DecodePacket(buffer)); } byte[] nonce = buffer.Take(8).ToArray(); var parms = new ParametersWithIV(new KeyParameter(cloakKey), nonce); var chacha = new ChaChaEngine(20); chacha.Init(false, parms); byte[] outBuff = new byte[buffer.Length - 8]; chacha.ProcessBytes(buffer, 8, buffer.Length - 8, outBuff, 0); return(Decloak(outBuff)); }
public static byte[] DecryptAES(byte[] data, byte[] key) { AesEngine engine = new AesEngine(); CbcBlockCipher blockCipher = new CbcBlockCipher(engine); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); KeyParameter keyParam = new KeyParameter(key); ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, key, 0, key.Length); cipher.Init(false, keyParamWithIV); byte[] comparisonBytes = new byte[cipher.GetOutputSize(data.Length)]; int length1 = cipher.ProcessBytes(data, comparisonBytes, 0); int length2 = cipher.DoFinal(comparisonBytes, length1); //Do the final block int actualLength = length1 + length2; byte[] result = new byte[actualLength]; System.Buffer.BlockCopy(comparisonBytes, 0, result, 0, result.Length); return(result); }
public void Init( bool forEncryption, //ignored by this CTR mode ICipherParameters parameters) { if (parameters is ParametersWithIV) { ParametersWithIV ivParam = (ParametersWithIV)parameters; byte[] iv = ivParam.GetIV(); Array.Copy(iv, 0, IV, 0, IV.Length); Reset(); cipher.Init(true, ivParam.Parameters); } else { throw new ArgumentException("SIC mode requires ParametersWithIV", "parameters"); } }
public void Init(bool forEncryption, ICipherParameters parameters) { encrypting = forEncryption; if (parameters is ParametersWithIV) { ParametersWithIV parametersWithIV = (ParametersWithIV)parameters; byte[] iV = parametersWithIV.GetIV(); int num = IV.Length - iV.Length; Array.Copy(iV, 0, IV, num, iV.Length); Array.Clear(IV, 0, num); parameters = parametersWithIV.Parameters; } Reset(); if (parameters != null) { cipher.Init(forEncryption: true, parameters); } }
public static byte[] Encrypt(string text, byte[] key) { byte[] iv = new byte[16]; AesEngine engine = new AesEngine(); CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); //Default scheme is PKCS5/PKCS7 KeyParameter keyParam = new KeyParameter(key); ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16); var inputBytes = Encoding.UTF8.GetBytes(text); cipher.Init(true, keyParamWithIV); byte[] outputBytes = new byte[cipher.GetOutputSize(inputBytes.Length)]; int length = cipher.ProcessBytes(inputBytes, outputBytes, 0); cipher.DoFinal(outputBytes, length); //Do the final block return(outputBytes); }
public virtual void Init(bool forWrapping, ICipherParameters param) { this.forWrapping = forWrapping; if (param is ParametersWithRandom) { ParametersWithRandom random = (ParametersWithRandom)param; this.rand = random.Random; this.param = (ParametersWithIV)random.Parameters; } else { if (forWrapping) { this.rand = new SecureRandom(); } this.param = (ParametersWithIV)param; } }
private void reinitBug() { KeyParameter key = new KeyParameter(Hex.Decode("80000000000000000000000000000000")); ParametersWithIV parameters = new ParametersWithIV(key, Hex.Decode("0000000000000000")); IStreamCipher salsa = new Salsa20Engine(); salsa.Init(true, parameters); try { salsa.Init(true, key); Fail("Salsa20 should throw exception if no IV in Init"); } catch (ArgumentException) { } }
public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom random) { byte[] key = contentEncryptionKey.GetKey(); string rfc3211WrapperName = Helper.GetRfc3211WrapperName(keyEncryptionKeyOID); IWrapper wrapper = Helper.CreateWrapper(rfc3211WrapperName); int num = (Platform.StartsWith(rfc3211WrapperName, "DESEDE") ? 8 : 16); byte[] array = new byte[num]; ((Random)random).NextBytes(array); ICipherParameters parameters = new ParametersWithIV(keyEncryptionKey, array); wrapper.Init(forWrapping: true, new ParametersWithRandom(parameters, random)); Asn1OctetString encryptedKey = new DerOctetString(wrapper.Wrap(key, 0, key.Length)); DerSequence parameters2 = new DerSequence(new DerObjectIdentifier(keyEncryptionKeyOID), new DerOctetString(array)); AlgorithmIdentifier keyEncryptionAlgorithm = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgPwriKek, parameters2); return(new RecipientInfo(new PasswordRecipientInfo(keyDerivationAlgorithm, keyEncryptionAlgorithm, encryptedKey))); }
public void Init(bool forEncryption, ICipherParameters parameters) { this.encrypting = forEncryption; if (parameters is ParametersWithIV) { ParametersWithIV hiv = (ParametersWithIV)parameters; byte[] iV = hiv.GetIV(); int destinationIndex = this.IV.Length - iV.Length; Array.Copy(iV, 0, this.IV, destinationIndex, iV.Length); Array.Clear(this.IV, 0, destinationIndex); parameters = hiv.Parameters; } this.Reset(); if (parameters != null) { this.cipher.Init(true, parameters); } }
/// <summary> /// Encryption using 3DES algorithm in CBC mode /// </summary> /// <param name="message">Input message bytes</param> /// <param name="isEncryption">true for encrypting, false for decrypting</param> /// <param name="iv">initialization vector</param> /// <param name="key">key for encryption</param> /// <returns>Encrypted message bytes</returns> public static byte[] Encrypt_Decrypt3DES_CBC(byte[] message, byte[] key, byte[] iv, bool isEncryption) { Debug.WriteLine("msg=" + Convert.ToBase64String(message)); DesEdeEngine desedeEngine = new DesEdeEngine(); BufferedBlockCipher bufferedCipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(desedeEngine), new Pkcs7Padding()); // 192 bita ključ = 24 bajta KeyParameter keyparam = ParameterUtilities.CreateKeyParameter("DESEDE", key); ParametersWithIV keyWithIV = new ParametersWithIV(keyparam, iv); byte[] output = new byte[bufferedCipher.GetOutputSize(message.Length)]; bufferedCipher.Init(isEncryption, keyWithIV); output = bufferedCipher.DoFinal(message); Debug.WriteLine("msgENCRY=" + Convert.ToBase64String(output)); return(output); }
/** * Initialise the cipher and, possibly, the initialisation vector (IV). * If an IV isn't passed as part of the parameter, the IV will be all zeros. * An IV which is too short is handled in FIPS compliant fashion. * * @param forEncryption if true the cipher is initialised for * encryption, if false for decryption. * @param param the key and other data required by the cipher. * @exception ArgumentException if the parameters argument is * inappropriate. */ public void Init( bool forEncryption, ICipherParameters parameters) { this.encrypting = forEncryption; if (parameters is ParametersWithIV) { ParametersWithIV ivParam = (ParametersWithIV)parameters; byte[] iv = ivParam.GetIV(); int diff = IV.Length - iv.Length; Array.Copy(iv, 0, IV, diff, iv.Length); Array.Clear(IV, 0, diff); parameters = ivParam.Parameters; } Reset(); cipher.Init(true, parameters); }
public virtual void Init( bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; ICipherParameters cipherParameters; if (parameters is AeadParameters) { AeadParameters param = (AeadParameters)parameters; nonce = param.GetNonce(); initialAssociatedText = param.GetAssociatedText(); macSize = param.MacSize / 8; cipherParameters = param.Key; } else if (parameters is ParametersWithIV) { ParametersWithIV param = (ParametersWithIV)parameters; nonce = param.GetIV(); initialAssociatedText = null; macSize = macBlock.Length / 2; cipherParameters = param.Parameters; } else { throw new ArgumentException("invalid parameters passed to CCM"); } // NOTE: Very basic support for key re-use, but no performance gain from it if (cipherParameters != null) { keyParam = cipherParameters; } if (nonce == null || nonce.Length < 7 || nonce.Length > 13) { throw new ArgumentException("nonce must have length from 7 to 13 octets"); } Reset(); }
// https://tools.ietf.org/html/rfc4357#section-6.5 protected override byte[] KEKDiversification(byte[] kek, byte[] ukm) { var cipher = CipherUtilities.GetCipher("GOST/CFB/NOPADDING"); var result = new byte[32]; Array.Copy(kek, result, 32); var S = new byte[8]; for (int i = 0; i < 8; ++i) { int sum1 = 0; int sum2 = 0; for (int j = 0, mask = 1; j < 8; ++j, mask <<= 1) { var kj = (result[4 * j]) | (result[4 * j + 1] << 8) | (result[4 * j + 2] << 16) | (result[4 * j + 3] << 24); if ((mask & ukm[i]) != 0) { sum1 += kj; } else { sum2 += kj; } } S[0] = (byte)(sum1 & 0xff); S[1] = (byte)((sum1 >> 8) & 0xff); S[2] = (byte)((sum1 >> 16) & 0xff); S[3] = (byte)((sum1 >> 24) & 0xff); S[4] = (byte)(sum2 & 0xff); S[5] = (byte)((sum2 >> 8) & 0xff); S[6] = (byte)((sum2 >> 16) & 0xff); S[7] = (byte)((sum2 >> 24) & 0xff); var key = ParameterUtilities.CreateKeyParameter("GOST", result); var sbox = new ParametersWithSBox(key, Gost28147Engine.GetSBox("E-A")); var prms = new ParametersWithIV(sbox, S); cipher.Init(true, prms); result = cipher.ProcessBytes(result); } return(result); }
private static byte[] DoCryptStuff(byte[] data, IKey key, CipherDirection direction, CipherMode cipherMode, byte[] iv) { byte[] result; String transformation = key.GetAlgorithm(); if (key.GetAlgorithm().StartsWith(ALG_DES)) { transformation += "/" + ModetoString(cipherMode) + "/" + DES_NO_PADDING; } ICipherParameters keyparam = new KeyParameter(key.GetEncoded()); IBufferedCipher cipher = CipherUtilities.GetCipher(transformation); if (cipherMode != CipherMode.ECB) { keyparam = new ParametersWithIV(keyparam, iv); } byte[] output = new byte[cipher.GetOutputSize(data.Length)]; cipher.Init(direction == CipherDirection.ENCRYPT_MODE ? true : false, keyparam); result = cipher.DoFinal(data); if (cipherMode != CipherMode.ECB) { Array.Copy(result, result.Length - 8, iv, 0, iv.Length); } //AlgorithmParameterSpec aps = null; //try //{ // Cipher c1 = Cipher.getInstance(transformation, provider.getName()); // if (cipherMode != CipherMode.ECB) // aps = new IvParameterSpec(iv); // c1.init(direction, key, aps); // result = c1.doFinal(data); // if (cipherMode != CipherMode.ECB) // System.arraycopy(result, result.length - 8, iv, 0, iv.length); //} //catch (Exception e) //{ // throw e; //} return(result); }
/// <summary> /// Decryption & Authentication (AES-GCM) of a UTF8 Message /// </summary> /// <param name="encryptedMessage">The encrypted message.</param> /// <param name="key">The key.</param> /// <param name="nonSecretPayloadLength">Length of the optional non-secret payload.</param> /// <returns>Decrypted Message</returns> private byte[] AESGCMDecrypt(byte[] encryptedMessage, byte[] key) { // User Error Checks if (key == null || key.Length != KeyBitSize / 8) { throw new ArgumentException(string.Format("Key needs to be {0} bit!", KeyBitSize), "key"); } if (encryptedMessage == null || encryptedMessage.Length == 0) { throw new ArgumentException("Encrypted Message Required!", "encryptedMessage"); } using (var cipherStream = new MemoryStream(encryptedMessage)) { using (var cipherReader = new BinaryReader(cipherStream)) { // Grab Nonce var iv = cipherReader.ReadBytes(NonceBitSize / 8); var cipher = new GcmBlockCipher(new AesEngine()); var keyParam = new KeyParameter(key); ICipherParameters parameters = new ParametersWithIV(keyParam, iv); cipher.Init(false, parameters); // Decrypt Cipher Text var cipherText = cipherReader.ReadBytes(encryptedMessage.Length - iv.Length); var plainText = new byte[cipher.GetOutputSize(cipherText.Length)]; try { var len = cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0); cipher.DoFinal(plainText, len); } catch (InvalidCipherTextException) { return(null); } return(plainText); } } }
public void Init(int cipherMode, IKey key, AlgorithmParameterSpec aps) { ICipherParameters cp; if (aps is RC2ParameterSpec) { cp = new RC2Parameters(key.GetEncoded(), (aps as RC2ParameterSpec).GetEffectiveKeyBits()); } else if (aps is IvParameterSpec) { cp = new KeyParameter(key.GetEncoded()); cp = new ParametersWithIV(cp, (aps as IvParameterSpec).GetIV()); } else { throw new NotImplementedException(); } cipherImpl.Init(cipherMode == ENCRYPT_MODE, cp); }
private static ICipherParameters FixDesParity(string mechanism, ICipherParameters parameters) { if (!BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "DES-CBC") && !BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "DESEDE-CBC")) { return(parameters); } if (parameters is ParametersWithIV) { ParametersWithIV ivParams = (ParametersWithIV)parameters; return(new ParametersWithIV(FixDesParity(mechanism, ivParams.Parameters), ivParams.GetIV())); } KeyParameter kParam = (KeyParameter)parameters; byte[] keyBytes = kParam.GetKey(); DesParameters.SetOddParity(keyBytes); return(new KeyParameter(keyBytes)); }
public void Init(bool forEncryption, ICipherParameters parameters) { if (parameters is ParametersWithIV) { ParametersWithIV hiv = (ParametersWithIV)parameters; byte[] iV = hiv.GetIV(); if (iV.Length < this.IV.Length) { Array.Copy(iV, 0, this.IV, this.IV.Length - iV.Length, iV.Length); } else { Array.Copy(iV, 0, this.IV, 0, this.IV.Length); } parameters = hiv.Parameters; } this.Reset(); this.cipher.Init(true, parameters); }
// Encrypt data using Chacha engine public byte[] encryptWithChacha(byte[] input, byte[] key) { // Create a buffer that will contain the encrypted output and an 8 byte nonce byte[] outData = new byte[input.Length + 8]; // Generate the 8 byte nonce byte[] nonce = new byte[8]; using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider()) { // Fill the array with a random value. rngCsp.GetBytes(nonce); } // Prevent leading 0 to avoid edge cases if (nonce[0] == 0) { nonce[0] = 1; } // Generate the Chacha engine var parms = new ParametersWithIV(new KeyParameter(key), nonce); var chacha = new ChaChaEngine(chacha_rounds); try { chacha.Init(true, parms); } catch (Exception e) { Logging.error(string.Format("Error in chacha encryption. {0}", e.ToString())); return(null); } // Encrypt the input data while maintaing an 8 byte offset at the start chacha.ProcessBytes(input, 0, input.Length, outData, 8); // Copy the 8 byte nonce to the start of outData buffer Buffer.BlockCopy(nonce, 0, outData, 0, 8); // Return the encrypted data buffer return(outData); }
private void CheckMac(byte[] cekDecrypted, byte[] kek) { var cekmac = new byte[4]; var mac = new Gost28147MacIV(); var key = ParameterUtilities.CreateKeyParameter("GOST", kek); var prms = new ParametersWithIV(key, UKM); mac.Init(prms); mac.BlockUpdate(cekDecrypted, 0, cekDecrypted.Length); mac.DoFinal(cekmac, 0); for (int i = 0; i < 4; ++i) { if (cekmac[i] != MAC[i]) { throw new CryptographicException("CEK decryption error, MAC values do not match."); } } }
public void Init(bool forEncryption, ICipherParameters parameters) { if (parameters is ParametersWithIV) { ParametersWithIV parametersWithIV = (ParametersWithIV)parameters; byte[] iV = parametersWithIV.GetIV(); if (iV.Length < IV.Length) { Array.Copy(iV, 0, IV, IV.Length - iV.Length, iV.Length); } else { Array.Copy(iV, 0, IV, 0, IV.Length); } parameters = parametersWithIV.Parameters; } Reset(); cipher.Init(forEncryption: true, parameters); }
private static ICipherParameters FixDesParity(string mechanism, ICipherParameters parameters) { if (!mechanism.EndsWith("DES-CBC") & !mechanism.EndsWith("DESEDE-CBC")) { return(parameters); } if (parameters is ParametersWithIV) { ParametersWithIV ivParams = (ParametersWithIV)parameters; return(new ParametersWithIV(FixDesParity(mechanism, ivParams.Parameters), ivParams.GetIV())); } KeyParameter kParam = (KeyParameter)parameters; byte[] keyBytes = kParam.GetKey(); DesParameters.SetOddParity(keyBytes); return(new KeyParameter(keyBytes)); }
public void EnableEncryption(byte[] key, byte[] iv) { var keyParam = new KeyParameter(key); var param = new ParametersWithIV(keyParam, iv); var cipherEncrypt = CipherUtilities.GetCipher("AES/CFB8/NoPadding"); var cipherDecrypt = CipherUtilities.GetCipher("AES/CFB8/NoPadding"); cipherEncrypt.Init(true, param); cipherDecrypt.Init(false, param); _encryptor = new CipherStream(_stream, cipherEncrypt, cipherDecrypt); //_encryptor = new CryptoStream(_stream, Aes.Create().CreateEncryptor(param, iv), CryptoStreamMode.Write, true); //_decryptor = new CryptoStream(_stream, Aes.Create().CreateDecryptor(param, iv), CryptoStreamMode.Read, true); Encryption = true; }
/// <summary> /// Method for encrypting plain text /// </summary> /// <param name="selectedAlgorithim">the algorithim to use, will be cast to enum</param> /// <param name="keySize">the key size to use</param> /// <param name="secretKey">the secret key for the algorithim</param> /// <param name="plainText">the plain text to encrypt</param> /// <returns>the encrypted bytes</returns> public byte[] EncryptText(int selectedAlgorithim, int keySize, byte[] secretKey, byte[] iv, string plainText) { var algorithim = ((SymmetricBouncyCastleCipher)selectedAlgorithim).ToString().Replace("_", "-");; bufferedCipher = CipherUtilities.GetCipher(algorithim); var plain = ByteConvert.StringToUTF8Bytes(plainText); if (GetIvSize(selectedAlgorithim) > 0) { var kp = new KeyParameter(secretKey); var ivp = new ParametersWithIV(kp, iv); bufferedCipher.Init(true, ivp); } else { bufferedCipher.Init(true, new KeyParameter(secretKey)); } return(bufferedCipher.DoFinal(plain)); }
public virtual void Init(bool forWrapping, ICipherParameters parameters) { //IL_009c: Unknown result type (might be due to invalid IL or missing references) //IL_00ea: Unknown result type (might be due to invalid IL or missing references) this.forWrapping = forWrapping; engine = new CbcBlockCipher(new DesEdeEngine()); SecureRandom secureRandom; if (parameters is ParametersWithRandom) { ParametersWithRandom parametersWithRandom = (ParametersWithRandom)parameters; parameters = parametersWithRandom.Parameters; secureRandom = parametersWithRandom.Random; } else { secureRandom = new SecureRandom(); } if (parameters is KeyParameter) { param = (KeyParameter)parameters; if (this.forWrapping) { iv = new byte[8]; ((Random)secureRandom).NextBytes(iv); paramPlusIV = new ParametersWithIV(param, iv); } } else if (parameters is ParametersWithIV) { if (!forWrapping) { throw new ArgumentException("You should not supply an IV for unwrapping"); } paramPlusIV = (ParametersWithIV)parameters; iv = paramPlusIV.GetIV(); param = (KeyParameter)paramPlusIV.Parameters; if (iv.Length != 8) { throw new ArgumentException("IV is not 8 octets", "parameters"); } } }
public static byte[] DecryptAES(byte[] outputBytes, byte[] key, byte[] iVector) { byte[] iv = iVector; //new byte[16]; AesEngine engine = new AesEngine(); CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); //Default scheme is PKCS5/PKCS7 KeyParameter keyParam = new KeyParameter(key); ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16); //Decrypt cipher.Init(false, keyParamWithIV); byte[] comparisonBytes = new byte[cipher.GetOutputSize(outputBytes.Length)]; int length = cipher.ProcessBytes(outputBytes, comparisonBytes, 0); cipher.DoFinal(comparisonBytes, length); //Do the final block return(comparisonBytes); }
internal static DERForRecipientParams CalculateDERForRecipientParams(byte[] @in) { /* * According to ISO 32000-2 (7.6.5.3 Public-key encryption algorithms) RC-2 algorithm is outdated * and should be replaced with a safer one 256-bit AES-CBC: * The algorithms that shall be used to encrypt the enveloped data in the CMS object are: * - RC4 with key lengths up to 256-bits (deprecated); * - DES, Triple DES, RC2 with key lengths up to 128 bits (deprecated); * - 128-bit AES in Cipher Block Chaining (CBC) mode (deprecated); * - 192-bit AES in CBC mode (deprecated); * - 256-bit AES in CBC mode. */ String s = "1.2.840.113549.3.2"; DERForRecipientParams parameters = new DERForRecipientParams(); byte[] outp = new byte[100]; DerObjectIdentifier derob = new DerObjectIdentifier(s); // keyp byte[] abyte0 = IVGenerator.GetIV(16); IBufferedCipher cf = CipherUtilities.GetCipher(derob); KeyParameter kp = new KeyParameter(abyte0); byte[] iv = IVGenerator.GetIV(cf.GetBlockSize()); ParametersWithIV piv = new ParametersWithIV(kp, iv); cf.Init(true, piv); int len = cf.DoFinal(@in, outp, 0); byte[] abyte1 = new byte[len]; Array.Copy(outp, 0, abyte1, 0, len); Asn1EncodableVector ev = new Asn1EncodableVector(); ev.Add(new DerInteger(58)); ev.Add(new DerOctetString(iv)); DerSequence seq = new DerSequence(ev); parameters.abyte0 = abyte0; parameters.abyte1 = abyte1; parameters.algorithmIdentifier = new AlgorithmIdentifier(derob, seq); return(parameters); }
public TlsBlockCipher(TlsClientContext context, IBlockCipher encryptCipher, IBlockCipher decryptCipher, IDigest writeDigest, IDigest readDigest, int cipherKeySize) { this.context = context; this.encryptCipher = encryptCipher; this.decryptCipher = decryptCipher; int prfSize = (2 * cipherKeySize) + writeDigest.GetDigestSize() + readDigest.GetDigestSize() + encryptCipher.GetBlockSize() + decryptCipher.GetBlockSize(); SecurityParameters securityParameters = context.SecurityParameters; byte[] keyBlock = TlsUtilities.PRF(securityParameters.masterSecret, "key expansion", TlsUtilities.Concat(securityParameters.serverRandom, securityParameters.clientRandom), prfSize); int offset = 0; // Init MACs writeMac = CreateTlsMac(writeDigest, keyBlock, ref offset); readMac = CreateTlsMac(readDigest, keyBlock, ref offset); // Build keys KeyParameter encryptKey = CreateKeyParameter(keyBlock, ref offset, cipherKeySize); KeyParameter decryptKey = CreateKeyParameter(keyBlock, ref offset, cipherKeySize); // Add IVs ParametersWithIV encryptParams = CreateParametersWithIV(encryptKey, keyBlock, ref offset, encryptCipher.GetBlockSize()); ParametersWithIV decryptParams = CreateParametersWithIV(decryptKey, keyBlock, ref offset, decryptCipher.GetBlockSize()); if (offset != prfSize) { throw new TlsFatalAlert(AlertDescription.internal_error); } // Init Ciphers encryptCipher.Init(true, encryptParams); decryptCipher.Init(false, decryptParams); }
public void Init(bool par0, ParametersWithIV parametersWithIv) { throw new System.NotImplementedException(); }