• AES加密 C++调用Crypto++加密库 样例


    这阵子写了一些数据加密的小程序,对照了好几种算法后,选择了AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES)。听这名字就非常厉害的样子

    预计会搜索到这文章的。对AES算法已经有了些基本了解了吧。以下先简介一下AES加密算法吧

    (1)AES在password学中又称Rijndael加密法。是美国联邦政府採用的一种区块加密标准。2006年。高级加密标准已然成为对称密钥加密中最流行的算法之中的一个。

    (2)AES加密数据块分组长度必须为128比特。密钥长度能够是128比特、192比特、256比特中的随意一个。(8比特 == 1字节)

    (3)在CBC、CFB、OFB、CTR模式下除了密钥外,还须要一个初始化向IV。

    ECB模式不用IV

    关于AES很多其它的介绍,http://zh.wikipedia.org/wiki/AES。或者是百度百科吧

    AES可使用的加密模式的介绍,http://blog.csdn.net/aaaaatiger/article/details/2525561


    我使用的是Crypto++库,开发人员是Wei Dai,使用C++写的加密库。实现了非常多的加密算法,基本能满足我们的加密需求。使用起来也非常easy方便。这是官方站点http://www.cryptopp.com/


    写这文章目的不是介绍AES算法,仅仅是想给一个小样例让大家參考一下而已,避免大家在查了大半天加密算法,看了老久AES原理,可就是就不知道怎么使用

    (基本加解密过程是stackoverflow的一个小demo,我将它改动一下,实现了一个在两个程序之间,以文件做为介质的加解密的过程)

    这里选的是CBC模式(其他模式调用也一样)

    1、程序一:加密

    #include <stdio.h>
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    #include <cryptopp/aes.h>
    #include <cryptopp/filters.h>
    #include <cryptopp/modes.h>
    
    using namespace std;
    
    byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE];
    
    void initKV()
    {
        memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
        memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
    
        // 或者也能够
        /*
        char tmpK[] = "1234567890123456";
        char tmpIV[] = "1234567890123456";
        for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j)
        {
            key[j] = tmpK[j];
        }
    
        for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i)
        {
            iv[i] = tmpIV[i];
        }
        */
    }
    
    string 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<byte>(cipherText[i]));
            cipherTextHex += ch;
        }
    
        return cipherTextHex;
    }
    
    
    
    void writeCipher(string output)
    {
        ofstream out("/tmp/cipher.data");
        out.write(output.c_str(), output.length());
        out.close();
    
        cout<<"writeCipher finish "<<endl<<endl;
    }
    
    
    
    int main()
    {
        string text = "hello zhuzhu dashen !";
        cout<<"text : "<<text<<endl;
    
        initKV();
        string cipherHex = encrypt(text);
        cout<<"cipher : "<<cipherHex<<endl;
        writeCipher(cipherHex);
    
        return 0;
    }
    


    程序二:解密

    #include <stdio.h>
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    #include <cryptopp/aes.h>
    #include <cryptopp/filters.h>
    #include <cryptopp/modes.h>
    
    using namespace std;
    
    byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE];
    
    void initKV()
    {
        memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
        memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
    
        // 或者也能够
        /*
        char tmpK[] = "1234567890123456";
        char tmpIV[] = "1234567890123456";
        for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j)
        {
            key[j] = tmpK[j];
        }
    
        for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i)
        {
            iv[i] = tmpIV[i];
        }
        */
    }
    
    string decrypt(string cipherTextHex)
    {
        string cipherText;
        string decryptedText;
    
        int i = 0;
        while(true)
        {
            char c;
            int x;
            stringstream ss;
            ss<<hex<<cipherTextHex.substr(i, 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 readCipher()
    {
        ifstream in("/tmp/cipher.data");
    
        string line;
        string decryptedText;
        while(getline(in, line))
        {
            if(line.length() > 1)
            {
                decryptedText += decrypt(line) + "
    ";
            }
            line.clear();
        }
    
        cout<<"readCipher finish "<<endl;
        in.close();
    
        return decryptedText;
    }
    
    int main()
    {
        initKV();
        string text = readCipher();
        cout<<"text : "<<text<<endl;
        return 0;
    }
    


    安装cryptopp: sudo apt-get install libcrypto++-dev
    编译:g++ main.cpp -o main -lcryptopp

    (以上内容仅供学习參考。若发现有误。请留言告知,谢谢。)




  • 相关阅读:
    作业之 抽屉的实现
    Python数字
    Python字符串
    Python变量
    Python生成0到9的随机数
    Python 中is和==的区别?
    整理控|四象限系列电脑桌面壁纸分享一波
    Jmeter系列之接口自动化实战
    Jmeter系列之数据库操作
    Jmeter系列之接口断言
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5136341.html
Copyright © 2020-2023  润新知