|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace NetworkLibrary.Components |
| 7 | +{ |
| 8 | + public class AesAlgorithm |
| 9 | + { |
| 10 | + protected Aes algorithm; |
| 11 | + protected ICryptoTransform encryptor; |
| 12 | + protected ICryptoTransform decryptor; |
| 13 | + |
| 14 | + public int EncryptorInputBlockSize { get => encryptor.InputBlockSize; } |
| 15 | + public int EncryptorOutputBlockSize { get => encryptor.OutputBlockSize; } |
| 16 | + public int DecryptorInputBlockSize { get => decryptor.InputBlockSize; } |
| 17 | + public int DecryptorOutputBlockSize { get => decryptor.OutputBlockSize; } |
| 18 | + |
| 19 | + private byte[] finalBlock= new byte[0]; |
| 20 | + public AesAlgorithm(byte[] Key, byte[] IV,string algorithmName = null) |
| 21 | + { |
| 22 | + if(algorithmName == null) |
| 23 | + { |
| 24 | + algorithm = Aes.Create(); |
| 25 | + } |
| 26 | + else |
| 27 | + { |
| 28 | + algorithm = Aes.Create(algorithmName); |
| 29 | + } |
| 30 | + |
| 31 | + algorithm.Key = Key; |
| 32 | + algorithm.IV = IV; |
| 33 | + |
| 34 | + encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV); |
| 35 | + decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV); |
| 36 | + } |
| 37 | + |
| 38 | + public int GetEncriptorOutputSize(int inputSize) |
| 39 | + { |
| 40 | + return inputSize + (EncryptorOutputBlockSize - (inputSize % EncryptorOutputBlockSize)); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Decrypts a message into new byte array |
| 45 | + /// </summary> |
| 46 | + /// <param name="message"></param> |
| 47 | + /// <returns></returns> |
| 48 | + public byte[] Decrypt(byte[] message) |
| 49 | + { |
| 50 | + byte[] output = decryptor.TransformFinalBlock(message, 0, message.Length); |
| 51 | + return output; |
| 52 | + |
| 53 | + } |
| 54 | + /// <summary> |
| 55 | + /// Decrypts a message from buffer region into new byte array |
| 56 | + /// </summary> |
| 57 | + /// <param name="buffer"></param> |
| 58 | + /// <param name="offset"></param> |
| 59 | + /// <param name="count"></param> |
| 60 | + /// <returns></returns> |
| 61 | + public byte[] Decrypt(byte[] buffer, int offset, int count) |
| 62 | + { |
| 63 | + byte[] output = decryptor.TransformFinalBlock(buffer, offset, count); |
| 64 | + return output; |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Decryps a message partially into output buffer without the final block. |
| 70 | + /// </summary> |
| 71 | + /// <param name="source"></param> |
| 72 | + /// <param name="sourceOffset"></param> |
| 73 | + /// <param name="sourceCount"></param> |
| 74 | + /// <param name="output"></param> |
| 75 | + /// <param name="outputOffset"></param> |
| 76 | + /// <returns></returns> |
| 77 | + public int PartialDecrpytInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset) |
| 78 | + { |
| 79 | + return decryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Decrypts a message into output destination array fully. |
| 85 | + /// </summary> |
| 86 | + /// <param name="source"></param> |
| 87 | + /// <param name="sourceOffset"></param> |
| 88 | + /// <param name="sourceCount"></param> |
| 89 | + /// <param name="output"></param> |
| 90 | + /// <param name="outputOffset"></param> |
| 91 | + /// <returns></returns> |
| 92 | + public int DecryptInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset) |
| 93 | + { |
| 94 | + int amountDecripted = decryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset); |
| 95 | + |
| 96 | + var lastChunk = Decrypt(finalBlock); |
| 97 | + |
| 98 | + if (lastChunk.Length != 0) |
| 99 | + { |
| 100 | + Buffer.BlockCopy(lastChunk, 0, output, outputOffset + amountDecripted, lastChunk.Length); |
| 101 | + amountDecripted += lastChunk.Length; |
| 102 | + } |
| 103 | + |
| 104 | + return amountDecripted; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Encrypts a message and retuns the encripted array as a new byte array |
| 109 | + /// </summary> |
| 110 | + /// <param name="message"></param> |
| 111 | + /// <returns></returns> |
| 112 | + public byte[] Encrypt(byte[] message) |
| 113 | + { |
| 114 | + return encryptor.TransformFinalBlock(message, 0, message.Length); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Encrypts a message from buffer region and retuns the encripted array as a new byte array |
| 120 | + /// </summary> |
| 121 | + /// <param name="message"></param> |
| 122 | + /// <returns></returns> |
| 123 | + public byte[] Encrypt(byte[] buffer, int offset, int count) |
| 124 | + { |
| 125 | + return encryptor.TransformFinalBlock(buffer, offset, count); |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Encrypts a message partially into output buffer without the final block. |
| 131 | + /// </summary> |
| 132 | + /// <param name="source"></param> |
| 133 | + /// <param name="sourceOffset"></param> |
| 134 | + /// <param name="sourceCount"></param> |
| 135 | + /// <param name="output"></param> |
| 136 | + /// <param name="outputOffset"></param> |
| 137 | + /// <returns></returns> |
| 138 | + public int PartialEncrpytInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset) |
| 139 | + { |
| 140 | + return encryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset); |
| 141 | + |
| 142 | + } |
| 143 | + /// <summary> |
| 144 | + /// Decrypts a message into output destination array fully. |
| 145 | + /// </summary> |
| 146 | + /// <param name="source"></param> |
| 147 | + /// <param name="sourceOffset"></param> |
| 148 | + /// <param name="sourceCount"></param> |
| 149 | + /// <param name="output"></param> |
| 150 | + /// <param name="outputOffset"></param> |
| 151 | + /// <returns></returns> |
| 152 | + public int EncryptInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset) |
| 153 | + { |
| 154 | + // it has to be multiple of block size to do partial. |
| 155 | + if (sourceCount < EncryptorOutputBlockSize) |
| 156 | + { |
| 157 | + byte[] oneShot = Encrypt(source, sourceOffset, sourceCount); |
| 158 | + Buffer.BlockCopy(oneShot, 0, output, outputOffset, oneShot.Length); |
| 159 | + return oneShot.Length; |
| 160 | + } |
| 161 | + |
| 162 | + int partialTransformAmount = sourceCount - sourceCount % EncryptorOutputBlockSize; |
| 163 | + int amountDecripted = encryptor.TransformBlock(source, sourceOffset, partialTransformAmount, output, outputOffset); |
| 164 | + |
| 165 | + byte[] lastChunk = Encrypt(source, sourceOffset + amountDecripted, sourceCount % EncryptorOutputBlockSize); |
| 166 | + |
| 167 | + if (lastChunk.Length != 0) |
| 168 | + { |
| 169 | + Buffer.BlockCopy(lastChunk, 0, output, outputOffset + amountDecripted, lastChunk.Length); |
| 170 | + amountDecripted += lastChunk.Length; |
| 171 | + } |
| 172 | + |
| 173 | + return amountDecripted; |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + } |
| 178 | +} |
0 commit comments