#include <gcrypt.h>
#include <stdio.h>
#include <stdlib.h>
// gcc test.c -lstdc++ -lgcrypt -I/local/include -L/local/lib -o testout
//需要传入的值有:明文密码,盐值,迭代次数,矢量
int main(int argc, char **argv){
/*FILE* fin=fopen("hi.txt","rb");
FILE*fout=fopen("hien.txt","wb");*/
FILE* fin=fopen("hien.txt","rb");
FILE*fout=fopen("hienout.txt","wb");
//1.得到文件大小
fseek(fin, 0, SEEK_END);
int file_size=ftell(fin);
fseek(fin, 0, SEEK_SET);
printf("filesize=%d",file_size);
//2.明文密码加密
//
char passphrase[20]="123456";//明文秘钥
char salt[15]="*!1223*";//盐值
char retkey[100]={0};//最后返回的秘钥
int retkey_bufferLen=100;//这个大小值随便写的
gpg_error_t err = gcry_kdf_derive(passphrase, strlen(passphrase),
GCRY_KDF_PBKDF2, GCRY_MD_SHA512,
salt, sizeof(salt),
3,
retkey_bufferLen, retkey);
//3.得到某一加密算法的 参数
size_t key_size = gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES256);
size_t block_size = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES256);
size_t block_required=file_size/block_size;
//4.根据上面的参数,准备好相应的buffer
printf("key_size=%d,block_size=%d,block_required=%d\n",key_size,block_size,block_required);
if (file_size % block_size != 0){
block_required++;
}
//4.1准备工作,,把相应参数都创建好
//根据参数创建矢量
char *iv=new char[block_size];
memset(iv,0,block_size);
memcpy(iv,"abcdef",sizeof("abcdef"));// 任意值
gcry_cipher_hd_t cipher_hd;
//创建操作句柄
gcry_error_t cipher_err=gcry_cipher_open(&cipher_hd, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_CTS);
//设置传入的密码要
cipher_err=gcry_cipher_setkey(cipher_hd,retkey,key_size);//密码的大小是获取的参数
//设置传入的矢量
cipher_err=gcry_cipher_setiv(cipher_hd, iv, block_size);
//4.2把文件内容读入恰当的buf中
printf("set bufffer\n");
char *input_buf = (char*)malloc(file_size);
char *cipher_buffer = (char*)malloc(block_size*block_required);
memset(cipher_buffer, 0, block_size*block_required);
int fLen=fread(input_buf,1,file_size,fin);
printf("*******************readData=%s,readLen=%d\n",input_buf,fLen);
//将数据读入输入缓存
memcpy(cipher_buffer,input_buf,block_required*block_size);
//5.调用加密,解密函数
//加密
//cipher_err=gcry_cipher_encrypt(cipher_hd,cipher_buffer, block_required*block_size,NULL,0);
//解密
cipher_err=gcry_cipher_decrypt(cipher_hd,cipher_buffer, block_required*block_size,NULL,0);
//5.1写入输出文件
fwrite(cipher_buffer,1,block_required*block_size,fout);
gcry_cipher_close(cipher_hd);//关闭句柄
fclose(fin);
fclose(fout);
}
#include <stdio.h>
#include <stdlib.h>
// gcc test.c -lstdc++ -lgcrypt -I/local/include -L/local/lib -o testout
//需要传入的值有:明文密码,盐值,迭代次数,矢量
int main(int argc, char **argv){
/*FILE* fin=fopen("hi.txt","rb");
FILE*fout=fopen("hien.txt","wb");*/
FILE* fin=fopen("hien.txt","rb");
FILE*fout=fopen("hienout.txt","wb");
//1.得到文件大小
fseek(fin, 0, SEEK_END);
int file_size=ftell(fin);
fseek(fin, 0, SEEK_SET);
printf("filesize=%d",file_size);
//2.明文密码加密
//
char passphrase[20]="123456";//明文秘钥
char salt[15]="*!1223*";//盐值
char retkey[100]={0};//最后返回的秘钥
int retkey_bufferLen=100;//这个大小值随便写的
gpg_error_t err = gcry_kdf_derive(passphrase, strlen(passphrase),
GCRY_KDF_PBKDF2, GCRY_MD_SHA512,
salt, sizeof(salt),
3,
retkey_bufferLen, retkey);
//3.得到某一加密算法的 参数
size_t key_size = gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES256);
size_t block_size = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES256);
size_t block_required=file_size/block_size;
//4.根据上面的参数,准备好相应的buffer
printf("key_size=%d,block_size=%d,block_required=%d\n",key_size,block_size,block_required);
if (file_size % block_size != 0){
block_required++;
}
//4.1准备工作,,把相应参数都创建好
//根据参数创建矢量
char *iv=new char[block_size];
memset(iv,0,block_size);
memcpy(iv,"abcdef",sizeof("abcdef"));// 任意值
gcry_cipher_hd_t cipher_hd;
//创建操作句柄
gcry_error_t cipher_err=gcry_cipher_open(&cipher_hd, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_CTS);
//设置传入的密码要
cipher_err=gcry_cipher_setkey(cipher_hd,retkey,key_size);//密码的大小是获取的参数
//设置传入的矢量
cipher_err=gcry_cipher_setiv(cipher_hd, iv, block_size);
//4.2把文件内容读入恰当的buf中
printf("set bufffer\n");
char *input_buf = (char*)malloc(file_size);
char *cipher_buffer = (char*)malloc(block_size*block_required);
memset(cipher_buffer, 0, block_size*block_required);
int fLen=fread(input_buf,1,file_size,fin);
printf("*******************readData=%s,readLen=%d\n",input_buf,fLen);
//将数据读入输入缓存
memcpy(cipher_buffer,input_buf,block_required*block_size);
//5.调用加密,解密函数
//加密
//cipher_err=gcry_cipher_encrypt(cipher_hd,cipher_buffer, block_required*block_size,NULL,0);
//解密
cipher_err=gcry_cipher_decrypt(cipher_hd,cipher_buffer, block_required*block_size,NULL,0);
//5.1写入输出文件
fwrite(cipher_buffer,1,block_required*block_size,fout);
gcry_cipher_close(cipher_hd);//关闭句柄
fclose(fin);
fclose(fout);
}