crypto

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值