fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. byte[] key = Convert.FromBase64String("DWsCdkYUwgXUPLRYWV/11mj56HsbS9Qw1EGYrygLETw=");
  12. byte[] iv = Convert.FromBase64String("120AY/Oe+jLhXiPd8vBcUQ==");
  13. byte[] cipher = Convert.FromBase64String("zfpqy8EVXg9xbBc=");
  14. var provider = new CounterModeCryptoTransform(new AesManaged { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }, key, iv);
  15. byte[] plain = provider.TransformFinalBlock(cipher, 0, cipher.Length);
  16. Console.WriteLine(Encoding.UTF8.GetString(plain));
  17. }
  18. }
  19.  
  20. public class CounterModeCryptoTransform : ICryptoTransform
  21. {
  22. private readonly byte[] _counter;
  23. private readonly ICryptoTransform _counterEncryptor;
  24. private readonly Queue<byte> _xorMask = new Queue<byte>();
  25. private readonly SymmetricAlgorithm _symmetricAlgorithm;
  26.  
  27. public CounterModeCryptoTransform(SymmetricAlgorithm symmetricAlgorithm, byte[] key, byte[] counter)
  28. {
  29. if (symmetricAlgorithm == null) throw new ArgumentNullException("symmetricAlgorithm");
  30. if (key == null) throw new ArgumentNullException("key");
  31. if (counter == null) throw new ArgumentNullException("counter");
  32. if (counter.Length != symmetricAlgorithm.BlockSize / 8)
  33. throw new ArgumentException(String.Format("Counter size must be same as block size (actual: {0}, expected: {1})",
  34. counter.Length, symmetricAlgorithm.BlockSize / 8));
  35.  
  36. _symmetricAlgorithm = symmetricAlgorithm;
  37. _counter = counter;
  38.  
  39. var zeroIv = new byte[_symmetricAlgorithm.BlockSize / 8];
  40. _counterEncryptor = symmetricAlgorithm.CreateEncryptor(key, zeroIv);
  41. }
  42.  
  43. public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
  44. {
  45. var output = new byte[inputCount];
  46. TransformBlock(inputBuffer, inputOffset, inputCount, output, 0);
  47. return output;
  48. }
  49.  
  50. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  51. {
  52. for (var i = 0; i < inputCount; i++)
  53. {
  54. if (NeedMoreXorMaskBytes()) EncryptCounterThenIncrement();
  55.  
  56. var mask = _xorMask.Dequeue();
  57. outputBuffer[outputOffset + i] = (byte)(inputBuffer[inputOffset + i] ^ mask);
  58. }
  59.  
  60. return inputCount;
  61. }
  62.  
  63. private bool NeedMoreXorMaskBytes()
  64. {
  65. return _xorMask.Count == 0;
  66. }
  67.  
  68. private void EncryptCounterThenIncrement()
  69. {
  70. var counterModeBlock = new byte[_symmetricAlgorithm.BlockSize / 8];
  71.  
  72. _counterEncryptor.TransformBlock(_counter, 0, _counter.Length, counterModeBlock, 0);
  73. IncrementCounter();
  74.  
  75. foreach (var b in counterModeBlock)
  76. {
  77. _xorMask.Enqueue(b);
  78. }
  79. }
  80.  
  81. private void IncrementCounter()
  82. {
  83. for (var i = _counter.Length - 1; i >= 0; i--)
  84. {
  85. if (++_counter[i] != 0)
  86. break;
  87. }
  88. }
  89.  
  90. public int InputBlockSize { get { return _symmetricAlgorithm.BlockSize / 8; } }
  91. public int OutputBlockSize { get { return _symmetricAlgorithm.BlockSize / 8; } }
  92. public bool CanTransformMultipleBlocks { get { return true; } }
  93. public bool CanReuseTransform { get { return false; } }
  94.  
  95. public void Dispose()
  96. {
  97. }
  98. }
Success #stdin #stdout 0.03s 26844KB
stdin
Standard input is empty
stdout
Test string