使用aes算法解密给定的加密文本

时间:2014-05-26 13:17:37

标签: c++ encryption crypto++

我不熟悉加密技术并尝试实施产品密钥验证系统。为此,我正在使用用于AES算法的crytopp库。以下是我解密AES算法生成的加密文本的代码。

#include <iostream>
#include <string.h>
#include <sstream>
#include <cryptopp/dll.h>
#include <cryptopp/md5.h>
#include <iomanip>
#include "cryptopp/modes.h"
#include "cryptopp/aes.h"
#include "cryptopp/filters.h"
#include<stdio.h>
#include "cryptopp/base64.h"

#define CRYPTOPP_DEFAULT_NO_DLL

void error(std::string err)
{
    std::cout << err << "\n";
    exit(1);
}

void decrypt()
{

    byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
    std::string plaintext;
    std::string ciphertext;
    std::string decryptedtext;

    //std::stringstream ss;
    std::cout<< "Enter Fingerprint:  ";
    std::getline(std::cin , ciphertext);    
    std::cout << ciphertext.c_str();

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

    std::cout << ciphertext[i] ;

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

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

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

    }

    memset( key, 0xACD3, CryptoPP::AES::DEFAULT_KEYLENGTH );
    memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );


    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );


    std::string src;

    CryptoPP::StringSource ss(ciphertext, true, 
        new CryptoPP::Base64Decoder(
        new CryptoPP::StringSink(src)
        )
        );

     std::cout << src << " ";



    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
    stfDecryptor.Put( reinterpret_cast<const unsigned char*>( src.c_str() ), src.size() );
    stfDecryptor.MessageEnd();
}

void generateActivationKey(char *license_string_dect , char *activation_key){}

int main(int argc , char * argv[])
{
    char *license_string_dect , *activation_key;
    /*if(argc < 2)
      error("License Key Not Provided");*/
    decrypt();
    std::cout<< license_string_dect;
    generateActivationKey(license_string_dect , activation_key);
    std::cout<< activation_key;
    return 0;
}

现在,问题是,当加密文本作为字符串

传递时
7ff8b3ea8a2b37a3d28669c9713a7b3fa2502580d5d232cee8a5733ef70ff48e9e8498a94c25ea7b04043a1fc23b1a1eb1eb2f6976270a181ca6e788090

我收到以下错误

 terminate called after throwing an instance of 'CryptoPP::InvalidCiphertext'
  what():  StreamTransformationFilter: ciphertext length is not a multiple of block size.

我用谷歌搜索了这个但是dint得到了一个合适的方法来解密给定的加密文本

plz help

我用它来获取指纹:

// 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
    byte key[16]; 
    byte iv[ CryptoPP::AES::BLOCKSIZE ];
    memset( key, 0xACD3 , CryptoPP::AES::DEFAULT_KEYLENGTH );
    memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
    //
    // String and Sink setup
    //

    std::string plaintext = "vcm7878air:14522569:a12bc";
    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() + 1 );
    stfEncryptor.MessageEnd();

    //
    // Dump Cipher Text
    //
    std::cout << "Fingerprint is :" << std::endl;

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

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

0 个答案:

没有答案