利用openssl的AES加密解密数据明文(ECB模式)

来源:互联网 发布:生化奇兵3:无限知乎 编辑:程序博客网 时间:2024/06/05 08:52
#include<windows.h>#include <stdio.h>#include <stdlib.h>#include <openssl/rsa.h>#include <openssl/pem.h>#include <openssl/aes.h>#include<iostream>using namespace std;#pragma comment(lib,"libssl.lib")#pragma comment(lib,"libcrypto.lib")#define code1024 117#define code512  53char g_pPubFile[30] = { 0 };char g_pPriFile[30] = { 0 };//maxCodeByte = g_nBits/8-11这里设置了2048,意味着最多可以 编、解码  2048/8-11=117个字节const int g_nBits = 1024;using namespace std;bool ReadTxt(unsigned char(&buf)[3200]){FILE *fp;if ((fp = fopen("1.txt", "rb")) == NULL){return false;}fseek(fp, 0, SEEK_END);int fileLen = ftell(fp);char *tmp = new char[fileLen];fseek(fp, 0, SEEK_SET);fread(tmp, fileLen, sizeof(char), fp);fclose(fp);memcpy(buf, tmp, fileLen);delete tmp;return true;}bool WriteTxt(unsigned char(&Decbuf)[3200]){FILE *fp;if ((fp = fopen("2.txt", "wb")) == NULL){return false;}rewind(fp);int  fileLen = strlen((char*)Decbuf);fwrite(Decbuf, fileLen, sizeof(char), fp);fclose(fp);}////////////////////RSA功能函数部分/////////////////////////////************************************// function:  产生公私钥对,并保存为pem格式的文件。用来后续的加密解密//************************************int MakeKey(const char* publicName, const char* privateName){//产生一个模为bits位的 密钥对,e为公开的加密指数,一般为65537(0x10001即 RSA_F4)//其它两个参数可以设置为NULLchar* g_pPub = "public.pem";char* g_pPri = "private.pem";sprintf(g_pPubFile, "%s%s", publicName, g_pPub);sprintf(g_pPriFile, "%s%s", privateName, g_pPri);RSA *pRsa = RSA_generate_key(g_nBits, RSA_F4, NULL, NULL);if (pRsa == NULL){cout << "rsa_generate_key error" << endl;return -1;}//////////////////////////////////1.开始生成公钥pem文件BIO *pBio = BIO_new_file(g_pPubFile, "wb");//该函数根据给定的mode类型创建了一个文件BIO,mode参数跟fopen函数中mode参数的含义是一样的if (pBio == NULL){cout << "BIO_new_file " << g_pPubFile << " error" << endl;return -2;}if (PEM_write_bio_RSAPublicKey(pBio, pRsa) == 0)//输出RSAPublicKey公钥到pem文件中{cout << "write public key error" << endl;return -3;}BIO_free_all(pBio);// 公钥文件生成成功,释放资源     ////////////////////////////////   //2.开始生成私钥pem文件pBio = BIO_new_file(g_pPriFile, "w");if (pBio == NULL){cout << "BIO_new_file " << g_pPriFile << " error" << endl;return -4;}if (PEM_write_bio_RSAPrivateKey(pBio, pRsa, NULL, NULL, 0, NULL, NULL) == 0)////输出RSAPrivateKey私钥到pem文件中{cout << "write private key error" << endl;return -5;}BIO_free_all(pBio);RSA_free(pRsa);return 0;}//************************************// function:  使用公钥对字符串进行加密//************************************int Enc(unsigned char *infile, int inLen, unsigned char *out, int &outLen){BIO *pBio = BIO_new_file(g_pPubFile, "r");RSA *pRsa = PEM_read_bio_RSAPublicKey(pBio, NULL, NULL, NULL);BIO_free_all(pBio);outLen = RSA_public_encrypt((RSA_size(pRsa) - 11) > inLen ? inLen : RSA_size(pRsa) - 11,infile,out,pRsa,RSA_PKCS1_PADDING);RSA_free(pRsa);if (outLen >= 0) { return 0; }return -1;}//************************************// function:  用私钥解密//************************************int Dec(unsigned char *infile, int inLen, unsigned char *out, int &outLen){BIO *pBio = BIO_new_file(g_pPriFile, "r");RSA *pRsa = PEM_read_bio_RSAPrivateKey(pBio, NULL, NULL, NULL);BIO_free_all(pBio);outLen = RSA_private_decrypt(inLen,infile,out,pRsa,RSA_PKCS1_PADDING);RSA_free(pRsa);if (outLen >= 0)return 0;return -1;}//////////////////////////AES功能部分///////////////////////////////////////////////////////////AES的ECB方式数据加密void AES_ECB_enc(unsigned char(&aes_keybuf)[32+1], unsigned char(&buf)[3200], unsigned char(&Encbuf)[3200]){AES_KEY aeskey;//设定加密用的KeyAES_set_encrypt_key(aes_keybuf, 256, &aeskey);//数据加密for (int i = 0; i < sizeof(buf); i += 16){AES_encrypt(buf + i, Encbuf + i, &aeskey);}//cout << "加密之后:" << Encbuf << endl << endl;}///////////////////////////////////AES的ECB方式数解密void AES_ECB_dec(unsigned char(&rsa_keybuf)[32+1], unsigned char(&buf)[3200], unsigned char(&Encbuf)[3200], unsigned char(&Decbuf)[3200]){AES_KEY rsa_aeskey;//设定解密用的KeyAES_set_decrypt_key(rsa_keybuf, 256, &rsa_aeskey);//数据解密for (int i = 0; i < sizeof(buf); i += 16){AES_decrypt(Encbuf + i, Decbuf + i, &rsa_aeskey);}//cout << "解密之后:" << Decbuf << endl << endl;}///////////////////////////////////////////////////////////////AES的ECB方式主函数int main(int argc, char **argv){// 产生 AES 256-bit 密码 unsigned char aes_keybuf[32+1]=")*^&$*kdjfkdjfjdsjfska!*&^%$#@ab";//memset(aes_keybuf, 0xbc, sizeof(aes_keybuf));cout << "AES 256-bit key:" << aes_keybuf << endl << endl;unsigned char buf[3200] = {0};if (false == ReadTxt(buf)){cout << "源文件无法读取" << endl;system("pause");return 0;}//cout << "原数据:" << buf << endl << endl;unsigned char Encbuf[3200];//AES加密数据unsigned char Decbuf[3200];//AES解密数据memset(Encbuf, 0, sizeof(Encbuf));memset(Decbuf, 0, sizeof(Decbuf));unsigned char rsaEnc[500] = { 0 };//rsa处理后的加密keyint nEncLen = 0;unsigned char rsaDec[500] = { 0 };//rsa处理后的解密keyint nDecLen = 0;//生成RSA公钥私钥pem文件MakeKey("2", "2");//rsa公钥去加密keyEnc(aes_keybuf, strlen((char*)aes_keybuf), rsaEnc, nEncLen);//cout << "RSA加密处理后:" << endl << szEnc << endl << endl;//rsa私钥去解密keyDec(rsaEnc, nEncLen, rsaDec, nDecLen);//cout << "RSA解密处理后: " << szDec << endl << endl;AES_ECB_enc(aes_keybuf, buf, Encbuf);unsigned char rsa_keybuf[32+1] = { 0 };//memcpy( rsa_keybuf, rsaDec, 32*sizeof(unsigned char) );memcpy(rsa_keybuf, rsaDec,strlen((char*)rsaDec));AES_ECB_dec(rsa_keybuf,buf, Encbuf, Decbuf);//cout << "解密之后:" << Decbuf << endl;if (memcmp(buf, Decbuf, sizeof(buf)) == 0)cout << "test success\r\n";elsecout << "test fail\r\n";WriteTxt(Decbuf);system("pause");return 1;}

原创粉丝点击