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()); }
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; }
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; } }
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; }
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(); }
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 /*********************************\ \*********************************/ }
string convert_to_string(const private_key& priv_key) { string x; priv_key.Save(StringSink(x).Ref()); string x_1; StringSource ss(x, true, new HexEncoder(new StringSink(x_1))); return x_1; }
//signature string signature_sign(const string & msg){ // Setup string message = msg; cout << "unsigned message: "<< msg <<endl; RSA::PrivateKey privateKey = privkey; 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); string signature; // StringSource StringSource(message, true, new SignerFilter(rng, signer, new StringSink(signature), true // putMessage ) // SignerFilter ); return signature; }
void LoadKey(const string& filename, RSA::PrivateKey& PrivateKey) { // DER Encode Key - PKCS #8 key format PrivateKey.Load( FileSource(filename.c_str(), true, NULL, true /*binary*/).Ref() ); }
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; }
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 ); }
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; }
void SaveKey(const RSA::PublicKey& PublicKey, const string& filename) { // DER Encode Key - X.509 key format PublicKey.Save( FileSink(filename.c_str(), true /*binary*/).Ref() ); }
void SaveKey(const RSA::PrivateKey& PrivateKey, const string& filename) { // DER Encode Key - PKCS #8 key format PrivateKey.Save( FileSink(filename.c_str(), true /*binary*/).Ref() ); }
void LoadKey(const string& filename, RSA::PublicKey& PublicKey) { // DER Encode Key - X.509 key format PublicKey.Load( FileSource(filename.c_str(), true, NULL, true /*binary*/).Ref() ); }
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; }
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; }
void decrypt_file(string efile) //keep hash { string efilename = efile; efile.erase(efile.end()-4, efile.end()); string rfilename = efile; //SecByteBlock key(AES::MAX_KEYLENGTH); //byte iv[ AES::BLOCKSIZE ]; if(decMode == "OFB") { OFB_Mode< AES >::Decryption d2; d2.SetKeyWithIV( key, key.size(), iv, sizeof(iv) ); FileSource( efilename.c_str(), true, new StreamTransformationFilter( d2, new FileSink( rfilename.c_str() ))); } else if(decMode == "CFB") { CFB_Mode< AES >::Decryption d2; d2.SetKeyWithIV( key, key.size(), iv, sizeof(iv) ); FileSource( efilename.c_str(), true, new StreamTransformationFilter( d2, new FileSink( rfilename.c_str() ))); } else if(decMode == "GCM") { GCM< AES >::Decryption d2; d2.SetKeyWithIV( key, key.size(), iv, sizeof(iv) ); FileSource fs2( efilename.c_str(), true, new AuthenticatedDecryptionFilter( d2, new FileSink( rfilename.c_str() ), AuthenticatedDecryptionFilter::THROW_EXCEPTION)); } else { cerr << "Decryption Error" <<endl; } }
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; }
inline void Swap128(__m128i& a,__m128i& b) { #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120) // __m128i is an unsigned long long[2], and support for swapping it was not added until C++11. // SunCC 12.1 - 12.3 fail to consume the swap; while SunCC 12.4 consumes it without -std=c++11. vec_swap(a, b); #else std::swap(a, b); #endif }
void SPECK64_Dec_Block(uint32x4_p &block0, uint32x4_p &block1, const word32 *subkeys, unsigned int rounds) { #if (CRYPTOPP_BIG_ENDIAN) const uint8x16_p m1 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; const uint8x16_p m2 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; #else const uint8x16_p m1 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; const uint8x16_p m2 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; #endif // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... uint32x4_p x1 = VecPermute(block0, block1, m1); uint32x4_p y1 = VecPermute(block0, block1, m2); for (int i = static_cast<int>(rounds-1); i >= 0; --i) { #if CRYPTOPP_POWER7_AVAILABLE const uint32x4_p rk = vec_splats(subkeys[i]); #else // subkeys has extra elements so memory backs the last subkey const uint8x16_p m = {0,1,2,3, 0,1,2,3, 0,1,2,3, 0,1,2,3}; uint32x4_p rk = VecLoad(subkeys+i); rk = VecPermute(rk, rk, m); #endif y1 = VecXor(y1, x1); y1 = RotateRight32<3>(y1); x1 = VecXor(x1, rk); x1 = VecSub(x1, y1); x1 = RotateLeft32<8>(x1); } #if (CRYPTOPP_BIG_ENDIAN) const uint8x16_p m3 = {19,18,17,16, 3,2,1,0, 23,22,21,20, 7,6,5,4}; const uint8x16_p m4 = {27,26,25,24, 11,10,9,8, 31,30,29,28, 15,14,13,12}; #else const uint8x16_p m3 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; const uint8x16_p m4 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; #endif // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] block0 = (uint32x4_p)VecPermute(x1, y1, m3); block1 = (uint32x4_p)VecPermute(x1, y1, m4); }
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; }
QByteArray encrypt(QByteArray in) { byte iv[CryptoPP::AES::BLOCKSIZE]; rnd.GenerateBlock(iv, CryptoPP::AES::BLOCKSIZE); QByteArray out = QByteArray((char*)iv, CryptoPP::AES::BLOCKSIZE); int inputSize = in.size(); string cipher; CBC_Mode<AES>::Encryption aes(keyByte(), keySize(), iv); ArraySource((byte *)in.data(), inputSize, true, new StreamTransformationFilter(aes, new StringSink(cipher))); QByteArray encryptedBytes = QByteArray(cipher.c_str(), cipher.size()); out.append(encryptedBytes); return out; }
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; }
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; }
int Cryption::encrypt(int fd){ AutoSeededRandomPool prng; string encryptedBytes, encodedKey, decodedKey; encodedKey = getKey(); cout<<"Encode: "<<encodedKey<<endl; StringSource(encodedKey, true, new HexDecoder(new StringSink(decodedKey))); cout<<"Decode: "<<decodedKey<<endl; const byte* ebyteKey = (const byte*) decodedKey.data(); string encodedIV; //createIV byte IVBytes[AES::BLOCKSIZE]; prng.GenerateBlock(IVBytes, sizeof(IVBytes)); StringSource(IVBytes, sizeof(IVBytes), true, new HexEncoder(new StringSink(encodedIV))); encryptedBytes = read(fd); string encryptedContents; //Encrypting the file contents try{ CBC_Mode< AES >::Encryption e; e.SetKeyWithIV(ebyteKey, CryptoPP::AES::DEFAULT_KEYLENGTH, IVBytes); StringSource file(encryptedBytes, true, new StreamTransformationFilter(e,new StringSink(encryptedContents))); } catch( const CryptoPP::Exception& e ){ cerr << e.what() << endl; exit(1); } string output = encodedIV + encryptedContents; const char *bufContents = output.c_str(); ssize_t WriteLength = pwrite(fd, bufContents, output.length(), 0); ftruncate(fd, output.length()); return 0; }
inline void SIMON128_Dec_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds) { #if (CRYPTOPP_BIG_ENDIAN) const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8}; const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0}; #else const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16}; const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24}; #endif // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... uint64x2_p x1 = (uint64x2_p)VecPermute(block, block, m1); uint64x2_p y1 = (uint64x2_p)VecPermute(block, block, m2); if (rounds & 1) { std::swap(x1, y1); const uint64x2_p rk = vec_splats((unsigned long long)subkeys[rounds-1]); y1 = VecXor(VecXor(y1, rk), SIMON128_f(x1)); rounds--; } for (int i = static_cast<int>(rounds-2); i >= 0; i -= 2) { const uint64x2_p rk1 = vec_splats((unsigned long long)subkeys[i+1]); x1 = VecXor(VecXor(x1, SIMON128_f(y1)), rk1); const uint64x2_p rk2 = vec_splats((unsigned long long)subkeys[i]); y1 = VecXor(VecXor(y1, SIMON128_f(x1)), rk2); } #if (CRYPTOPP_BIG_ENDIAN) const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8}; //const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0}; #else const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16}; //const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24}; #endif // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... block = (uint32x4_p)VecPermute(x1, y1, m3); }
std::string Encryptor::decrypt(const std::string& word) throw (CryptoPP::Exception) { ECB_Mode<AES>::Decryption d; d.SetKey(key, AES_SIZE); std::string cipher; StringSource(word, true, new HexDecoder(new StringSink(cipher))); std::string recovered; StringSource s(cipher, true, new StreamTransformationFilter(d, new StringSink(recovered)) // StreamTransformationFilter ); // StringSource return recovered; }
bool SignMessage(const ECDSA<ECP, SHA1>::PrivateKey& key, const string& message, string& signature) { AutoSeededRandomPool prng; signature.erase(); StringSource(message, true, new SignerFilter(prng, ECDSA<ECP, SHA1>::Signer(key), new StringSink(signature) ) //SignerFilter ); //StringSource return !signature.empty(); }
void decrypt_file(string efile) { string efilename = efile; efile.erase(efile.end()-4, efile.end()); string rfilename = efile; //SecByteBlock key(AES::MAX_KEYLENGTH); //byte iv[ AES::BLOCKSIZE ]; OFB_Mode< AES >::Decryption d2; d2.SetKeyWithIV( key, key.size(), iv, sizeof(iv) ); FileSource( efilename.c_str(), true, new StreamTransformationFilter( d2, new FileSink( rfilename.c_str() ) ) // StreamTransformationFilter ); // StringSource }