本文整理汇总了C++中CryptoPP::StringSource方法的典型用法代码示例。如果您正苦于以下问题:C++ CryptoPP::StringSource方法的具体用法?C++ CryptoPP::StringSource怎么用?C++ CryptoPP::StringSource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CryptoPP
的用法示例。
在下文中一共展示了CryptoPP::StringSource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: encrypt
string RNEncryptor::encrypt(string plaintext, string password, RNCryptorSchema schemaVersion)
{
this->configureSettings(schemaVersion);
RNCryptorPayloadComponents components;
components.schema = (char)schemaVersion;
components.options = (char)this->options;
components.salt = this->generateSalt();
components.hmacSalt = this->generateSalt();
components.iv = this->generateIv(this->ivLength);
SecByteBlock key = this->generateKey(components.salt, password);
switch (this->aesMode) {
case MODE_CTR: {
CTR_Mode<AES>::Encryption encryptor;
encryptor.SetKeyWithIV((const byte *)key.data(), key.size(), (const byte *)components.iv.data());
StringSource(plaintext, true,
// StreamTransformationFilter adds padding as required.
new StreamTransformationFilter(encryptor,
new StringSink(components.ciphertext)
)
);
break;
}
case MODE_CBC: {
CBC_Mode<AES>::Encryption encryptor;
encryptor.SetKeyWithIV(key.BytePtr(), key.size(), (const byte *)components.iv.data());
StringSource(plaintext, true,
// StreamTransformationFilter adds padding as required.
new StreamTransformationFilter(encryptor,
new StringSink(components.ciphertext)
)
);
break;
}
}
stringstream binaryData;
binaryData << components.schema;
binaryData << components.options;
binaryData << components.salt;
binaryData << components.hmacSalt;
binaryData << components.iv;
binaryData << components.ciphertext;
std::cout << "Hex encoded: " << this->hex_encode(binaryData.str()) << std::endl;
binaryData << this->generateHmac(components, password);
return this->base64_encode(binaryData.str());
}
示例2: Encrypt
QString QuizDatabase::Encrypt(QString src)
{
QString t_output;
byte key[AES::DEFAULT_KEYLENGTH] = EncryptionKey;
byte iv[AES::BLOCKSIZE] = EncryptionKey_2;
OFB_Mode<AES>::Encryption t_Encrypt;
t_Encrypt.SetKeyWithIV(key,sizeof(key),iv);
string t_Str,t_strNonHex,t_strCompletelyEncrypted;
t_Str = src.toStdString();
StringSource(t_Str,true,new StreamTransformationFilter(t_Encrypt,new StringSink(t_strNonHex)));
StringSource(t_strNonHex,true,new HexEncoder(new StringSink(t_strCompletelyEncrypted)));
t_output = QString::fromStdString(t_strCompletelyEncrypted);
return t_output;
}
示例3: encrypt_file
void encrypt_file(string oFile, string encMode)
{
// SecByteBlock key(AES::MAX_KEYLENGTH);
// byte iv[ AES::BLOCKSIZE ];
string ofilename = oFile;
string outFile = oFile + ".egg";
string efilename = outFile;
std::ifstream ifile(oFile.c_str(), ios::binary);
std::ifstream::pos_type size = ifile.seekg(0, std::ios_base::end).tellg();
ifile.seekg(0, std::ios_base::beg);
string temp;
temp.resize(size);
ifile.read((char*)temp.data(), temp.size());
if(encMode == "OFB")
{
OFB_Mode< AES >::Encryption e1;
e1.SetKeyWithIV( key, key.size(), iv, sizeof(iv) );
StringSource( temp, true,
new StreamTransformationFilter( e1,
new FileSink( efilename.c_str() )));
}
else if(encMode == "CFB")
{
CFB_Mode< AES >::Encryption e1;
e1.SetKeyWithIV( key, key.size(), iv, sizeof(iv) );
StringSource( temp, true,
new StreamTransformationFilter( e1,
new FileSink( efilename.c_str() )));
}
else if(encMode == "GCM")
{
GCM< AES >::Encryption e1;
e1.SetKeyWithIV( key, key.size(), iv, sizeof(iv) );
StringSource ss1( temp, true,
new AuthenticatedEncryptionFilter( e1,
new FileSink( efilename.c_str() )));
}
else
{
cerr << "Invalid Mode" <<endl;
}
}
示例4: signature_verify
void signature_verify( const string & account_num, string & signature){
RSA::PublicKey publicKey;
string recovered, message;
// Load public key
CryptoPP::ByteQueue bytes;
FileSource file("pubkey.txt", true, new Base64Decoder);
file.TransferTo(bytes);
bytes.MessageEnd();
publicKey.Load(bytes);
// Verify and Recover
RSASS<PSSR, SHA1>::Verifier verifier(publicKey);
StringSource(signature, true,
new SignatureVerificationFilter(
verifier,
new StringSink(recovered),
SignatureVerificationFilter::THROW_EXCEPTION |
SignatureVerificationFilter::PUT_MESSAGE
) // SignatureVerificationFilter
); // StringSource
assert(account_num == recovered);
cout << "Verified signature on message" << endl;
cout << "Message: " << "'" << recovered << "'" << endl;
}
示例5: main
int main(int argc, char* argv[])
{
AutoSeededRandomPool prng;
byte fkey[SHA256::DIGESTSIZE];
string key_from_file, plain;
byte key[AES::MAX_KEYLENGTH]; // 32 bytes
string filename=argv[2];
string share_fkey;
FileSource(argv[1], true,
new HexDecoder(
new StringSink(key_from_file))
);
//removing directory paths if any
filename = filename.substr(filename.find_last_of("/")+1,filename.length());
byte iv[AES::BLOCKSIZE]; // 16 bytes
iv[0] = 0;
//prng.GenerateBlock(iv, sizeof(iv));
string temp = key_from_file+filename;
byte digest_input[temp.length()];
for (int i=0;i<=temp.length();i++)
digest_input[i]=temp[i];
SHA256().CalculateDigest(fkey, digest_input, sizeof(digest_input));
StringSource(fkey, sizeof(fkey),true,
new HexEncoder(
new StringSink(share_fkey))
);
cout<<"fkey to share : "<<share_fkey<<endl;
string new_filename = filename.substr(filename.find_last_of(".")+1,filename.length()) + '.' + filename.substr(0,filename.find_last_of("."));
byte efile[SHA256::DIGESTSIZE];
string encoded_efile;
byte filename_plain_input[new_filename.length()];
for (int i=0;i<=new_filename.length();i++)
filename_plain_input[i]=(byte)new_filename[i];
SHA256().CalculateDigest(efile, filename_plain_input, sizeof(filename_plain_input));
StringSource(efile, sizeof(efile), true,
new HexEncoder(
new StringSink(encoded_efile)
) // HexEncoder
); // StringSource
cout<<"the filename on cloud server : "<<encoded_efile<<endl<<endl;
return 0;
}
示例6: HASH224_message
int HASH224_message(char* message, int len,
char* hash_message, int hash_len)
{
AutoSeededRandomPool prng;
SecByteBlock key(16);
prng.GenerateBlock(key, key.size());
string plain;
CharToString(plain, message, len);
string mac, encoded;
encoded.clear();
StringSource(key, key.size(), true,
new HexEncoder(
new StringSink(encoded)
)
); //StringSource
try
{
HMAC<SHA224> hmac(key, key.size());
StringSource(plain, true,
new HashFilter(hmac,
new StringSink(mac)
)
);
}
catch(const CryptoPP::Exception& e)
{
cerr<<e.what()<<endl;
return 0;
exit(1);
}
encoded.clear();
StringSource(mac, true,
new HexEncoder(
new StringSink(encoded)
)
);
if(hash_len <= encoded.length())
return -1;
StringToChar(encoded, hash_message);
return (int)encoded.length();
}
示例7: rcry_encryptInContext
unsigned int rcry_encryptInContext(string *input, string *output)
{
if (target == RCRY_RIJNDAEL)
{
try
{
CTR_Mode<AES>::Encryption e;
e.SetKeyWithIV(vault_key, sizeof(vault_key), context->iv);
StringSource((*input), true,
new StreamTransformationFilter(e,
new StringSink((*output))));
} catch (const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
return 0x44;
}
}
else
{
cerr << "Context not implemented! Use RCRY_RIJNAEL INSTEAD!" << endl;
return 0xFAF;
}
return 0;
}
示例8: signature_sign
void signature_sign(const string & account_num, string & signature){
// Setup
string message = account_num;
RSA::PrivateKey privateKey;
AutoSeededRandomPool rng;
// Load private key
CryptoPP::ByteQueue bytes;
FileSource file("privkey.txt", true, new Base64Decoder);
file.TransferTo(bytes);
bytes.MessageEnd();
privateKey.Load(bytes);
// Sign and Encode
RSASS<PSSR, SHA1>::Signer signer(privateKey);
// StringSource
StringSource(message, true,
new SignerFilter(rng, signer,
new StringSink(signature),
true // putMessage
) // SignerFilter
);
}
示例9: encrypt_file
void encrypt_file(string oFile)
{
// SecByteBlock key(AES::MAX_KEYLENGTH);
// byte iv[ AES::BLOCKSIZE ];
string ofilename = oFile;
string outFile = oFile + ".egg";
string efilename = outFile;
//try {
/*********************************\
\*********************************/
OFB_Mode< AES >::Encryption e1;
e1.SetKeyWithIV( key, key.size(), iv, sizeof(iv) );
std::ifstream ifile(oFile.c_str(), ios::binary);
std::ifstream::pos_type size = ifile.seekg(0, std::ios_base::end).tellg();
ifile.seekg(0, std::ios_base::beg);
string temp;
temp.resize(size);
ifile.read((char*)temp.data(), temp.size());
StringSource( temp, true,
new StreamTransformationFilter( e1,
new FileSink( efilename.c_str() )
) // StreamTransformationFilter
); // StringSource
/*********************************\
\*********************************/
}
示例10: DecryptToInt
int QuizDatabase::DecryptToInt(QString &src)
{
QString t_input;
int lResult;
QString temp;
byte key[AES::DEFAULT_KEYLENGTH] = EncryptionKey;
byte iv[AES::BLOCKSIZE] = EncryptionKey_2;
OFB_Mode<AES>::Decryption t_Decrypt;
t_Decrypt.SetKeyWithIV(key,sizeof(key),iv);
string t_Str,t_strNonHex,t_strCompletelyEncrypted;
t_strCompletelyEncrypted = src.toStdString();
StringSource(t_strCompletelyEncrypted,true,new HexDecoder(new StringSink(t_strNonHex)));
StringSource(t_strNonHex,true,new StreamTransformationFilter(t_Decrypt,new StringSink(t_Str)));
t_input = QString::fromStdString(t_Str);
lResult = t_input.toInt();
return lResult;
}
示例11: main
int main(int argc, char* argv[])
{
std::clock_t start,finish;
start=clock();
AutoSeededRandomPool prng;
/* generate key for mac */
unsigned char key1[16];
prng.GenerateBlock(key1, sizeof(key1));
/* generate key for encrypt */
unsigned char key2[AES::DEFAULT_KEYLENGTH];
prng.GenerateBlock(key2, sizeof(key2));
string encoded1,encoded2;
encoded1.clear();
encoded2.clear();
StringSource(key1, sizeof(key1), true,
new HexEncoder(
new StringSink(encoded1)
)
);
cout << "key for mac is:" << key1 <<endl;
cout << "hexadecimal key for mac is: " << encoded1 << endl;
StringSource(key2, sizeof(key2), true,
new HexEncoder(
new StringSink(encoded2)
)
);
cout << "key for encrypt is:" << key2 <<endl;
cout << "hexadecimal key for encrypto is: " << encoded2 << endl;
std::fstream f("key.txt",std::ios::out);
f<<encoded1<<endl;
f<<encoded2<<endl;
f.close();
finish=clock();
cout<< "the keygen running time is " << difftime(finish,start) << " ms" << endl;
return 0;
}
示例12: encrypt
std::string Encryptor::encrypt(const std::string& word)
throw (CryptoPP::Exception) {
std::string cipher;
ECB_Mode<AES>::Encryption e;
e.SetKey(key, AES_SIZE);
// The StreamTransformationFilter adds padding
// as required. ECB and CBC Mode must be padded
// to the block size of the cipher.
StringSource(word, true,
new StreamTransformationFilter(e, new StringSink(cipher)) // StreamTransformationFilter
); // StringSource
std::string encoded;
StringSource(cipher, true, new HexEncoder(new StringSink(encoded))); // StringSource
return encoded;
}
示例13: HexEncoder
unsigned int rcry_toBase64Converter(byte *binary, string *base64, bool print)
{
if (base64 == NULL) return 0xB0;
// TODO: Figure out WHY?
(*base64).clear();
StringSource(binary, sizeof(binary) * 2, true,
new HexEncoder(new StringSink((*base64))));
if (print) cout << "=> " << (*base64) << endl;
return 0;
}
示例14: VerifyMessage
bool VerifyMessage(const ECDSA<ECP, SHA1>::PublicKey& key,
const string& message, const string& signature)
{
bool result = false;
StringSource(signature + message, true,
new SignatureVerificationFilter(
ECDSA<ECP, SHA1>::Verifier(key),
new ArraySink((byte*)&result, sizeof(result))
) // SignatureVerificationFilter
);
return result;
}
示例15: decrypt
int Cryption::decrypt(int fd){
string IVContents, decryptionKey, decodedKey, decodedIV;
IVContents = read(fd);
string encodedIV = IVContents.substr(0,32);
string FileContents = IVContents.substr(encodedIV.length());
//Decoding the Key
decryptionKey = getKey();
StringSource(decryptionKey, true, new HexDecoder(new StringSink(decodedKey)));
const byte* dbyteKey = (const byte*) decodedKey.data();
//Decoding the IV
StringSource(encodedIV, true, new HexDecoder(new StringSink(decodedIV)));
const byte* IV = (const byte*) decodedIV.data();
string decryptedContents;
//Decrypting actual file
try{
CBC_Mode< AES >::Decryption d;
d.SetKeyWithIV(dbyteKey, CryptoPP::AES::DEFAULT_KEYLENGTH, IV);
StringSource de(FileContents, true, new StreamTransformationFilter(d, new StringSink (decryptedContents)));
}
catch(const CryptoPP::Exception& e){
cerr<<e.what()<<endl;
exit(1);
}
string DContents = decryptedContents;
const char *OriginalContents = DContents.c_str();
ssize_t WriteLength = pwrite(fd, OriginalContents, DContents.length(), 0);
ftruncate(fd, DContents.length());
return 0;
}