繁体   English   中英

使用 AES 进行加密 ++ 解密入门

[英]Getting Started with crypto++ decryption with AES

我正在尝试学习一些有关密码学的知识,并尝试将 AES 解密与 c++ 中的 crypto++ 库一起使用。我有一个密文字符串和一个密钥字符串。 使用这两个,我想解密这个密文。 这是我的代码:

#include "mycrypto.h"
#include <stdio.h>
#include <cstdlib>
#include <string>

#include <aes.h>
#include <config.h>
#include <hex.h>
#include <files.h>
#include <cryptlib.h>
#include <modes.h>
#include <osrng.h>
#include <filters.h>
#include <sha.h>
#include <rijndael.h>


using namespace std;
using namespace CryptoPP;

int main()
{

    string myPlainText;
    string myKey = "140b41";
    string myCipherText = "4ca00f";

    byte key[AES::DEFAULT_KEYLENGTH];
    byte iv[AES::BLOCKSIZE];

    CryptoPP::CBC_Mode<AES>::DECRYPTION decryptor;
    decryptor.SetKeyWithIV(key, sizeof(key), iv);

    StringSource(myCipherText, true, new StreamTransformationFilter( decryptor, new StringSink(myPlainText)));

    return 0;
}

我在这段代码中遇到了很多错误。 最直接的是这个:

“DECRYPTION”不是“CryptoPP::CBC_Mode”的成员

任何人都可以用这段代码理顺我。 我已经遍历了 crypto++ 文档,但我看不出我做错了什么。

谢谢!

我认为DECRYPTION拼写为Decryption 至少,这就是它在这个例子中的表现

你在这里错过了一些东西。 我知道学习 crypto++ 让我望而生畏。

  • 它是 CBC_Mode< AES >::解密。
  • 你没有 IV 来解密?
  • 您还没有将十六进制“myCipherText”转换回 (byte*)。

这是我的工作示例。

职能:

encc - 加密纯文本并返回“ivString::ciphertxt”。

decc - 反汇编 IV 和密文并解密“密文”。

string encc(string plain) {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    SecByteBlock iv(AES::BLOCKSIZE);
    
    //the password
    std::string sKey = "UltraSecretKeyPhrase";
    
    // Convert "UltraSecretKeyPhrase" to SecByteBlock
    SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());
    
    // Generate IV
    prng.GenerateBlock(iv, iv.size());
    std::string cipher, recovered;

    //Try Encrypt
    try
    {
        CBC_Mode< AES >::Encryption e;
        e.SetKeyWithIV(key, key.size(), iv);

        StringSource s(plain, true,
            new StreamTransformationFilter(e,
                new StringSink(cipher)
            ) 
        ); 
    }
    catch (const Exception& e)
    {
        exit(1);
    }

    string ciphertxt, ivString;
    
    //HexEncode IV
    HexEncoder encoder(new FileSink(std::cout));
    encoder.Detach(new StringSink(ivString));
    encoder.Put(iv, iv.size());
    encoder.MessageEnd();

    //HexEncode ciphertxt
    encoder.Detach(new StringSink(ciphertxt));
    encoder.Put((const byte*)&cipher[0], cipher.size());
    encoder.MessageEnd();

    string toSend = ivString + "::" + ciphertxt;
    return toSend;
}

string decc(string toDec) {
    using namespace CryptoPP;

    std::string sKey = "UltraSecretKeyPhrase";
    SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());


    std::string recovered;
    string str1 = "::";


    size_t found = toDec.find(str1);

    //seperate iv and ciphertxt
    if (found != string::npos) {

        std::string sIv = toDec.substr(0, found);
        std::string encMessageHex = toDec.substr(found + 2);
    
        cout << endl << "IV: " << sIv << endl << "Encoded Msg: " << encMessageHex << endl;
    
        string iv, encMessage;

        HexDecoder decoder, msgDecoder;

        //Decode the IV Hex back to byte*
        decoder.Attach(new StringSink(iv));
        decoder.Put((byte*)sIv.data(), sIv.size());
        decoder.MessageEnd();
        
        //Decode the ciphertxt Hex back to byte*
        decoder.Attach(new StringSink(encMessage));
        decoder.Put((byte*)encMessageHex.data(), encMessageHex.size());
        decoder.MessageEnd();

        //Try decoding the ciphertxt
        try
        {
            CBC_Mode< AES >::Decryption d;
            d.SetKeyWithIV(key.data(), key.size(), (byte *)iv.data(), AES::BLOCKSIZE);

            StringSource s(encMessage, true,
                new StreamTransformationFilter(d,
                    new StringSink(recovered)
                )
            );
            return recovered;
        }
        catch (const Exception& e)
        {
            std::cerr << e.what() << std::endl;
             exit(1);
        }
    }
    else return NULL;
}

int main(){
    string hh = encc("this is encoded");
    cout << hh << endl;
    string gg = decc(hh);
    cout << gg << endl;
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM