aes 128 cbc java_C++ 和 java 使用 AES CBC 128 加解密

#include "cryptopp.h"

using namespacestd;void print(const string&cipherText) {

cout<< "[";for( unsigned int i = 0; i < cipherText.size(); i++)

{

cout<< int(cipherText[i]) << ",";

}

cout<< "]"<

}bytecryptopp::s_key[CryptoPP::AES::DEFAULT_KEYLENGTH];bytecryptopp::s_iv[CryptoPP::AES::DEFAULT_KEYLENGTH];bool cryptopp::init(const string& key, const string&iv) {if (key.size() !=CryptoPP::AES::DEFAULT_KEYLENGTH) {return false;

}if (iv.size() !=CryptoPP::AES::BLOCKSIZE) {return false;

}for(int i = 0; i < CryptoPP::AES::DEFAULT_KEYLENGTH; i++) {

s_key[i]=key[i];

}for(int i = 0; i < CryptoPP::AES::BLOCKSIZE; i++) {

s_iv[i]=iv[i];

}//memset(s_key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);//memset(s_iv, 0x00, CryptoPP::AES::BLOCKSIZE);

return true;

}string cryptopp::encrypt(const string&plainText)

{/*if ((plainText.length() % CryptoPP::AES::BLOCKSIZE) != 0) {

return "";

}*/

stringcipherTextHex;try{stringcipherText;

CryptoPP::AES::Encryption aesEncryption(s_key, CryptoPP::AES::DEFAULT_KEYLENGTH);

CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, s_iv);//CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipherText ), CryptoPP::StreamTransformationFilter::NO_PADDING);

CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, newCryptoPP::StringSink( cipherText ));

stfEncryptor.Put( reinterpret_cast( plainText.c_str() ), plainText.length() );

stfEncryptor.MessageEnd();

print(cipherText);for( unsigned int i = 0; i < cipherText.size(); i++)

{char ch[3] = {0};

sprintf(ch,"%02x", static_cast(cipherText[i]));

cipherTextHex+=ch;

}

}catch (const std::exception &e) {

cipherTextHex= "";

}returncipherTextHex;

}string cryptopp::decrypt(const string&cipherTextHex)

{/*if(cipherTextHex.empty()) {

return string();

}

if ((cipherTextHex.length() % CryptoPP::AES::BLOCKSIZE) != 0) {

return string();

}*/

stringcipherText;stringdecryptedText;

unsignedint i = 0;while(true)

{charc;intx;

stringstream ss;

ss<

ss>>x;

c= (char)x;

cipherText+=c;if(i >= cipherTextHex.length() - 2)break;

i+= 2;

}try{

CryptoPP::AES::Decryption aesDecryption(s_key, CryptoPP::AES::DEFAULT_KEYLENGTH);

CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, s_iv );//CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ),CryptoPP::StreamTransformationFilter::NO_PADDING);

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, newCryptoPP::StringSink( decryptedText ));

stfDecryptor.Put( reinterpret_cast( cipherText.c_str() ), cipherText.size());

stfDecryptor.MessageEnd();

}catch (const std::exception &e) {

decryptedText= "";

}returndecryptedText;

}intmain() {

cryptopp::init("1234567890123456", "0000000000000000");string en = cryptopp::encrypt("hello world, cryptopp");

cout<< en <

cout<< cryptopp::decrypt(en) <

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值