【问题标题】:Example of AES using Crypto++ [closed]使用 Crypto++ 的 AES 示例 [关闭]
【发布时间】:2012-08-31 16:18:03
【问题描述】:

我一直在 Internet 上搜索 good c++ AES 代码示例/教程,它教授加密技术的基础知识和库的使用,但到目前为止我有没有运气得到像样的材料。

好:易于理解(只是移动学习的基础知识)。

【问题讨论】:

  • 你想了解如何使用库或算法的基础?
  • @MatteoItalia 我需要在我的项目中使用 AES,所以学习图书馆是必须的(因为项目截止日期)但是如果我能沿途收集一些知识,那就太好了!
  • 很遗憾这个问题已经被关闭了!

标签: encryption cryptography aes crypto++


【解决方案1】:

Crypto++ AES的官方文档是一个好的开始。从我的档案中,AES 的基本实现如下:

更多解释请参考here,我建议您先了解algorithm,然后尝试逐步了解每一行。

#include <iostream>
#include <iomanip>

#include "modes.h"
#include "aes.h"
#include "filters.h"

int main(int argc, char* argv[]) {

    //Key and IV setup
    //AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-   
    //bit). This key is secretly exchanged between two parties before communication   
    //begins. DEFAULT_KEYLENGTH= 16 bytes
    CryptoPP::byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
    memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
    memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );

    //
    // String and Sink setup
    //
    std::string plaintext = "Now is the time for all good men to come to the aide...";
    std::string ciphertext;
    std::string decryptedtext;

    //
    // Dump Plain Text
    //
    std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
    std::cout << plaintext;
    std::cout << std::endl << std::endl;

    //
    // Create Cipher Text
    //
    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() );
    stfEncryptor.MessageEnd();

    //
    // Dump Cipher Text
    //
    std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

    for( int i = 0; i < ciphertext.size(); i++ ) {

        std::cout << "0x" << std::hex << (0xFF & static_cast<CryptoPP::byte>(ciphertext[i])) << " ";
    }

    std::cout << std::endl << std::endl;

    //
    // Decrypt
    //
    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();

    //
    // Dump Decrypted Text
    //
    std::cout << "Decrypted Text: " << std::endl;
    std::cout << decryptedtext;
    std::cout << std::endl << std::endl;

    return 0;
}

安装详情:

sudo apt-get install libcrypto++-dev libcrypto++-doc libcrypto++-utils

【讨论】:

  • 我在附加依赖项中添加了 cryptlib.lib 并尝试在 VS2010 上构建它,但出现 47 个链接错误,大多数如下所示:- 错误 LNK2005:_tolower 已在 MSVCRTD.lib(MSVCR100D.dll 中定义) ) ,有什么帮助吗?
  • 我不知道 key 的初始化位置,我应该如何使用未知大小的 std::string 类型对其进行初始化?
  • @AMomchilov,原因是当输入字节数组使用代码plaintext.length() + 1时,因此最后一个0x0被添加到编码器的输入字节数组中。我只看到明文的大小是55,但解密后的大小是56。我认为我们应该使用plaintext.length(),而不是plaintext.length() +1
  • @Shalec:是的,这是一个愚蠢的声明。我删除了它。
  • 这主要对我有用,但我发现decryptedtext 字符串比plaintext 长一个字节,因为它有一个额外的尾随空字节。正如@Micka 所指出的,我还必须修复byte 类型缺少的命名空间。我用这些修复更新了答案。
猜你喜欢
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 2012-12-28
相关资源
最近更新 更多