为了能够将AES加密文本作为std::istream解析器组件提供给我,我试图创建一个std::streambuf包含vanilla crypto ++加密/解密的实现.
该main()函数调用以下函数来比较我的包装器和vanilla实现:
EncryptFile() - 使用我的streambuf实现加密文件DecryptFile() - 使用我的streambuf实现解密文件EncryptFileVanilla() - 使用vanilla crypto ++加密文件DecryptFileVanilla() - 使用vanilla crypto ++解密文件问题是虽然加密文件由EncryptFile()和创建的EncryptFileVanilla()相同.创建的解密文件DecryptFile()不正确,比由创建的文件短16个字节DecryptFileVanilla().可能并非巧合的是,块大小也是16.
我认为问题必须在CryptStreamBuffer::GetNextChar(),但我一直在盯着它和加密++文档几个小时.
有人可以帮忙/解释一下吗?
任何其他关于我的std::streambuf实施如何蹩脚或天真的评论也欢迎;-)
谢谢,
汤姆
// Runtime Includes
#include <iostream>
// Crypto++ Includes
#include "aes.h"
#include "modes.h" // xxx_Mode< >
#include "filters.h" // StringSource and
// StreamTransformation
#include "files.h"
using namespace std;
class CryptStreamBuffer: public std::streambuf {
public:
CryptStreamBuffer(istream& encryptedInput, CryptoPP::StreamTransformation& …Run Code Online (Sandbox Code Playgroud) 我是cryptopp的新手,我尝试加密和解密文件中的文本。我总是在以下几行之后在内存位置 0x0012efe4 收到此错误 CryptoPP::InvalidCiphertext:
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.length());
stfDecryptor.MessageEnd();
Run Code Online (Sandbox Code Playgroud)
加解密代码:
BOOL Encryption()
{
// Key and IV setup
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
HW_PROFILE_INFO hwProfileInfo;
GetCurrentHwProfile(&hwProfileInfo);
(hwProfileInfo.szHwProfileGuid, strlen(hwProfileInfo.szHwProfileGuid), key);
(hwProfileInfo.szHwProfileGuid, strlen(hwProfileInfo.szHwProfileGuid), iv);
// String and Sink setup
string STRING;
ifstream infile;
infile.open ("test2.txt");
getline(infile,STRING, '\0'); // Saves the line in STRING.
char cFilm[1000];
strcpy(cFilm,STRING.c_str());
infile.close();
std::string plaintext = …Run Code Online (Sandbox Code Playgroud) 我有这个代码尝试解密:
byte key[AES::DEFAULT_KEYLENGTH];
string key_s = "essasenhaehfraca";
for (int i = 0; i < key_s.size(); i++)
key[i] = (byte) key_s[i];
string ciphertext = "A506A19333F306AC2C62CBE931963AE7DFCFFA940360A40FFD5DC69B9C2E53AD"
string decryptedtext;
try
{
ECB_Mode< AES >::Decryption decryptor;
decryptor.SetKey(key, sizeof(key));
CryptoPP::StringSource(ciphertext, true,
new CryptoPP::StreamTransformationFilter( decryptor,
new CryptoPP::StringSink( decryptedtext )
)
);
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
system("pause");
exit(1);
}
return 0;
Run Code Online (Sandbox Code Playgroud)
当我执行它时,我得到了异常
StreamTransformationFilter: invalid pkcs #7 block padding found.我搜索过,但没有找到任何东西.有人知道我为什么会收到这个错误吗?我在互联网上找到的每个例子都是以同样的方式,没有一个提到这个错误.
我正在使用CryptoPP来编码字符串"abc",问题是编码的base64字符串总是有一个尾随的'0x0a'?这是我的代码:
#include <iostream>
#include <string>
using namespace std;
#include "crypto/base64.h"
using namespace CryptoPP;
int main() {
string in("abc");
string encoded;
CryptoPP::StringSource ss(
in,
true,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(encoded)
)
);
cout << encoded.length() << endl;// outputs 5, should be 4
cout << encoded;
}
Run Code Online (Sandbox Code Playgroud)
字符串"abc"应编码为"YWJj",但结果为YWJj \n,(\n == 0x0a),长度为5.
更改源字符串没有帮助,任何字符串都将使用尾随加密\n为什么是这样?谢谢
我目前正在用C++编写blowfish加密/解密程序(使用crypto ++).
我真的没有在谷歌找到满意的答案.我正在尝试将一个SecByteBlock的密钥作为字符串发送,然后在另一部分接收为字符串,然后需要重新获得SecByteBlock.
是否可以转换字符串< - > SecByteBlock
我可以做一些更好的事情将密钥从一个功能发送到另一个功能吗
提前感谢您的帮助.
我使用的代码如下:
int main(int argc, char* argv[])
{
AutoSeededRandomPool prng;
SecByteBlock key(Blowfish::DEFAULT_KEYLENGTH);
prng.GenerateBlock(key, key.size());
byte iv[Blowfish::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));
BLOCK test = ReadStaticBlocks("testBin.dat");
string plain=test.bl1 ;
string cipher, encoded, recovered;
/*********************************\
\*********************************/
// Pretty print key
encoded.clear();
StringSource ss1(key, key.size(), true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "key: " << encoded << endl;
// Pretty print iv
encoded.clear();
StringSource ss2(iv, sizeof(iv), true,
new HexEncoder(
new StringSink(encoded)
) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Crypto ++加密文本.上次使用AES CTR时效果很好,但现在使用CBC或GCM时,我可以使用的最大密钥长度是32位??
处理加密的代码:
string xAESPlain, xAESCipher;
AutoSeededRandomPool xRng;
byte xAESKey[128]; // Doesnt Work has to be 32 or 16
byte xAESIv[128];
xRng.GenerateBlock(xAESKey, sizeof(xAESKey));
xRng.GenerateBlock(xAESIv, sizeof(xAESIv));
CBC_Mode< AES >::Encryption E;
E.SetKeyWithIV(xAESKey, sizeof(xAESKey), xAESIv);
StringSource ss(xAESPlain, true,
new StreamTransformationFilter(E,
new StringSink(xAESCipher)
)
);
Run Code Online (Sandbox Code Playgroud)
运行此Crypto ++时会抛出Exception:
terminate called after throwing an instance of 'CryptoPP::InvalidKeyLength'
what(): AES/CBC: 128 is not a valid key length
Run Code Online (Sandbox Code Playgroud)
请注意,使用Wiki中提供的example.zip(并将密钥长度更改为256或128)时会发生相同的情况
有什么想法Exception被抛出?
我开始使用crypto ++ lib,也许我有一些误解.
我没有意识到为什么下面的代码会产生一个糟糕的sha 256
# include <string>
# include <iostream>
# include "cryptopp/cryptlib.h"
# include <cryptopp/sha.h>
# include <cryptopp/hex.h>
using namespace std;
string sha256_hex(const string & str)
{
byte digest[CryptoPP::SHA256::DIGESTSIZE];
CryptoPP::SHA256().CalculateDigest(digest, (byte*) &str[0], str.size());
string ret;
CryptoPP::HexEncoder encoder;
encoder.Attach(new CryptoPP::StringSink(ret));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
return ret;
}
int main(int argc, char *argv[])
{
auto sha256 = sha256_hex(argv[1]);
cout << "str = " << argv[1] << endl
<< "sha 256 = " << sha256_hex(sha256) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下命令
./test-sha256 …Run Code Online (Sandbox Code Playgroud) 我正在使用 crypto++ 来加密和解密字符串。代码如下所示。该代码对用户名和密码进行加密。但我不知道如何将其再次解密为字符串。什么是将加密的SHA256代码解密成字符串的代码。谁能帮我。
#include <cryptopp/hex.h>
#include <cryptopp/sha.h>
#include <cryptopp/base64.h>
#include <iostream>
#include <string>
int main()
{
CryptoPP::SHA256 hash;
byte digest[CryptoPP::SHA256::DIGESTSIZE];
std::string username, password, salt, output;
std::cout << "Enter username: ";
std::getline(std::cin,username);
std::cout << std::endl << "Enter password: ";
std::getline(std::cin,password);
salt = username + password;
hash.CalculateDigest(digest,(const byte *)salt.c_str(),salt.size());
CryptoPP::HexEncoder encoder;
CryptoPP::StringSink *SS = new CryptoPP::StringSink(output);
encoder.Attach(SS);
encoder.Put(digest,sizeof(digest));
encoder.MessageEnd();
std::cout << "The username/password salted hash is => " << output << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) crypto++ ×8
c++ ×7
encryption ×3
aes ×1
aes-gcm ×1
base64 ×1
converters ×1
cryptography ×1
sha ×1
string ×1