#include
#include<memory.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"openssl\aes.h"
#pragma comment(lib,“libssl.lib”)
#pragma comment(lib,“libcrypto.lib”)
void testAes(char inString[], int inLen, char passwd[], int pwdLen)
{
int i, j, len, nLoop, nRes; char enString[1024]; char deString[1024];
unsigned char buf[16]; unsigned char buf2[16]; unsigned char buf3[16];
unsigned char aes_keybuf[32]; AES_KEY aeskey;
memset(aes_keybuf, 0x90, 32);
if (pwdLen < 32) { len = pwdLen; }
else { len = 32; }
for (i = 0; i < len; i++) aes_keybuf[i] = passwd[i];
nLoop = inLen / 16; nRes = inLen % 16;
AES_set_encrypt_key(aes_keybuf, 256, &aeskey);
for (i = 0; i < nLoop; i++)
{
memset(buf, 0, 16);
for (j = 0; j < 16; j++)buf[j] = inString[i * 16 + j];
AES_encrypt(buf, buf2, &aeskey);
for (j = 0; j < 16; j++) enString[i * 16 + j] = buf2[j];
}
if(nRes>0)
{
memset(buf, 0, 16);
for (j = 0; j < nRes; j++)buf[j] = inString[i * 16 + j];
AES_encrypt(buf, buf2, &aeskey);
for (j = 0; j < 16; j++) enString[i * 16 + j] = buf2[j];
puts(“encrypt”);
}
enString[i * 16 + j] = 0;
AES_set_decrypt_key(aes_keybuf, 256, &aeskey);
for (i = 0; i < nLoop; i++)
{
memset(buf, 0, 16);
for (j = 0; j < 16; j++)buf[j] = enString[i * 16 + j];
AES_decrypt(buf, buf2, &aeskey);
for (j = 0; j < 16; j++) deString[i * 16 + j] = buf2[j];
}
if (nRes > 0)
{
memset(buf, 0, 16);
for (j = 0; j < 16; j++)buf[j] = enString[i * 16 + j];
AES_decrypt(buf, buf2, &aeskey);
for (j = 0; j < 16; j++) deString[i * 16 + j] = buf2[j];
puts(“decrypt”);
}
deString(i * 16 + nRes) = 0;
if (memcmp(inString, deString, strlen(inString)) == 0)
{
printf(“test success\r\n”);
}
else { printf(“test fail\r\n”); }
printf(“The original string is:\n %s”, inString);
printf(“The encrypted string is:\n %s”, enString);
printf(“The decrypted string is:\n %s”, deString);
}
int main()
{
char inString[] = “liuyinan”;
char passwd[] = “0123456789ABCDEFGHIJK”;
testAes(inString, strlen(inString), passwd, strlen(passwd));
return 0;
}
crypto
最新推荐文章于 2024-09-03 22:01:34 发布