-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptoDemo.cpp
More file actions
79 lines (70 loc) · 2.13 KB
/
cryptoDemo.cpp
File metadata and controls
79 lines (70 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// cryptoDemo.cpp : Defines the entry point for the console application.
// Windows: cl cryptoDemo.cpp
// Linux: gcc -o cryptoDemo cryptoDemo.cpp -lcrypto
#include <memory.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "openssl/aes.h"
#pragma comment(lib,"libeay32.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 aes_keybuf[32];
AES_KEY aeskey;
// 准备32字节(256位)的AES密码字节
memset(aes_keybuf,0x90,32);
if(pwdLen<32){ len=pwdLen; } else { len=32;}
for(i=0;i<len;i++) aes_keybuf[i]=passwd[i];
// 输入字节串分组成16字节的块
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(int argc, char* argv[])
{
char inString[] = "This is a sample. I am a programer.\n";
char passwd[] = "0123456789ABCDEFGHIJK";
testAes(inString, strlen(inString), passwd, strlen(passwd));
return 0;
}