Changeset 1196 for trunk/NativeCryptography/NativeCryptography.cpp
 Timestamp:
 Feb 26, 2010, 2:46:36 AM (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/NativeCryptography/NativeCryptography.cpp
r1174 r1196 3 3 #include "NativeCryptography.h" 4 4 5 // This is the main DLL file. 5 namespace NativeCryptography { 6 6 7 #include "NativeCryptography.h" 7 /* Fast way to xor an AES block */ 8 void Crypto::xorBlockAES(int *t1, int *t2) 9 { 10 t1[0] ^= t2[0]; 11 t1[1] ^= t2[1]; 12 t1[2] ^= t2[2]; 13 t1[3] ^= t2[3]; 14 } 8 15 16 /* Fast way to xor an DES block */ 17 void Crypto::xorBlockDES(int *t1, int *t2) 18 { 19 t1[0] ^= t2[0]; 20 t1[1] ^= t2[1]; 21 } 22 23 void Crypto::encrypt(unsigned char* in, unsigned char* out, const cryptMethod method, AES_KEY* aeskey, DES_key_schedule* deskey) 24 { 25 if (method == cryptMethod::methodAES) 26 AES_encrypt(in, out, aeskey); 27 else 28 DES_ecb_encrypt((const_DES_cblock*)in, (const_DES_cblock*)out, deskey, DES_ENCRYPT); 29 } 30 31 void Crypto::decrypt(unsigned char* in, unsigned char* out, const cryptMethod method, AES_KEY* aeskey, DES_key_schedule* deskey) 32 { 33 if (method == cryptMethod::methodAES) 34 AES_decrypt(in, out, aeskey); 35 else 36 DES_ecb_encrypt((const_DES_cblock*)in, (const_DES_cblock*)out, deskey, DES_DECRYPT); 37 } 38 39 40 void Crypto::xorblock(unsigned char* t1, unsigned char* t2, const cryptMethod method) { 41 if (method == cryptMethod::methodAES) 42 xorBlockAES((int*)t1, (int*)t2); 43 else 44 xorBlockDES((int*)t1, (int*)t2); 45 } 46 47 array<unsigned char>^ Crypto::decryptAESorDES(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) 48 { 49 int numBlocks = length / blockSize; 50 if (length % blockSize != 0) 51 numBlocks++; 52 53 if (IV == nullptr) 54 IV = gcnew array<unsigned char>(blockSize); 55 56 pin_ptr<unsigned char> input = &Input[0]; 57 pin_ptr<unsigned char> key = &Key[0]; 58 pin_ptr<unsigned char> iv = &IV[0]; 59 60 array<unsigned char>^ output = gcnew array<unsigned char>(length); 61 pin_ptr<unsigned char> outp = &output[0]; 62 63 AES_KEY aeskey; 64 DES_key_schedule deskey; 65 if (mode == 2) //CFB 66 { 67 if (method == cryptMethod::methodAES) 68 AES_set_encrypt_key(key, bits, &aeskey); 69 else 70 DES_set_key_unchecked((const_DES_cblock*)key, &deskey); 71 72 encrypt(iv, outp, method, &aeskey, &deskey); 73 xorblock(outp, input, method); 74 75 for (int c = 0; c < numBlocks1; c++) 76 { 77 encrypt(input+c*blockSize, outp+(c+1)*blockSize, method, &aeskey, &deskey); 78 xorblock(outp+(c+1)*blockSize, input+(c+1)*blockSize, method); 79 } 80 } 81 else //CBC or ECB 82 { 83 if (method == cryptMethod::methodAES) 84 AES_set_decrypt_key(key, bits, &aeskey); 85 else 86 DES_set_key_unchecked((const_DES_cblock*)key, &deskey); 87 88 decrypt(input, outp, method, &aeskey, &deskey); 89 if (mode == 1) //CBC 90 xorblock(outp, iv, method); 91 for (int c = 1; c < numBlocks; c++) 92 { 93 decrypt(input+c*blockSize, outp+c*blockSize, method, &aeskey, &deskey); 94 if (mode == 1) //CBC 95 xorblock(outp+c*blockSize, input+(c1)*blockSize, method); 96 } 97 } 98 99 return output; 100 } 101 102 }
Note: See TracChangeset
for help on using the changeset viewer.