uint32_t decrypt_with_mode(const BYTE *password, const BYTE* data, uint32_t len, BYTE* ans, encrypt_mode mode, encrypt_method method) { uint32_t outl; switch (mode) { case AES128: outl = decrypt_aes128(password, data, len, ans, method); break; case AES192: outl = decrypt_aes192(password, data, len, ans, method); break; case AES256: outl = decrypt_aes256(password, data, len, ans, method); break; case DES: outl = decrypt_des(password, data, len, ans, method); break; } return outl; }
int main() { DES_cblock cb1 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE }; DES_cblock cb2 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE }; DES_cblock cb3 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE }; DES_key_schedule ks1,ks2,ks3; DES_cblock cblock = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; char string[] = "I am a software developer, bbbxxxxxxxxxx, gjeogjeogjeojsb"; int ret = -1; // --------------------------------------------- // I use sizeof instead of strlen because I want // to count the '\0' at the end, strlen would // not count it int stringLen = strlen(string) + 1; int cipherLen = stringLen *2; printf("Plain Text : %s\n",string); char *cipher = (char*)malloc(cipherLen); char *text = (char*)malloc(stringLen); memset(cipher, 0, cipherLen); memset(text,0,stringLen); DES_set_odd_parity(&cblock); if (DES_set_key_checked(&cb1, &ks1) || DES_set_key_checked(&cb2, &ks2) || DES_set_key_checked(&cb3, &ks3)) { printf("Key error, exiting ....\n"); return 1; } // ks1的值是根据解密是的数据变化的,所以该方法失败。 ret = encrypt_des(&cblock, &ks1, &ks2, &ks3, cipher, string, stringLen); if (ret < 0) { return ret; } ret = decrypt_des(cipher, text, &ks1, &ks2, &ks3, &cblock, cipherLen); if (ret < 0) { return ret; } return 0; }
static void des_test_withmode(int mode) { DEBUG("\n\n== des_test_withmode (%s) ==\n", des_getmode(mode)); char *testData = des_padding(gTestData0); if (testData == NULL) return NULL; size_t out_len = 0; char *encrypted = encrypt_des((const char*)testData, strlen(testData), gPassword, mode, &out_len); if (encrypted != NULL) { des_dump(encrypted, out_len); char *plain = decrypt_des(encrypted, out_len, gPassword, mode, &out_len); if (plain != NULL) { DEBUG("%s : plain text =\n%s \n", __func__, plain); if (strncmp(plain, testData, strlen(testData)) == 0) { DEBUG("%s : Success \n", __func__); } else { DEBUG("%s : Fail \n", __func__); } DO_FREE(plain); } DO_FREE(encrypted); } DO_FREE(testData); }