123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using Org.BouncyCastle.Crypto.Parameters;
- namespace Org.BouncyCastle.Crypto.Modes
- {
-
- public class CfbBlockCipher
- : IBlockCipher
- {
- private byte[] IV;
- private byte[] cfbV;
- private byte[] cfbOutV;
- private bool encrypting;
- private readonly int blockSize;
- private readonly IBlockCipher cipher;
-
- public CfbBlockCipher(
- IBlockCipher cipher,
- int bitBlockSize)
- {
- this.cipher = cipher;
- this.blockSize = bitBlockSize / 8;
- this.IV = new byte[cipher.GetBlockSize()];
- this.cfbV = new byte[cipher.GetBlockSize()];
- this.cfbOutV = new byte[cipher.GetBlockSize()];
- }
-
- public IBlockCipher GetUnderlyingCipher()
- {
- return cipher;
- }
-
- 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();
-
- if (parameters != null)
- {
- cipher.Init(true, parameters);
- }
- }
-
- public string AlgorithmName
- {
- get { return cipher.AlgorithmName + "/CFB" + (blockSize * 8); }
- }
- public bool IsPartialBlockOkay
- {
- get { return true; }
- }
-
- public int GetBlockSize()
- {
- return blockSize;
- }
-
- public int ProcessBlock(
- byte[] input,
- int inOff,
- byte[] output,
- int outOff)
- {
- return (encrypting)
- ? EncryptBlock(input, inOff, output, outOff)
- : DecryptBlock(input, inOff, output, outOff);
- }
-
- public int EncryptBlock(
- byte[] input,
- int inOff,
- byte[] outBytes,
- int outOff)
- {
- if ((inOff + blockSize) > input.Length)
- {
- throw new DataLengthException("input buffer too short");
- }
- if ((outOff + blockSize) > outBytes.Length)
- {
- throw new DataLengthException("output buffer too short");
- }
- cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
-
-
-
- for (int i = 0; i < blockSize; i++)
- {
- outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
- }
-
-
-
- Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
- Array.Copy(outBytes, outOff, cfbV, cfbV.Length - blockSize, blockSize);
- return blockSize;
- }
-
- public int DecryptBlock(
- byte[] input,
- int inOff,
- byte[] outBytes,
- int outOff)
- {
- if ((inOff + blockSize) > input.Length)
- {
- throw new DataLengthException("input buffer too short");
- }
- if ((outOff + blockSize) > outBytes.Length)
- {
- throw new DataLengthException("output buffer too short");
- }
- cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
-
-
-
- Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
- Array.Copy(input, inOff, cfbV, cfbV.Length - blockSize, blockSize);
-
-
-
- for (int i = 0; i < blockSize; i++)
- {
- outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
- }
- return blockSize;
- }
-
- public void Reset()
- {
- Array.Copy(IV, 0, cfbV, 0, IV.Length);
- cipher.Reset();
- }
- }
- }
- #endif
|