|
| 1 | +using System; |
| 2 | +using System.Security.Cryptography; |
| 3 | + |
| 4 | +namespace Force.Blazer.Encyption |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// This class emulates ISO10126 AES transform for .NET Core |
| 8 | + /// </summary> |
| 9 | + public class Iso10126TransformEmulator : ICryptoTransform |
| 10 | + { |
| 11 | + private readonly ICryptoTransform _origTransofrm; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Constructor. Origignal transform should be aes with PKCS7 padding |
| 15 | + /// </summary> |
| 16 | + /// <param name="origTransofrm"></param> |
| 17 | + public Iso10126TransformEmulator(ICryptoTransform origTransofrm) |
| 18 | + { |
| 19 | + _origTransofrm = origTransofrm; |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Dispose current transform |
| 24 | + /// </summary> |
| 25 | + public void Dispose() |
| 26 | + { |
| 27 | + _origTransofrm.Dispose(); |
| 28 | + } |
| 29 | + |
| 30 | + int ICryptoTransform.TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) |
| 31 | + { |
| 32 | + return _origTransofrm.TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset); |
| 33 | + } |
| 34 | + |
| 35 | + byte[] ICryptoTransform.TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) |
| 36 | + { |
| 37 | + var outBuffer = new byte[_origTransofrm.OutputBlockSize + inputCount]; |
| 38 | + var dummyBuffer = new byte[_origTransofrm.InputBlockSize + inputCount]; |
| 39 | + Buffer.BlockCopy(inputBuffer, inputOffset, dummyBuffer, 0, inputCount); |
| 40 | + _origTransofrm.TransformBlock(dummyBuffer, 0, dummyBuffer.Length, outBuffer, 0); |
| 41 | + Array.Resize(ref outBuffer, outBuffer.Length - outBuffer[outBuffer.Length - 1]); |
| 42 | + return outBuffer; |
| 43 | + } |
| 44 | + |
| 45 | + int ICryptoTransform.InputBlockSize |
| 46 | + { |
| 47 | + get |
| 48 | + { |
| 49 | + return _origTransofrm.InputBlockSize; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + int ICryptoTransform.OutputBlockSize |
| 54 | + { |
| 55 | + get |
| 56 | + { |
| 57 | + return _origTransofrm.OutputBlockSize; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + bool ICryptoTransform.CanTransformMultipleBlocks |
| 62 | + { |
| 63 | + get |
| 64 | + { |
| 65 | + return _origTransofrm.CanTransformMultipleBlocks; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + bool ICryptoTransform.CanReuseTransform |
| 70 | + { |
| 71 | + get |
| 72 | + { |
| 73 | + return _origTransofrm.CanReuseTransform; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments