- // Demo_aes.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <string>
- #include <cassert>
- #include <Windows.h>
- #include <openssl\\aes.h>
- #include <openssl\\rand.h>
- #include <openssl\\evp.h>
- #pragma comment(lib,"libeay32.lib")
- #pragma comment(lib,"ssleay32.lib")
- #define BIG_TEST_SIZE 10240
- using namespace std;
- std::string EncodeAES( /*const std::string&*/char * strPassword, const std::string& strData )
- {
- AES_KEY aes_key;
- if(AES_set_encrypt_key((const unsigned char*)strPassword,AES_BLOCK_SIZE*8/*strlen(strPassword)*8 *//*strPassword.length() * 8*/, &aes_key) < 0)
- {
- assert(false);
- return "";
- }
- std::string strRet;
- for(unsigned int i = 0; i < strData.length()/AES_BLOCK_SIZE; i++)
- {
- std::string str16 = strData.substr(i*AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- unsigned char out[AES_BLOCK_SIZE];
- AES_encrypt((const unsigned char*)str16.c_str(), out, &aes_key);
- strRet += std::string((const char*)out, AES_BLOCK_SIZE);
- }
- return strRet;
- }
- std::string EncodeAES_little( /*const std::string&*/char * strPassword, const std::string& strData )
- {
- AES_KEY aes_key;
- if(AES_set_encrypt_key((const unsigned char*)strPassword, AES_BLOCK_SIZE*8/*strlen(strPassword)*8*/ /*strPassword.length() * 8*/, &aes_key) < 0)
- {
- assert(false);
- return "";
- }
- unsigned char out[AES_BLOCK_SIZE];
- AES_encrypt((const unsigned char*)strData.c_str(), out, &aes_key);
- return std::string((const char*)out);
- }
- std::string EncodeAES_Big( /*const std::string&*/char * strPassword, const std::string& strData )
- {
- AES_KEY aes_key;
- if(AES_set_encrypt_key((const unsigned char*)strPassword,AES_BLOCK_SIZE*8/*strlen(strPassword)*8 *//*strPassword.length() * 8*/, &aes_key) < 0)
- {
- assert(false);
- return "";
- }
- std::string strRet;
- unsigned int i = 0;
- std::string str16;
- unsigned char out[AES_BLOCK_SIZE];
- for(; i < strData.length()/AES_BLOCK_SIZE; i++)
- {
- str16 = strData.substr(i*AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- AES_encrypt((const unsigned char*)str16.c_str(), out, &aes_key);
- strRet += std::string((const char*)out, AES_BLOCK_SIZE);
- }
- str16 = strData.substr(i*AES_BLOCK_SIZE, strData.length()-i*AES_BLOCK_SIZE);
- AES_encrypt((const unsigned char*)str16.c_str(), out, &aes_key);
- strRet += std::string((const char*)out, AES_BLOCK_SIZE);
- cout<<"*************:"<<str16<<endl;
- cout<<"strRet.length() = "<<strRet.length()<<endl;
- return strRet;
- }
- std::string DecodeAES( /*const std::string&*/char * strPassword, const std::string& strData )
- {
- AES_KEY aes_key;
- if(AES_set_decrypt_key((const unsigned char*)strPassword, AES_BLOCK_SIZE*8/*strlen(strPassword)*8*/ /*strPassword.length() * 8*/, &aes_key) < 0)
- {
- assert(false);
- return "";
- }
- std::string strRet;
- for(unsigned int i = 0; i < strData.length()/AES_BLOCK_SIZE; i++)
- {
- std::string str16 = strData.substr(i*AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- unsigned char out[AES_BLOCK_SIZE];
- AES_decrypt((const unsigned char*)str16.c_str(), out, &aes_key);
- strRet += std::string((const char*)out, AES_BLOCK_SIZE);
- }
- return strRet;
- }
- std::string DecodeAES_little( /*const std::string&*/char * strPassword, const std::string& strData )
- {
- AES_KEY aes_key;
- if(AES_set_decrypt_key((const unsigned char*)strPassword, AES_BLOCK_SIZE*8/*strlen(strPassword)*8*/ /*strPassword.length() * 8*/, &aes_key) < 0)
- {
- assert(false);
- return "";
- }
- unsigned char out[AES_BLOCK_SIZE];
- AES_decrypt((const unsigned char*)strData.c_str(), out, &aes_key);
- return std::string((const char*)out);
- }
- std::string DecodeAES_Big( /*const std::string&*/char * strPassword, const std::string& strData )
- {
- cout<<"strData.length() = "<<strData.length()<<endl;
- AES_KEY aes_key;
- if(AES_set_decrypt_key((const unsigned char*)strPassword, AES_BLOCK_SIZE*8/*strlen(strPassword)*8*/ /*strPassword.length() * 8*/, &aes_key) < 0)
- {
- assert(false);
- return "";
- }
- std::string strRet;
- unsigned int i = 0;
- unsigned char out[AES_BLOCK_SIZE];
- for(; i < strData.length()/AES_BLOCK_SIZE; i++)
- {
- std::string str16 = strData.substr(i*AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- AES_decrypt((const unsigned char*)str16.c_str(), out, &aes_key);
- strRet += std::string((const char*)out, AES_BLOCK_SIZE);
- }
- std::string str16 = strData.substr(i*AES_BLOCK_SIZE,strData.length()- i*AES_BLOCK_SIZE);
- AES_decrypt((const unsigned char*)str16.c_str(), out, &aes_key);
- strRet += std::string((const char*)out, AES_BLOCK_SIZE);
- return strRet;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- system("cls");
- std::string buf;
- cout<<"please input an string:"<<endl;
- getline(cin,buf);
- char userkey[AES_BLOCK_SIZE];
- //std::string userkey;
- RAND_pseudo_bytes((unsigned char*)userkey, sizeof userkey);
- std::string encrypt_data;
- std::string decrypt_data;
- if(buf.length()%16 == 0)
- {
- cout<<"16 "<<endl;
- encrypt_data = EncodeAES(userkey,buf);
- cout<<"encrypt_data:"<<endl;
- cout<<encrypt_data<<endl;
- decrypt_data = DecodeAES(userkey,encrypt_data);
- cout<<"decrypt_data:"<<endl;
- cout<<decrypt_data<<endl;
- }
- else
- {
- if(buf.length()<16)
- {
- cout<<"16 >"<<endl;
- encrypt_data = EncodeAES_little(userkey,buf);
- cout<<"encrypt_data:"<<endl;
- cout<<encrypt_data<<endl;
- decrypt_data = DecodeAES_little(userkey,encrypt_data);
- cout<<"decrypt_data:"<<endl;
- cout<<decrypt_data<<endl;
- }
- else
- {
- cout<<"16< "<<endl;
- encrypt_data = EncodeAES_Big(userkey,buf);
- cout<<"encrypt_data:"<<endl;
- cout<<encrypt_data<<endl;
- decrypt_data = DecodeAES_Big(userkey,encrypt_data);
- cout<<"decrypt_data:"<<endl;
- cout<<decrypt_data<<endl;
- }
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/160520149586.html
来源: http://www.codesnippet.cn/detail/160520149586.html