Changeset 2075
 Timestamp:
 Nov 10, 2010, 8:43:55 PM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/CrypPlugins/NativeCryptography/NativeCryptography.cpp
r2028 r2075 148 148 array<unsigned char>^ Crypto::encryptAESorDES(array<unsigned char>^ Input, array<unsigned char>^ Key, array<unsigned char>^ IV, const int bits, const int length, const int mode, const int blockSize, const cryptMethod method) 149 149 { 150 int numBlocks = length / blockSize; 150 int numBlocks = length / blockSize; 151 151 if (length % blockSize != 0) 152 numBlocks++;152 throw gcnew System::Exception("Input must be multiple of " + blockSize); 153 153 154 154 bool noIV = false; … … 172 172 pin_ptr<unsigned char> outp = &output[0]; 173 173 174 array<unsigned char>^ block = nullptr; 175 pin_ptr<unsigned char> blockp = nullptr; 176 if (mode == 1) 177 { 178 block = gcnew array<unsigned char>(blockSize); 179 blockp = &block[0]; 180 } 181 174 182 AES_KEY aeskey; 175 183 DES_key_schedule deskey; 176 184 if (mode == 2) //CFB 177 185 { 178 unsigned char block[16]; //16 is enough for AES and DES 179 unsigned char shiftregister[16]; 180 //works only for little endian architectures: 181 if (blockSize == 8) 182 { 183 *((unsigned int*)shiftregister) = *((unsigned int*)&iv[1]); 184 *((unsigned int*)&shiftregister[4]) = (*((unsigned int*)&iv[4]) >> 8)  ((unsigned int)(input[0]) << 24); 185 } 186 else if (blockSize == 16) 187 { 188 *((unsigned int*)shiftregister) = *((unsigned int*)&iv[1]); 189 *((unsigned int*)&shiftregister[4]) = (*((unsigned int*)&iv[4]) >> 8)  ((unsigned int)iv[8] << 24); 190 *((unsigned int*)&shiftregister[8]) = (*((unsigned int*)&iv[8]) >> 8)  ((unsigned int)iv[12] << 24); 191 *((unsigned int*)&shiftregister[12]) = (*((unsigned int*)&iv[12]) >> 8)  ((unsigned int)input[0] << 24); 192 } 193 else 194 return nullptr; 195 186 throw gcnew System::Exception("Encrypting CFB not supported (yet?)"); 187 } 188 else //CBC or ECB 189 { 196 190 if (method == cryptMethod::methodAES) 197 191 AES_set_encrypt_key(key, bits, &aeskey); 198 192 else 199 193 DES_set_key_unchecked((const_DES_cblock*)key, &deskey); 200 201 encrypt(iv, block, method, &aeskey, &deskey); 202 unsigned char leftmost = block[0]; 203 outp[0] = leftmost ^ input[0]; 204 205 for (int i = 1; i < length; i++) 206 { 207 encrypt(shiftregister, block, method, &aeskey, &deskey); 208 leftmost = block[0]; 209 outp[i] = leftmost ^ input[i]; 210 211 //shift input[i] in register: 212 if (blockSize == 8) 194 195 if (mode == 1 && !noIV) //CBC 196 { 197 for (int d = 0; d < blockSize; d++) 198 block[d] = input[d]; 199 xorblock(blockp, iv, method); 200 encrypt(blockp, outp, method, &aeskey, &deskey); 201 } 202 else 203 encrypt(input, outp, method, &aeskey, &deskey); 204 205 for (int c = 1; c < numBlocks; c++) 206 { 207 if (mode == 1) //CBC 213 208 { 214 *((unsigned int*)shiftregister) = *((unsigned int*)&shiftregister[1]); 215 *((unsigned int*)&shiftregister[4]) = (*((unsigned int*)&shiftregister[4]) >> 8)  ((unsigned int)input[i] << 24); 209 for (int d = 0; d < blockSize; d++) 210 block[d] = input[c*blockSize+d]; 211 xorblock(blockp, outp+(c1)*blockSize, method); 212 encrypt(blockp, outp+c*blockSize, method, &aeskey, &deskey); 216 213 } 217 else if (blockSize == 16) 218 { 219 *((unsigned int*)shiftregister) = *((unsigned int*)&shiftregister[1]); 220 *((unsigned int*)&shiftregister[4]) = (*((unsigned int*)&shiftregister[4]) >> 8)  ((unsigned int)shiftregister[8] << 24); 221 *((unsigned int*)&shiftregister[8]) = (*((unsigned int*)&shiftregister[8]) >> 8)  ((unsigned int)shiftregister[12] << 24); 222 *((unsigned int*)&shiftregister[12]) = (*((unsigned int*)&shiftregister[12]) >> 8)  ((unsigned int)input[i] << 24); 223 } 224 } 225 } 226 else //CBC or ECB 227 { 228 if (method == cryptMethod::methodAES) 229 AES_set_encrypt_key(key, bits, &aeskey); 230 else 231 DES_set_key_unchecked((const_DES_cblock*)key, &deskey); 232 233 encrypt(input, outp, method, &aeskey, &deskey); 234 if (mode == 1 && !noIV) //CBC 235 xorblock(outp, iv, method); 236 for (int c = 1; c < numBlocks; c++) 237 { 238 encrypt(input+c*blockSize, outp+c*blockSize, method, &aeskey, &deskey); 239 if (mode == 1) //CBC 240 xorblock(outp+c*blockSize, input+(c1)*blockSize, method); 214 else 215 encrypt(input+c*blockSize, outp+c*blockSize, method, &aeskey, &deskey); 241 216 } 242 217 }
Note: See TracChangeset
for help on using the changeset viewer.