我刚开始接触AES,问题很弱,希望达人体谅并不吝赐教,谢谢了!
根据openssl中的AES相关函数,我编写的main函数如下,但经过加密、解密之后得到的不是原来的明文,不知哪里用错了:
#include <openssl/aes.h>
#include "aes.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "aes_locl.h"int main(void)
{
// parameter in AES_set_encrypt_key
unsigned char userKey[16];
unsigned char key0='a';
int bits=128;
AES_KEY *key;
AES_KEY key_stack;// parameter in AES_cbc_encrypt
unsigned char *in;
unsigned char in_stack[16];
unsigned char *out;
unsigned char out_stack[64];
unsigned long length=16;
int i,j; //循环输入输出时使用
int enc=1;
unsigned char *ivec;
unsigned char ivec_stack[16];
errno_t err;
// file operation
FILE *fp,*fp_o;
char *filename="data.txt",*outfile="outdata.txt";//前者存明文,后者存密文
if(err=fopen_s(&fp,filename,"r")!=0)
{
printf("Error: can not open %s",*filename);
}
fseek(fp, 0, SEEK_SET);
i=0;
//读入data.txt中的数据到in_stack数组中
do
{
in_stack[i]=fgetc(fp);
} while ((char)in_stack[i++]!=EOF);
fclose(fp);
i-=2;
//设置密钥种子
userKey[0]='0';
userKey[1]='0';
userKey[2]='1';
userKey[3]='1';
userKey[4]='2';
userKey[5]='2';
userKey[6]='3';
userKey[7]='3';
userKey[8]='4';
userKey[9]='4';
userKey[10]='5';
userKey[11]='5';
userKey[12]='6';
userKey[13]='6';
userKey[14]='7';
userKey[15]='7';//把数组和指针对应
in=in_stack;
out=out_stack;
key=&key_stack;
ivec=ivec_stack;//生成加密密钥表
if(AES_set_encrypt_key(userKey,bits,key)==0)
printf("key has been set!\n");//加密
AES_cbc_encrypt(in, out,length, key,ivec,enc);
//输出、保存密文
if(err=fopen_s(&fp_o,outfile,"w")!=0)
printf("cannot open output file");
fseek(fp_o, 0, SEEK_SET);
j=0;
do
{
printf("%2x",out_stack[j]);
// putchar(out_stack[j]);
fputc(out_stack[j++],fp_o);
} while (j<(i+1));
fputc(EOF,fp_o); printf("the encrypt data has been saved\n");
// 转换到解密状态
enc=0;
in=out_stack; //out_stack存的是密文,解密时作为输入
out=in_stack;
//设置解密密钥表
if(AES_set_decrypt_key(userKey,bits,key)==0)
printf("the decrypt key has been set");
//enc=0进行解密操作
AES_cbc_encrypt(in, out,length, key,ivec,enc); return 0;
}结果得到的out中的数据不是原来的明文。。哪位能帮我看看吗?谢谢了!!!
另外,自己写的代码,可能有比较傻的语句,也请大家随便拍!
根据openssl中的AES相关函数,我编写的main函数如下,但经过加密、解密之后得到的不是原来的明文,不知哪里用错了:
#include <openssl/aes.h>
#include "aes.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "aes_locl.h"int main(void)
{
// parameter in AES_set_encrypt_key
unsigned char userKey[16];
unsigned char key0='a';
int bits=128;
AES_KEY *key;
AES_KEY key_stack;// parameter in AES_cbc_encrypt
unsigned char *in;
unsigned char in_stack[16];
unsigned char *out;
unsigned char out_stack[64];
unsigned long length=16;
int i,j; //循环输入输出时使用
int enc=1;
unsigned char *ivec;
unsigned char ivec_stack[16];
errno_t err;
// file operation
FILE *fp,*fp_o;
char *filename="data.txt",*outfile="outdata.txt";//前者存明文,后者存密文
if(err=fopen_s(&fp,filename,"r")!=0)
{
printf("Error: can not open %s",*filename);
}
fseek(fp, 0, SEEK_SET);
i=0;
//读入data.txt中的数据到in_stack数组中
do
{
in_stack[i]=fgetc(fp);
} while ((char)in_stack[i++]!=EOF);
fclose(fp);
i-=2;
//设置密钥种子
userKey[0]='0';
userKey[1]='0';
userKey[2]='1';
userKey[3]='1';
userKey[4]='2';
userKey[5]='2';
userKey[6]='3';
userKey[7]='3';
userKey[8]='4';
userKey[9]='4';
userKey[10]='5';
userKey[11]='5';
userKey[12]='6';
userKey[13]='6';
userKey[14]='7';
userKey[15]='7';//把数组和指针对应
in=in_stack;
out=out_stack;
key=&key_stack;
ivec=ivec_stack;//生成加密密钥表
if(AES_set_encrypt_key(userKey,bits,key)==0)
printf("key has been set!\n");//加密
AES_cbc_encrypt(in, out,length, key,ivec,enc);
//输出、保存密文
if(err=fopen_s(&fp_o,outfile,"w")!=0)
printf("cannot open output file");
fseek(fp_o, 0, SEEK_SET);
j=0;
do
{
printf("%2x",out_stack[j]);
// putchar(out_stack[j]);
fputc(out_stack[j++],fp_o);
} while (j<(i+1));
fputc(EOF,fp_o); printf("the encrypt data has been saved\n");
// 转换到解密状态
enc=0;
in=out_stack; //out_stack存的是密文,解密时作为输入
out=in_stack;
//设置解密密钥表
if(AES_set_decrypt_key(userKey,bits,key)==0)
printf("the decrypt key has been set");
//enc=0进行解密操作
AES_cbc_encrypt(in, out,length, key,ivec,enc); return 0;
}结果得到的out中的数据不是原来的明文。。哪位能帮我看看吗?谢谢了!!!
另外,自己写的代码,可能有比较傻的语句,也请大家随便拍!
aes解密,先解密,再异或
是你写的还是系统提供的?
请问你说的需要异或的是明文还是ivec啊?
加密和解密之前的ivec需要什么预处理吗?谢谢~
谢谢你这么细心~~
不过加密还是解密操作是根据enc的值来决定的,1为加密0为解密~