bool CEncrypt::MakeFileKey3(const std::string &sPassword, unsigned char *pHash, int nHashSize, unsigned char *pHash2, int nHashSize2) { if (!pHash) return false; int size = 64 * (sPassword.length() + 64 + nHashSize2); // max unsigned char K[64]; //max size sha unsigned char *K1 = new unsigned char[size]; unsigned char *E = new unsigned char[size]; int hash_size = nHashSize; memcpy(K, pHash, nHashSize); int iteration = 0; while( (iteration < 64) || (iteration < E[size - 1] + 32)) { size = 0; for (int i = 0; i < 64; i++) { memcpy(K1 + size, sPassword.c_str(), sPassword.length()); size += sPassword.length(); memcpy(K1 + size, K, hash_size); size += hash_size; if (pHash2) { memcpy(K1 + size, pHash2, nHashSize2); size += nHashSize2; } } CryptoPP::AES::Encryption aesEncryption(K, 16); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, K + 16); CryptoPP::StreamTransformationFilter stfEncryption(cbcEncryption, new CryptoPP::ArraySink( E, size), CryptoPP::StreamTransformationFilter::NO_PADDING); stfEncryption.Put( K1, size); stfEncryption.MessageEnd(); //---------------------------------------------------------- int E_mod_3 = 0; for (unsigned int i = 0; i < 16; ++i) { E_mod_3 += E[i]; } E_mod_3 %= 3; hash_size = SHA(E_mod_3, E, size, K); iteration++; } delete []K1; delete []E; memcpy (pHash, K, 32); // pHash - from sha256 return true; }
string AES::crypt(string plaintext, string key){ string ciphertext; CryptoPP::AES::Encryption aesEncryption((byte*)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv ); CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) ); stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 ); stfEncryptor.MessageEnd(); return ciphertext; }
void cryptos::_sha_encrypt(const bybuff& key, const bybuff& data, bybuff& out)const { //Key and IV setup //AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256- //bit). This key is secretly exchanged between two parties before communication //begins. DEFAULT_KEYLENGTH= 16 bytes //uint8_t iv[ CryptoPP::AES::BLOCKSIZE ] = {0}; std::string encoded; try{ #if 0 CryptoPP::AES::Encryption aesEncryption(key.buffer(), key.length()); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv ); CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( encoded ) ); stfEncryptor.Put( data.buffer(), (size_t)data.length() ); stfEncryptor.MessageEnd(); out = encoded; _TRACE("_sha_e(" << key.to_string() <<","<< data.to_string() <<")=" << out.to_string()); bybuff r; _sha_decrypt(key, out, r); #endif //0 CryptoPP::ECB_Mode< CryptoPP::AES >::Encryption aes_128_ecb;//(key.buffer(), key.length(), iv); aes_128_ecb.SetKey( key.buffer(), key.length() ); CryptoPP::StreamTransformationFilter encryptor(aes_128_ecb, 0, CryptoPP::BlockPaddingSchemeDef::NO_PADDING); for(size_t j = 0; j < data.length(); j++) { encryptor.Put((byte)data[j]); } encryptor.MessageEnd(); size_t ready = encryptor.MaxRetrievable(); byte outa[32] = {0}; encryptor.Get((byte*) outa, ready); out.append(outa, ready); } catch( CryptoPP::Exception& e ) { ERROR( e.what()); assert(0); } // bybuff r; //_sha_decrypt(key, out, r); }
void CEncrypt::CreateOwnerKey() { CryptoPP::RandomPool prng; CryptoPP::SecByteBlock salt(16); CryptoPP::OS_GenerateRandomBlock(false, salt, salt.size()); prng.IncorporateEntropy(salt, salt.size()); memcpy(m_anOwnerKey + 32, salt.data(), salt.size()); CryptoPP::SHA256 hash; hash.Update( (unsigned char*) impl->m_sOwnerPassword.c_str(), impl->m_sOwnerPassword.length()); hash.Update( m_anOwnerKey + 32, 8); hash.Update( m_anUserKey, 48); CryptoPP::SecByteBlock pHashData(hash.DigestSize()); hash.Final(pHashData); if (MakeFileKey3(impl->m_sOwnerPassword, pHashData.data(), pHashData.size(), m_anUserKey, 48)) { memcpy(m_anOwnerKey, pHashData.data(), pHashData.size()); hash.Update( (unsigned char*) impl->m_sOwnerPassword.c_str(), impl->m_sOwnerPassword.length()); hash.Update( m_anOwnerKey + 40, 8); hash.Update( m_anUserKey, 48); CryptoPP::SecByteBlock pHashKeyData(hash.DigestSize()); hash.Final(pHashKeyData); MakeFileKey3(impl->m_sOwnerPassword, pHashKeyData.data(), pHashKeyData.size(), m_anUserKey, 48); unsigned char empty[16] = {}; CryptoPP::AES::Encryption aesEncryption(pHashKeyData.data(), pHashKeyData.size()); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, empty); CryptoPP::StreamTransformationFilter stfEncryption(cbcEncryption, new CryptoPP::ArraySink( m_anOwnerEncryptKey, 32), CryptoPP::StreamTransformationFilter::NO_PADDING ); stfEncryption.Put2(impl->m_anEncryptionKey, 32, 1, true); stfEncryption.MessageEnd(); } }
void CEncrypt::CreateEncryptionKey() { CryptoPP::RandomPool prng; CryptoPP::SecByteBlock key(32); CryptoPP::OS_GenerateRandomBlock(false, key, key.size()); prng.IncorporateEntropy(key, key.size()); memcpy(impl->m_anEncryptionKey, key.data(), key.size()); //------------------------------------------------------------------- unsigned long long extended_perms = 0xffffffff00000000LL | m_unPermission; for (int i = 0; i < 8; ++i) { m_anPermEncrypt[i] = static_cast<unsigned char>(extended_perms & 0xff); extended_perms >>= 8; } m_anPermEncrypt[8] = /*m_bEncryptMetadata ? 'T' : */'F'; m_anPermEncrypt[9] = 'a'; m_anPermEncrypt[10] = 'd'; m_anPermEncrypt[11] = 'b'; CryptoPP::SecByteBlock p(4); CryptoPP::OS_GenerateRandomBlock(false, p, p.size()); prng.IncorporateEntropy(p, p.size()); memcpy(m_anPermEncrypt + 12, p.data(), p.size()); unsigned char empty[16] = {}; CryptoPP::AES::Encryption aesEncryption(impl->m_anEncryptionKey, 32); CryptoPP::CipherModeFinalTemplate_ExternalCipher<CryptoPP::ECB_OneWay> ecbEncryption(aesEncryption, empty ); CryptoPP::StreamTransformationFilter stfEncryption(ecbEncryption, new CryptoPP::ArraySink( m_anPermEncrypt, 16), CryptoPP::StreamTransformationFilter::NO_PADDING ); stfEncryption.Put2(m_anPermEncrypt, 16, 1, true); stfEncryption.MessageEnd(); }
void AESDecryptFile( const char *keyFilename, const char *messageFilename, const char *outputFilename ) { // hex decoding halvs length int bufferLength = 256; char *keyBuffer = new char[ bufferLength + 1 ]; // add string termination keyBuffer[ bufferLength ] = '\0'; // read in the key FileSource keyFile( keyFilename, true, new HexDecoder( new ArraySink( (unsigned char*)keyBuffer, bufferLength ) ) ); // iv is all-zero char *iv = new char[ 32 ]; for( int i=0; i<32; i++ ) { iv[i] = 0; } AESEncryption aesEncryption( (byte *)keyBuffer, 32 ); CFB_Mode_ExternalCipher::Decryption cfbDecryption( aesEncryption, (byte *)iv ); FILE *messageFile = fopen( messageFilename, "r" ); FILE *outputFile = fopen( outputFilename, "wb" ); if( messageFile != NULL && outputFile != NULL ) { char *oneCharInputString = new char[2]; oneCharInputString[1] = '\0'; char *oneCharOutputString = new char[2]; oneCharOutputString[1] = '\0'; int charReadAsInt = getc( messageFile ); while( charReadAsInt != EOF ) { unsigned char charReadAsChar = (unsigned char)charReadAsInt; oneCharInputString[0] = charReadAsChar; cfbDecryption.ProcessString( (byte *)oneCharOutputString, (byte *)oneCharInputString, 1 ); // FIXME: need to hex-encode output fprintf( outputFile, "%s", oneCharOutputString ); charReadAsInt = getc( messageFile ); } fclose( messageFile ); fclose( outputFile ); delete [] oneCharOutputString; delete [] oneCharInputString; } delete [] keyBuffer; delete [] iv; }
int main(int argc, char* argv[]) { //Key and IV setup //AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256- //bit). This key is secretly exchanged between two parties before communication //begins. DEFAULT_KEYLENGTH= 16 bytes byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ]; memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH ); memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE ); // // String and Sink setup // std::string plaintext = "Now is the time for all good men to come to the aide..."; std::string ciphertext; std::string decryptedtext; // // Dump Plain Text // std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl; std::cout << plaintext; std::cout << std::endl << std::endl; // // Create Cipher Text // CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv ); CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) ); stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 ); stfEncryptor.MessageEnd(); // // Dump Cipher Text // std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl; for( int i = 0; i < ciphertext.size(); i++ ) { std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " "; } std::cout << std::endl << std::endl; // // Decrypt // CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH); CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv ); CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) ); stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() ); stfDecryptor.MessageEnd(); // // Dump Decrypted Text // std::cout << "Decrypted Text: " << std::endl; std::cout << decryptedtext; std::cout << std::endl << std::endl; return 0; }