| | | 1 | | using System; |
| | | 2 | | using Renci.SshNet.Common; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Security.Cryptography.Ciphers |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Implements DES cipher algorithm. |
| | | 8 | | /// </summary> |
| | | 9 | | public class DesCipher : BlockCipher |
| | | 10 | | { |
| | | 11 | | private int[] _encryptionKey; |
| | | 12 | | |
| | | 13 | | private int[] _decryptionKey; |
| | | 14 | | |
| | 4 | 15 | | private static readonly short[] Bytebit = { 128, 64, 32, 16, 8, 4, 2, 1 }; |
| | | 16 | | |
| | 4 | 17 | | private static readonly int[] Bigbyte = |
| | 4 | 18 | | { |
| | 4 | 19 | | 0x800000, 0x400000, 0x200000, 0x100000, |
| | 4 | 20 | | 0x080000, 0x040000, 0x020000, 0x010000, |
| | 4 | 21 | | 0x008000, 0x004000, 0x002000, 0x001000, |
| | 4 | 22 | | 0x000800, 0x000400, 0x000200, 0x000100, |
| | 4 | 23 | | 0x000080, 0x000040, 0x000020, 0x000010, |
| | 4 | 24 | | 0x000008, 0x000004, 0x000002, 0x000001 |
| | 4 | 25 | | }; |
| | | 26 | | |
| | | 27 | | /* |
| | | 28 | | * Use the key schedule specified in the Standard (ANSI X3.92-1981). |
| | | 29 | | */ |
| | | 30 | | |
| | 4 | 31 | | private static readonly byte[] Pc1 = |
| | 4 | 32 | | { |
| | 4 | 33 | | 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, |
| | 4 | 34 | | 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, |
| | 4 | 35 | | 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, |
| | 4 | 36 | | 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 |
| | 4 | 37 | | }; |
| | | 38 | | |
| | 4 | 39 | | private static readonly byte[] Totrot = |
| | 4 | 40 | | { |
| | 4 | 41 | | 1, 2, 4, 6, 8, 10, 12, 14, |
| | 4 | 42 | | 15, 17, 19, 21, 23, 25, 27, 28 |
| | 4 | 43 | | }; |
| | | 44 | | |
| | 4 | 45 | | private static readonly byte[] Pc2 = |
| | 4 | 46 | | { |
| | 4 | 47 | | 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, |
| | 4 | 48 | | 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, |
| | 4 | 49 | | 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, |
| | 4 | 50 | | 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 |
| | 4 | 51 | | }; |
| | | 52 | | |
| | 4 | 53 | | private static readonly uint[] Sp1 = |
| | 4 | 54 | | { |
| | 4 | 55 | | 0x01010400, 0x00000000, 0x00010000, 0x01010404, |
| | 4 | 56 | | 0x01010004, 0x00010404, 0x00000004, 0x00010000, |
| | 4 | 57 | | 0x00000400, 0x01010400, 0x01010404, 0x00000400, |
| | 4 | 58 | | 0x01000404, 0x01010004, 0x01000000, 0x00000004, |
| | 4 | 59 | | 0x00000404, 0x01000400, 0x01000400, 0x00010400, |
| | 4 | 60 | | 0x00010400, 0x01010000, 0x01010000, 0x01000404, |
| | 4 | 61 | | 0x00010004, 0x01000004, 0x01000004, 0x00010004, |
| | 4 | 62 | | 0x00000000, 0x00000404, 0x00010404, 0x01000000, |
| | 4 | 63 | | 0x00010000, 0x01010404, 0x00000004, 0x01010000, |
| | 4 | 64 | | 0x01010400, 0x01000000, 0x01000000, 0x00000400, |
| | 4 | 65 | | 0x01010004, 0x00010000, 0x00010400, 0x01000004, |
| | 4 | 66 | | 0x00000400, 0x00000004, 0x01000404, 0x00010404, |
| | 4 | 67 | | 0x01010404, 0x00010004, 0x01010000, 0x01000404, |
| | 4 | 68 | | 0x01000004, 0x00000404, 0x00010404, 0x01010400, |
| | 4 | 69 | | 0x00000404, 0x01000400, 0x01000400, 0x00000000, |
| | 4 | 70 | | 0x00010004, 0x00010400, 0x00000000, 0x01010004 |
| | 4 | 71 | | }; |
| | | 72 | | |
| | 4 | 73 | | private static readonly uint[] Sp2 = |
| | 4 | 74 | | { |
| | 4 | 75 | | 0x80108020, 0x80008000, 0x00008000, 0x00108020, |
| | 4 | 76 | | 0x00100000, 0x00000020, 0x80100020, 0x80008020, |
| | 4 | 77 | | 0x80000020, 0x80108020, 0x80108000, 0x80000000, |
| | 4 | 78 | | 0x80008000, 0x00100000, 0x00000020, 0x80100020, |
| | 4 | 79 | | 0x00108000, 0x00100020, 0x80008020, 0x00000000, |
| | 4 | 80 | | 0x80000000, 0x00008000, 0x00108020, 0x80100000, |
| | 4 | 81 | | 0x00100020, 0x80000020, 0x00000000, 0x00108000, |
| | 4 | 82 | | 0x00008020, 0x80108000, 0x80100000, 0x00008020, |
| | 4 | 83 | | 0x00000000, 0x00108020, 0x80100020, 0x00100000, |
| | 4 | 84 | | 0x80008020, 0x80100000, 0x80108000, 0x00008000, |
| | 4 | 85 | | 0x80100000, 0x80008000, 0x00000020, 0x80108020, |
| | 4 | 86 | | 0x00108020, 0x00000020, 0x00008000, 0x80000000, |
| | 4 | 87 | | 0x00008020, 0x80108000, 0x00100000, 0x80000020, |
| | 4 | 88 | | 0x00100020, 0x80008020, 0x80000020, 0x00100020, |
| | 4 | 89 | | 0x00108000, 0x00000000, 0x80008000, 0x00008020, |
| | 4 | 90 | | 0x80000000, 0x80100020, 0x80108020, 0x00108000 |
| | 4 | 91 | | }; |
| | | 92 | | |
| | 4 | 93 | | private static readonly uint[] Sp3 = |
| | 4 | 94 | | { |
| | 4 | 95 | | 0x00000208, 0x08020200, 0x00000000, 0x08020008, |
| | 4 | 96 | | 0x08000200, 0x00000000, 0x00020208, 0x08000200, |
| | 4 | 97 | | 0x00020008, 0x08000008, 0x08000008, 0x00020000, |
| | 4 | 98 | | 0x08020208, 0x00020008, 0x08020000, 0x00000208, |
| | 4 | 99 | | 0x08000000, 0x00000008, 0x08020200, 0x00000200, |
| | 4 | 100 | | 0x00020200, 0x08020000, 0x08020008, 0x00020208, |
| | 4 | 101 | | 0x08000208, 0x00020200, 0x00020000, 0x08000208, |
| | 4 | 102 | | 0x00000008, 0x08020208, 0x00000200, 0x08000000, |
| | 4 | 103 | | 0x08020200, 0x08000000, 0x00020008, 0x00000208, |
| | 4 | 104 | | 0x00020000, 0x08020200, 0x08000200, 0x00000000, |
| | 4 | 105 | | 0x00000200, 0x00020008, 0x08020208, 0x08000200, |
| | 4 | 106 | | 0x08000008, 0x00000200, 0x00000000, 0x08020008, |
| | 4 | 107 | | 0x08000208, 0x00020000, 0x08000000, 0x08020208, |
| | 4 | 108 | | 0x00000008, 0x00020208, 0x00020200, 0x08000008, |
| | 4 | 109 | | 0x08020000, 0x08000208, 0x00000208, 0x08020000, |
| | 4 | 110 | | 0x00020208, 0x00000008, 0x08020008, 0x00020200 |
| | 4 | 111 | | }; |
| | | 112 | | |
| | 4 | 113 | | private static readonly uint[] Sp4 = |
| | 4 | 114 | | { |
| | 4 | 115 | | 0x00802001, 0x00002081, 0x00002081, 0x00000080, |
| | 4 | 116 | | 0x00802080, 0x00800081, 0x00800001, 0x00002001, |
| | 4 | 117 | | 0x00000000, 0x00802000, 0x00802000, 0x00802081, |
| | 4 | 118 | | 0x00000081, 0x00000000, 0x00800080, 0x00800001, |
| | 4 | 119 | | 0x00000001, 0x00002000, 0x00800000, 0x00802001, |
| | 4 | 120 | | 0x00000080, 0x00800000, 0x00002001, 0x00002080, |
| | 4 | 121 | | 0x00800081, 0x00000001, 0x00002080, 0x00800080, |
| | 4 | 122 | | 0x00002000, 0x00802080, 0x00802081, 0x00000081, |
| | 4 | 123 | | 0x00800080, 0x00800001, 0x00802000, 0x00802081, |
| | 4 | 124 | | 0x00000081, 0x00000000, 0x00000000, 0x00802000, |
| | 4 | 125 | | 0x00002080, 0x00800080, 0x00800081, 0x00000001, |
| | 4 | 126 | | 0x00802001, 0x00002081, 0x00002081, 0x00000080, |
| | 4 | 127 | | 0x00802081, 0x00000081, 0x00000001, 0x00002000, |
| | 4 | 128 | | 0x00800001, 0x00002001, 0x00802080, 0x00800081, |
| | 4 | 129 | | 0x00002001, 0x00002080, 0x00800000, 0x00802001, |
| | 4 | 130 | | 0x00000080, 0x00800000, 0x00002000, 0x00802080 |
| | 4 | 131 | | }; |
| | | 132 | | |
| | 4 | 133 | | private static readonly uint[] Sp5 = |
| | 4 | 134 | | { |
| | 4 | 135 | | 0x00000100, 0x02080100, 0x02080000, 0x42000100, |
| | 4 | 136 | | 0x00080000, 0x00000100, 0x40000000, 0x02080000, |
| | 4 | 137 | | 0x40080100, 0x00080000, 0x02000100, 0x40080100, |
| | 4 | 138 | | 0x42000100, 0x42080000, 0x00080100, 0x40000000, |
| | 4 | 139 | | 0x02000000, 0x40080000, 0x40080000, 0x00000000, |
| | 4 | 140 | | 0x40000100, 0x42080100, 0x42080100, 0x02000100, |
| | 4 | 141 | | 0x42080000, 0x40000100, 0x00000000, 0x42000000, |
| | 4 | 142 | | 0x02080100, 0x02000000, 0x42000000, 0x00080100, |
| | 4 | 143 | | 0x00080000, 0x42000100, 0x00000100, 0x02000000, |
| | 4 | 144 | | 0x40000000, 0x02080000, 0x42000100, 0x40080100, |
| | 4 | 145 | | 0x02000100, 0x40000000, 0x42080000, 0x02080100, |
| | 4 | 146 | | 0x40080100, 0x00000100, 0x02000000, 0x42080000, |
| | 4 | 147 | | 0x42080100, 0x00080100, 0x42000000, 0x42080100, |
| | 4 | 148 | | 0x02080000, 0x00000000, 0x40080000, 0x42000000, |
| | 4 | 149 | | 0x00080100, 0x02000100, 0x40000100, 0x00080000, |
| | 4 | 150 | | 0x00000000, 0x40080000, 0x02080100, 0x40000100 |
| | 4 | 151 | | }; |
| | | 152 | | |
| | 4 | 153 | | private static readonly uint[] Sp6 = |
| | 4 | 154 | | { |
| | 4 | 155 | | 0x20000010, 0x20400000, 0x00004000, 0x20404010, |
| | 4 | 156 | | 0x20400000, 0x00000010, 0x20404010, 0x00400000, |
| | 4 | 157 | | 0x20004000, 0x00404010, 0x00400000, 0x20000010, |
| | 4 | 158 | | 0x00400010, 0x20004000, 0x20000000, 0x00004010, |
| | 4 | 159 | | 0x00000000, 0x00400010, 0x20004010, 0x00004000, |
| | 4 | 160 | | 0x00404000, 0x20004010, 0x00000010, 0x20400010, |
| | 4 | 161 | | 0x20400010, 0x00000000, 0x00404010, 0x20404000, |
| | 4 | 162 | | 0x00004010, 0x00404000, 0x20404000, 0x20000000, |
| | 4 | 163 | | 0x20004000, 0x00000010, 0x20400010, 0x00404000, |
| | 4 | 164 | | 0x20404010, 0x00400000, 0x00004010, 0x20000010, |
| | 4 | 165 | | 0x00400000, 0x20004000, 0x20000000, 0x00004010, |
| | 4 | 166 | | 0x20000010, 0x20404010, 0x00404000, 0x20400000, |
| | 4 | 167 | | 0x00404010, 0x20404000, 0x00000000, 0x20400010, |
| | 4 | 168 | | 0x00000010, 0x00004000, 0x20400000, 0x00404010, |
| | 4 | 169 | | 0x00004000, 0x00400010, 0x20004010, 0x00000000, |
| | 4 | 170 | | 0x20404000, 0x20000000, 0x00400010, 0x20004010 |
| | 4 | 171 | | }; |
| | | 172 | | |
| | 4 | 173 | | private static readonly uint[] Sp7 = |
| | 4 | 174 | | { |
| | 4 | 175 | | 0x00200000, 0x04200002, 0x04000802, 0x00000000, |
| | 4 | 176 | | 0x00000800, 0x04000802, 0x00200802, 0x04200800, |
| | 4 | 177 | | 0x04200802, 0x00200000, 0x00000000, 0x04000002, |
| | 4 | 178 | | 0x00000002, 0x04000000, 0x04200002, 0x00000802, |
| | 4 | 179 | | 0x04000800, 0x00200802, 0x00200002, 0x04000800, |
| | 4 | 180 | | 0x04000002, 0x04200000, 0x04200800, 0x00200002, |
| | 4 | 181 | | 0x04200000, 0x00000800, 0x00000802, 0x04200802, |
| | 4 | 182 | | 0x00200800, 0x00000002, 0x04000000, 0x00200800, |
| | 4 | 183 | | 0x04000000, 0x00200800, 0x00200000, 0x04000802, |
| | 4 | 184 | | 0x04000802, 0x04200002, 0x04200002, 0x00000002, |
| | 4 | 185 | | 0x00200002, 0x04000000, 0x04000800, 0x00200000, |
| | 4 | 186 | | 0x04200800, 0x00000802, 0x00200802, 0x04200800, |
| | 4 | 187 | | 0x00000802, 0x04000002, 0x04200802, 0x04200000, |
| | 4 | 188 | | 0x00200800, 0x00000000, 0x00000002, 0x04200802, |
| | 4 | 189 | | 0x00000000, 0x00200802, 0x04200000, 0x00000800, |
| | 4 | 190 | | 0x04000002, 0x04000800, 0x00000800, 0x00200002 |
| | 4 | 191 | | }; |
| | | 192 | | |
| | 4 | 193 | | private static readonly uint[] Sp8 = |
| | 4 | 194 | | { |
| | 4 | 195 | | 0x10001040, 0x00001000, 0x00040000, 0x10041040, |
| | 4 | 196 | | 0x10000000, 0x10001040, 0x00000040, 0x10000000, |
| | 4 | 197 | | 0x00040040, 0x10040000, 0x10041040, 0x00041000, |
| | 4 | 198 | | 0x10041000, 0x00041040, 0x00001000, 0x00000040, |
| | 4 | 199 | | 0x10040000, 0x10000040, 0x10001000, 0x00001040, |
| | 4 | 200 | | 0x00041000, 0x00040040, 0x10040040, 0x10041000, |
| | 4 | 201 | | 0x00001040, 0x00000000, 0x00000000, 0x10040040, |
| | 4 | 202 | | 0x10000040, 0x10001000, 0x00041040, 0x00040000, |
| | 4 | 203 | | 0x00041040, 0x00040000, 0x10041000, 0x00001000, |
| | 4 | 204 | | 0x00000040, 0x10040040, 0x00001000, 0x00041040, |
| | 4 | 205 | | 0x10001000, 0x00000040, 0x10000040, 0x10040000, |
| | 4 | 206 | | 0x10040040, 0x10000000, 0x00040000, 0x10001040, |
| | 4 | 207 | | 0x00000000, 0x10041040, 0x00040040, 0x10000040, |
| | 4 | 208 | | 0x10040000, 0x10001000, 0x10001040, 0x00000000, |
| | 4 | 209 | | 0x10041040, 0x00041000, 0x00041000, 0x00001040, |
| | 4 | 210 | | 0x00001040, 0x00040040, 0x10000000, 0x10041000 |
| | 4 | 211 | | }; |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Initializes a new instance of the <see cref="DesCipher"/> class. |
| | | 215 | | /// </summary> |
| | | 216 | | /// <param name="key">The key.</param> |
| | | 217 | | /// <param name="mode">The mode.</param> |
| | | 218 | | /// <param name="padding">The padding.</param> |
| | | 219 | | /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| | | 220 | | public DesCipher(byte[] key, CipherMode mode, CipherPadding padding) |
| | 34 | 221 | | : base(key, 8, mode, padding) |
| | 34 | 222 | | { |
| | 34 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="inputBuffer">The input data to encrypt.</param> |
| | | 229 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 230 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 231 | | /// <param name="outputBuffer">The output to which to write encrypted data.</param> |
| | | 232 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 233 | | /// <returns> |
| | | 234 | | /// The number of bytes encrypted. |
| | | 235 | | /// </returns> |
| | | 236 | | public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 9 | 237 | | { |
| | 9 | 238 | | if ((inputOffset + BlockSize) > inputBuffer.Length) |
| | 0 | 239 | | { |
| | 0 | 240 | | throw new ArgumentException("input buffer too short"); |
| | | 241 | | } |
| | | 242 | | |
| | 9 | 243 | | if ((outputOffset + BlockSize) > outputBuffer.Length) |
| | 0 | 244 | | { |
| | 0 | 245 | | throw new ArgumentException("output buffer too short"); |
| | | 246 | | } |
| | | 247 | | |
| | 9 | 248 | | _encryptionKey ??= GenerateWorkingKey(encrypting: true, Key); |
| | | 249 | | |
| | 9 | 250 | | DesFunc(_encryptionKey, inputBuffer, inputOffset, outputBuffer, outputOffset); |
| | | 251 | | |
| | 9 | 252 | | return BlockSize; |
| | 9 | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region |
| | | 257 | | /// </summary> |
| | | 258 | | /// <param name="inputBuffer">The input data to decrypt.</param> |
| | | 259 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 260 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 261 | | /// <param name="outputBuffer">The output to which to write decrypted data.</param> |
| | | 262 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 263 | | /// <returns> |
| | | 264 | | /// The number of bytes decrypted. |
| | | 265 | | /// </returns> |
| | | 266 | | public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 456 | 267 | | { |
| | 456 | 268 | | if ((inputOffset + BlockSize) > inputBuffer.Length) |
| | 0 | 269 | | { |
| | 0 | 270 | | throw new ArgumentException("input buffer too short"); |
| | | 271 | | } |
| | | 272 | | |
| | 456 | 273 | | if ((outputOffset + BlockSize) > outputBuffer.Length) |
| | 0 | 274 | | { |
| | 0 | 275 | | throw new ArgumentException("output buffer too short"); |
| | | 276 | | } |
| | | 277 | | |
| | 456 | 278 | | _decryptionKey ??= GenerateWorkingKey(encrypting: false, Key); |
| | | 279 | | |
| | 456 | 280 | | DesFunc(_decryptionKey, inputBuffer, inputOffset, outputBuffer, outputOffset); |
| | | 281 | | |
| | 456 | 282 | | return BlockSize; |
| | 456 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Generates the working key. |
| | | 287 | | /// </summary> |
| | | 288 | | /// <param name="encrypting">if set to <see langword="true"/> [encrypting].</param> |
| | | 289 | | /// <param name="key">The key.</param> |
| | | 290 | | /// <returns>Generated working key.</returns> |
| | | 291 | | protected int[] GenerateWorkingKey(bool encrypting, byte[] key) |
| | 84 | 292 | | { |
| | 84 | 293 | | ValidateKey(); |
| | | 294 | | |
| | 84 | 295 | | var newKey = new int[32]; |
| | 84 | 296 | | var pc1m = new bool[56]; |
| | 84 | 297 | | var pcr = new bool[56]; |
| | | 298 | | |
| | 9576 | 299 | | for (var j = 0; j < 56; j++) |
| | 4704 | 300 | | { |
| | 4704 | 301 | | int l = Pc1[j]; |
| | | 302 | | |
| | 4704 | 303 | | pc1m[j] = (key[(uint) l >> 3] & Bytebit[l & 07]) != 0; |
| | 4704 | 304 | | } |
| | | 305 | | |
| | 2856 | 306 | | for (var i = 0; i < 16; i++) |
| | 1344 | 307 | | { |
| | | 308 | | int l, m; |
| | | 309 | | |
| | 1344 | 310 | | if (encrypting) |
| | 592 | 311 | | { |
| | 592 | 312 | | m = i << 1; |
| | 592 | 313 | | } |
| | | 314 | | else |
| | 752 | 315 | | { |
| | 752 | 316 | | m = (15 - i) << 1; |
| | 752 | 317 | | } |
| | | 318 | | |
| | 1344 | 319 | | var n = m + 1; |
| | 1344 | 320 | | newKey[m] = newKey[n] = 0; |
| | | 321 | | |
| | 77952 | 322 | | for (var j = 0; j < 28; j++) |
| | 37632 | 323 | | { |
| | 37632 | 324 | | l = j + Totrot[i]; |
| | 37632 | 325 | | if (l < 28) |
| | 18144 | 326 | | { |
| | 18144 | 327 | | pcr[j] = pc1m[l]; |
| | 18144 | 328 | | } |
| | | 329 | | else |
| | 19488 | 330 | | { |
| | 19488 | 331 | | pcr[j] = pc1m[l - 28]; |
| | 19488 | 332 | | } |
| | 37632 | 333 | | } |
| | | 334 | | |
| | 77952 | 335 | | for (var j = 28; j < 56; j++) |
| | 37632 | 336 | | { |
| | 37632 | 337 | | l = j + Totrot[i]; |
| | 37632 | 338 | | if (l < 56) |
| | 18144 | 339 | | { |
| | 18144 | 340 | | pcr[j] = pc1m[l]; |
| | 18144 | 341 | | } |
| | | 342 | | else |
| | 19488 | 343 | | { |
| | 19488 | 344 | | pcr[j] = pc1m[l - 28]; |
| | 19488 | 345 | | } |
| | 37632 | 346 | | } |
| | | 347 | | |
| | 67200 | 348 | | for (var j = 0; j < 24; j++) |
| | 32256 | 349 | | { |
| | 32256 | 350 | | if (pcr[Pc2[j]]) |
| | 15964 | 351 | | { |
| | 15964 | 352 | | newKey[m] |= Bigbyte[j]; |
| | 15964 | 353 | | } |
| | | 354 | | |
| | 32256 | 355 | | if (pcr[Pc2[j + 24]]) |
| | 15360 | 356 | | { |
| | 15360 | 357 | | newKey[n] |= Bigbyte[j]; |
| | 15360 | 358 | | } |
| | 32256 | 359 | | } |
| | 1344 | 360 | | } |
| | | 361 | | |
| | | 362 | | /* |
| | | 363 | | * store the processed key |
| | | 364 | | */ |
| | | 365 | | |
| | 2856 | 366 | | for (var i = 0; i != 32; i += 2) |
| | 1344 | 367 | | { |
| | 1344 | 368 | | var i1 = newKey[i]; |
| | 1344 | 369 | | var i2 = newKey[i + 1]; |
| | | 370 | | |
| | 1344 | 371 | | newKey[i] = (int) ((uint) ((i1 & 0x00fc0000) << 6) | |
| | 1344 | 372 | | (uint) ((i1 & 0x00000fc0) << 10) | |
| | 1344 | 373 | | ((uint) (i2 & 0x00fc0000) >> 10) | |
| | 1344 | 374 | | ((uint) (i2 & 0x00000fc0) >> 6)); |
| | | 375 | | |
| | 1344 | 376 | | newKey[i + 1] = (int) ((uint) ((i1 & 0x0003f000) << 12) | |
| | 1344 | 377 | | (uint) ((i1 & 0x0000003f) << 16) | |
| | 1344 | 378 | | ((uint) (i2 & 0x0003f000) >> 4) | |
| | 1344 | 379 | | (uint) (i2 & 0x0000003f)); |
| | 1344 | 380 | | } |
| | | 381 | | |
| | 84 | 382 | | return newKey; |
| | 84 | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Validates the key. |
| | | 387 | | /// </summary> |
| | | 388 | | protected virtual void ValidateKey() |
| | 9 | 389 | | { |
| | 9 | 390 | | var keySize = Key.Length * 8; |
| | | 391 | | |
| | 9 | 392 | | if (keySize != 64) |
| | 0 | 393 | | { |
| | 0 | 394 | | throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize)); |
| | | 395 | | } |
| | 9 | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Performs DES function. |
| | | 400 | | /// </summary> |
| | | 401 | | /// <param name="wKey">The w key.</param> |
| | | 402 | | /// <param name="input">The input.</param> |
| | | 403 | | /// <param name="inOff">The in off.</param> |
| | | 404 | | /// <param name="outBytes">The out bytes.</param> |
| | | 405 | | /// <param name="outOff">The out off.</param> |
| | | 406 | | protected static void DesFunc(int[] wKey, byte[] input, int inOff, byte[] outBytes, int outOff) |
| | 6936 | 407 | | { |
| | 6936 | 408 | | var left = Pack.BigEndianToUInt32(input, inOff); |
| | 6936 | 409 | | var right = Pack.BigEndianToUInt32(input, inOff + 4); |
| | | 410 | | |
| | 6936 | 411 | | var work = ((left >> 4) ^ right) & 0x0f0f0f0f; |
| | 6936 | 412 | | right ^= work; |
| | 6936 | 413 | | left ^= work << 4; |
| | 6936 | 414 | | work = ((left >> 16) ^ right) & 0x0000ffff; |
| | 6936 | 415 | | right ^= work; |
| | 6936 | 416 | | left ^= work << 16; |
| | 6936 | 417 | | work = ((right >> 2) ^ left) & 0x33333333; |
| | 6936 | 418 | | left ^= work; |
| | 6936 | 419 | | right ^= work << 2; |
| | 6936 | 420 | | work = ((right >> 8) ^ left) & 0x00ff00ff; |
| | 6936 | 421 | | left ^= work; |
| | 6936 | 422 | | right ^= work << 8; |
| | 6936 | 423 | | right = (right << 1) | (right >> 31); |
| | 6936 | 424 | | work = (left ^ right) & 0xaaaaaaaa; |
| | 6936 | 425 | | left ^= work; |
| | 6936 | 426 | | right ^= work; |
| | 6936 | 427 | | left = (left << 1) | (left >> 31); |
| | | 428 | | |
| | 124848 | 429 | | for (var round = 0; round < 8; round++) |
| | 55488 | 430 | | { |
| | 55488 | 431 | | work = (right << 28) | (right >> 4); |
| | 55488 | 432 | | work ^= (uint)wKey[(round * 4) + 0]; |
| | 55488 | 433 | | var fval = Sp7[work & 0x3f]; |
| | 55488 | 434 | | fval |= Sp5[(work >> 8) & 0x3f]; |
| | 55488 | 435 | | fval |= Sp3[(work >> 16) & 0x3f]; |
| | 55488 | 436 | | fval |= Sp1[(work >> 24) & 0x3f]; |
| | 55488 | 437 | | work = right ^ (uint) wKey[(round * 4) + 1]; |
| | 55488 | 438 | | fval |= Sp8[work & 0x3f]; |
| | 55488 | 439 | | fval |= Sp6[(work >> 8) & 0x3f]; |
| | 55488 | 440 | | fval |= Sp4[(work >> 16) & 0x3f]; |
| | 55488 | 441 | | fval |= Sp2[(work >> 24) & 0x3f]; |
| | 55488 | 442 | | left ^= fval; |
| | 55488 | 443 | | work = (left << 28) | (left >> 4); |
| | 55488 | 444 | | work ^= (uint)wKey[(round * 4) + 2]; |
| | 55488 | 445 | | fval = Sp7[work & 0x3f]; |
| | 55488 | 446 | | fval |= Sp5[(work >> 8) & 0x3f]; |
| | 55488 | 447 | | fval |= Sp3[(work >> 16) & 0x3f]; |
| | 55488 | 448 | | fval |= Sp1[(work >> 24) & 0x3f]; |
| | 55488 | 449 | | work = left ^ (uint)wKey[(round * 4) + 3]; |
| | 55488 | 450 | | fval |= Sp8[work & 0x3f]; |
| | 55488 | 451 | | fval |= Sp6[(work >> 8) & 0x3f]; |
| | 55488 | 452 | | fval |= Sp4[(work >> 16) & 0x3f]; |
| | 55488 | 453 | | fval |= Sp2[(work >> 24) & 0x3f]; |
| | 55488 | 454 | | right ^= fval; |
| | 55488 | 455 | | } |
| | | 456 | | |
| | 6936 | 457 | | right = (right << 31) | (right >> 1); |
| | 6936 | 458 | | work = (left ^ right) & 0xaaaaaaaa; |
| | 6936 | 459 | | left ^= work; |
| | 6936 | 460 | | right ^= work; |
| | 6936 | 461 | | left = (left << 31) | (left >> 1); |
| | 6936 | 462 | | work = ((left >> 8) ^ right) & 0x00ff00ff; |
| | 6936 | 463 | | right ^= work; |
| | 6936 | 464 | | left ^= work << 8; |
| | 6936 | 465 | | work = ((left >> 2) ^ right) & 0x33333333; |
| | 6936 | 466 | | right ^= work; |
| | 6936 | 467 | | left ^= work << 2; |
| | 6936 | 468 | | work = ((right >> 16) ^ left) & 0x0000ffff; |
| | 6936 | 469 | | left ^= work; |
| | 6936 | 470 | | right ^= work << 16; |
| | 6936 | 471 | | work = ((right >> 4) ^ left) & 0x0f0f0f0f; |
| | 6936 | 472 | | left ^= work; |
| | 6936 | 473 | | right ^= work << 4; |
| | | 474 | | |
| | 6936 | 475 | | Pack.UInt32ToBigEndian(right, outBytes, outOff); |
| | 6936 | 476 | | Pack.UInt32ToBigEndian(left, outBytes, outOff + 4); |
| | 6936 | 477 | | } |
| | | 478 | | } |
| | | 479 | | } |