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 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() ); }
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; } }
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; }
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 }