Linux AES加密算法实现

       提供SDK给别人调用时候,有时候需要一个llicense.lic,一下是一个最简单的Demo,使用AES算法进行加密,在网上找了半天没有任何头绪,也不知道该怎么做。能够想到的就是这种比较low的方法了。

1依赖

一下AES算法是在crypto++库上实现的,首先安装crypto++。去官网下载相应的源码,低版本的好像对需要4.7一下版本GCC,我这里选择的是5.65。
安装:

   make 
   make libcryptopp.so 
   make install 

安装很简单,完成之后默认头文件在/usr/local/include下,链接库在/usr/local/lib下。

2实现

我使用的是读取mac地址和系统时间,对齐加密后存入llicense.lic,使用时候解密后给别人使用就ok了。
这里读取系统时间以及mac地址见https://blog.csdn.net/ycdhqzhiai/article/details/80840392
源码如下:

/*
AES.h
*/
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

using namespace CryptoPP;
using namespace std;

class MyAES
{
public:
    byte * key;
    byte * iv;
    int key_length;

    MyAES();
    MyAES(byte * key, byte *iv, int length);
    ~MyAES();

    //use the key to encrypt the plainText and return the cipher
    string Encrypt(string plainText);
    //use the same key to decrypt the cipher and return the recover
    string Decrypt(string cipherTextHex);
    void GenerateKey();
    void SetKey(byte * key, byte * iv, int length);
};
/*
Decrypt.cpp 解密
*/
#include "AES.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

MyAES::MyAES()
{

}

MyAES::MyAES(byte * key1, byte * iv1, int key_length1)
{
    SetKey(key1, iv1, key_length1);
}

MyAES::~MyAES()
{

}

void MyAES::GenerateKey()
{
    AutoSeededRandomPool rnd;
    byte key1[AES::DEFAULT_KEYLENGTH];
    rnd.GenerateBlock(key1, AES::DEFAULT_KEYLENGTH);

    // Generate a random IV
    byte iv1[AES::BLOCKSIZE];
    rnd.GenerateBlock(iv1, AES::BLOCKSIZE);

    SetKey(key1, iv1, 16);
}

void MyAES::SetKey(byte * key1, byte * iv1, int length1)
{
    this->key = key1;
    this->iv = iv1;
    this->key_length = length1;
}

string MyAES::Encrypt(string plainText)
{
    string cipherText;

    //
    CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipherText ));
    stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plainText.c_str() ), plainText.length() + 1 );
    stfEncryptor.MessageEnd();

    string cipherTextHex;
    for( int i = 0; i < cipherText.size(); i++ )
    {
        char ch[3] = {
    0};
        sprintf(ch, "%02x",  static_cast(cipherText[i]));
        cipherTextHex += ch;
    }

    return cipherTextHex;
}

string MyAES::Decrypt(string cipherTextHex)
{
    string cipherText;
    string decryptedText;
    int i = 0;
    while(true)
    {
        char c;
        int x;
        stringstream ss;
        ss<2).c_str();
        ss>>x;
        c = (char)x;
        cipherText += c;
        if(i >= cipherTextHex.length() - 2)break;
        i += 2;
    }

    //
    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ));
    stfDecryptor.Put( reinterpret_cast<const unsigned char*>( cipherText.c_str() ), cipherText.size());

    stfDecryptor.MessageEnd();

    return decryptedText;
}
string ltos(long l)
{
    ostringstream os;
    os<string result;
    istringstream is(os.str());
    is>>result;
    return result;

}

int main() {

    byte key[]  = {
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x01, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x01};
    byte iv[]   = {
    0x01,0x02,0x03,0x03,0x03,0x03,0x03,0x03, 0x03,0x03, 0x01,0x02,0x03,0x03,0x03,0x03};

    int keysize = 16;
    std::string month = "1";    

    MyAES aes(key, iv, keysize);

    cout << "AES parameters: " << endl;
    cout << "The algorithm name is : " << AES::StaticAlgorithmName() << endl;
    cout << "The iv is : " << aes.iv << endl;
    cout << "The key is : " << aes.key << endl;
    cout << "The key length is : " << aes.key_length << endl;

    string month_cipher = aes.Encrypt(month);

    ofstream out("license.lic", ios::app);
        out.write(month_cipher.c_str(), month_cipher.length());
        out.close();

    ifstream in("license.lic");

        string line;
        string decryptedText;
        while(getline(in, line))
        {
            if(line.length() > 1)
            {
                decryptedText = aes.Decrypt(line);
            }
        cout<<"readCipher finish "<< decryptedText <return 0;
 }
/*
Encrypt.cpp 加密
*/
#include "AES.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

MyAES::MyAES()
{

}

MyAES::MyAES(byte * key1, byte * iv1, int key_length1)
{
    SetKey(key1, iv1, key_length1);
}

MyAES::~MyAES()
{

}

void MyAES::GenerateKey()
{
    AutoSeededRandomPool rnd;
    byte key1[AES::DEFAULT_KEYLENGTH];
    rnd.GenerateBlock(key1, AES::DEFAULT_KEYLENGTH);

    // Generate a random IV
    byte iv1[AES::BLOCKSIZE];
    rnd.GenerateBlock(iv1, AES::BLOCKSIZE);

    SetKey(key1, iv1, 16);
}

void MyAES::SetKey(byte * key1, byte * iv1, int length1)
{
    this->key = key1;
    this->iv = iv1;
    this->key_length = length1;
}

string MyAES::Encrypt(string plainText)
{
    string cipherText;
    CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipherText ));
    stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plainText.c_str() ), plainText.length() + 1 );
    stfEncryptor.MessageEnd();

    string cipherTextHex;
    for( int i = 0; i < cipherText.size(); i++ )
    {
        char ch[3] = {
    0};
        sprintf(ch, "%02x",  static_cast(cipherText[i]));
        cipherTextHex += ch;
    }

    return cipherTextHex;
}

string MyAES::Decrypt(string cipherTextHex)
{
    string cipherText;
    string decryptedText;
    int i = 0;
    while(true)
    {
        char c;
        int x;
        stringstream ss;
        ss<2).c_str();
        ss>>x;
        c = (char)x;
        cipherText += c;
        if(i >= cipherTextHex.length() - 2)break;
        i += 2;
    }

    //
    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ));
    stfDecryptor.Put( reinterpret_cast<const unsigned char*>( cipherText.c_str() ), cipherText.size());

    stfDecryptor.MessageEnd();

    return decryptedText;
}

string ltos(long l)
{
    ostringstream os;
    os<string result;
    istringstream is(os.str());
    is>>result;
    return result;

}

int main() {

    byte key[]  = {
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x01, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x01};
    byte iv[]   = {
    0x01,0x02,0x03,0x03,0x03,0x03,0x03,0x03, 0x03,0x03, 0x01,0x02,0x03,0x03,0x03,0x03};

    int keysize = 16;
    char * this_mac = new char[6];
    get_mac(this_mac);
    printf("this mac: %s\n", this_mac);
    time_t tt = time(NULL);
    long cur_t = (long)tt;
    string begin = ltos(cur_t);
    char mac[64];
    sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x",this_mac[0]&0xff, this_mac[1]&0xff, this_mac[2]&0xff, this_mac[3]&0xff, this_mac[4]&0xff, this_mac[5]&0xff);
    printf("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", this_mac[0]&0xff, this_mac[1]&0xff, this_mac[2]&0xff, this_mac[3]&0xff, this_mac[4]&0xff, this_mac[5]&0xff);
    std::string mac_addre = mac;
    //std::string month = "1";  

    printf("mac: %s  time: %s \n", mac_addre.c_str(), begin.c_str());
    MyAES aes(key, iv, keysize);

    cout << "AES parameters: " << endl;
    cout << "The algorithm name is : " << AES::StaticAlgorithmName() << endl;
    cout << "The iv is : " << aes.iv << endl;
    cout << "The key is : " << aes.key << endl;
    cout << "The key length is : " << aes.key_length << endl;

    string mac_cipher = aes.Encrypt(mac_addre);
    string time_cipher = aes.Encrypt(begin);
    //string month_cipher = aes.Encrypt(month);
    cout << "The mac_cipher is : " << mac_cipher << endl;
    cout << "The time_cipher is : " << time_cipher << endl;

    if (access("license.lic", 0) == -1)
        system("touch license.lic");
    ofstream out("license.lic");
        out.write(mac_cipher.c_str(), mac_cipher.length());
    out << "\n";
    //out.write("\n");
    out.write(time_cipher.c_str(), time_cipher.length());
    out << "\n";
    //out.write(month_cipher.c_str(), month_cipher.length());
    out.close();
    return 0;
 }

3编译

编译很简单,只需要链接上libcryptopp.so就行

g++ *.cpp -o * -lpthread -lcryptopp

PS::

如果想用在caffe项目中,切记将#include “AES.h”放在最上面

你可能感兴趣的:(tools,code,AES)