在AES 256 Crypto ++中获取十六进制加密字符串 [英] Get hexadecimal encrypted string in AES 256 Crypto++

查看:226
本文介绍了在AES 256 Crypto ++中获取十六进制加密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MS视觉工作室中使用Crypto ++来实现AES 256算法。操作系统是Windows 7(64位)。



我需要将密钥提供为十六进制字符串,密码为字符串,最后我希望加密字符串也是十六进制字符串。



这是我要做的:



我的加密方法:

  std :: string encrypt(const std :: string& password)
{
std :: string plain = password ;
std :: string ciphertext;
char * decodedKey =729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E37011;

byte key [CryptoPP :: AES :: MAX_KEYLENGTH],iv [CryptoPP :: AES :: BLOCKSIZE];
CryptoPP :: StringSource(reinterpret_cast< const char *>(decodeKey),true,
new CryptoPP :: HashFilter(*(new CryptoPP :: SHA256),new CryptoPP :: ArraySink(key,CryptoPP :: AES :: MAX_KEYLENGTH)));
memset(iv,0x00,CryptoPP :: AES :: BLOCKSIZE);

CryptoPP :: CBC_Mode< CryptoPP :: AES> ::加密加密器(key,sizeof(key),iv);
CryptoPP :: StringSource(plain,true,new CryptoPP :: StreamTransformationFilter(Encryptor,
new CryptoPP :: HexEncoder(new CryptoPP :: StringSink(ciphertext))));

std :: cout<密文:<<密文;
返回密文;
}

从main方法

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

encrypt(test);
return 0;
}

目前,我正在硬编码密钥仅用于调试目的。我的键是十六进制字符串,如下所示。我需要将输出加密的字符串作为十六进制字符串。

解决方案


我需要提供密钥作为十六进制字符串,密码为字符串,最后我想加密的字符串也是十六进制字符串。


这在Crypto ++ wiki(有很多例子可以复制/粘贴)。从 HexDecoder的脚本和字符串



< blockquote>

有时邮件列表将收到有关
交叉验证的问题。例如,请参阅AES CTR Chiper。 PHP-mcrypt和Crypto ++之间输出不同的
。在这个问题中,PHP-mcrypt字符串
使用如下:

  $ key =12345678901234567890123456789012345678901234; 
$ key = pack(H.strlen($ key),$ key);
$ iv =1111111111222222222233333333334444444444555555555566666666667777;
$ iv = pack(H.strlen($ iv),$ iv);

避免打字错误的最简单的方法之一是复制/粘贴和
HexDecoder:

  string encodedKey =1234567890123456789012345678901234567890123456789012345678901234; 
string encodedIv =1111111111222222222233333333334444444444555555555566666666667777;
string key,iv; (

StringSource ssk // StringSource

StringSource ssv(encodedIv,true / * pumpAll * /,
new HexDecoder(
new StringSink(iv)
)// HexDecoder
); // StringSource


运行上述代码后, key iv 是十六进制(即二进制)字符串,而不是可打印(即ASCII)字符串。


I am trying to implement the AES 256 algorithm using Crypto++ in MS visual studio. Operating system is Windows 7 (64 bit).

I need to provide the key as a hexadecimal string, password as string and finally i want the encrypted string to also be hexadecimal string.

Here is what I am trying to do:

My encrypt method:

std::string encrypt(const std::string &password)
{
    std::string plain = password;
    std::string ciphertext;
    char * decodedKey= "729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E37011";

    byte key[ CryptoPP::AES::MAX_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
    CryptoPP::StringSource( reinterpret_cast<const char *>(decodedKey), true,
              new  CryptoPP::HashFilter(*(new  CryptoPP::SHA256), new CryptoPP::ArraySink(key, CryptoPP::AES::MAX_KEYLENGTH)) );
    memset( iv, 0x00,  CryptoPP::AES::BLOCKSIZE );

    CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption Encryptor( key, sizeof(key), iv );
    CryptoPP::StringSource( plain, true, new CryptoPP::StreamTransformationFilter( Encryptor,
              new CryptoPP::HexEncoder(new CryptoPP::StringSink( ciphertext ) ) ) );

    std::cout<<"Ciphertext:" << ciphertext;
    return ciphertext;
}

From main method

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

    encrypt("test");
    return 0;
}

At present I am hardcoding the key just for debugging purposes. My key is hexadecimal string as seen below. I need to get the output encrypted string as hex string.

解决方案

I need to provide the key as a hexadecimal string, password as string and finally i want the encrypted string to also be hexadecimal string.

This is covered in the Crypto++ wiki (there's lots of examples in there ready for copy/paste). From HexDecoder's Scripting and Strings:

On occasion, the mailing list will receive questions on cross-validation. For example, see AES CTR Chiper. Different output between PHP-mcrypt and Crypto++. In the question, PHP-mcrypt strings are used as follows:

$key = "1234567890123456789012345678901234567890123456789012345678901234";
$key = pack("H".strlen($key), $key);
$iv = "1111111111222222222233333333334444444444555555555566666666667777";
$iv = pack("H".strlen($iv), $iv);

One of the easiest ways to avoid typos is via Copy/Paste and a HexDecoder:

string encodedKey = "1234567890123456789012345678901234567890123456789012345678901234";
string encodedIv = "1111111111222222222233333333334444444444555555555566666666667777";
string key, iv;

StringSource ssk(encodedKey, true /*pumpAll*/,
    new HexDecoder(
        new StringSink(key)
    ) // HexDecoder
); // StringSource

StringSource ssv(encodedIv, true /*pumpAll*/,
    new HexDecoder(
        new StringSink(iv)
    ) // HexDecoder
); // StringSource

After running the above code, key and iv are hexadecimal (i.e., binary) strings rather than printable (i.e., ASCII) strings.

这篇关于在AES 256 Crypto ++中获取十六进制加密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆