void Node::generateKeysAES(void){ AutoSeededRandomPool rnd; aeskey=SecByteBlock(0x00, AES::DEFAULT_KEYLENGTH); rnd.GenerateBlock( aeskey, aeskey.size() ); rnd.GenerateBlock(iv, AES::BLOCKSIZE); return; }
int main(int argc, char* argv[]) { (void)argc; (void)argv; AutoSeededRandomPool prng; SecByteBlock key(Blowfish::DEFAULT_KEYLENGTH); prng.GenerateBlock( key, key.size() ); byte iv[ Blowfish::BLOCKSIZE ]; prng.GenerateBlock( iv, sizeof(iv) ); string ofilename = "puppy-and-teddy-orig.jpg"; string efilename = "puppy-and-teddy.enc"; string rfilename = "puppy-and-teddy-recovered.jpg"; try { /*********************************\ \*********************************/ EAX< Blowfish >::Encryption e1; e1.SetKeyWithIV( key, key.size(), iv, sizeof(iv) ); std::ifstream ifile("puppy-and-teddy-orig.jpg", 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 ss1( temp, true, new AuthenticatedEncryptionFilter( e1, new FileSink( efilename.c_str() ) ) // StreamTransformationFilter ); // StringSource /*********************************\ \*********************************/ EAX< Blowfish >::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 ) // StreamTransformationFilter ); // StringSource } catch (const Exception& ex) { cerr << ex.what() << endl; } return 0; }
bool CWSKey::Save(char *filename) { int version = WSK_VERSION; char junk1[1234] = {0}; char junk2[3456] = {0}; char encrypt[sizeof(junk1)+sizeof(WSKey_key)+sizeof(junk2)] = {0}; FILE *file = 0; // Set Seed Value srand(GetTickCount()); // set Seed Value for Pseudo Random Number Generator // Generate Junk Data for (int x = 0; x < sizeof(junk1); x++) { junk1[x] = (char) (-127 + (rand( ) % 127)); junk2[x] = (char) (-127 + (rand( ) % 127)); } for (int x = sizeof(junk1); x < sizeof(junk2); x++) { junk2[x] = (char) (0 + (rand( ) % 255)); } // Encrypt ALL DATA BEFORE WRITE AutoSeededRandomPool rnd; // Generate a random key byte key[AES::DEFAULT_KEYLENGTH]; rnd.GenerateBlock(key, AES::DEFAULT_KEYLENGTH); // Generate a random IV byte iv[AES::BLOCKSIZE]; rnd.GenerateBlock(iv, AES::BLOCKSIZE); // Copy To Tmp Place, Then Encrypt WSKey_key encrypt_key = this->m_key; CFB_Mode<AES>::Encryption cfbEncryption(key, AES::DEFAULT_KEYLENGTH, iv); cfbEncryption.ProcessData((byte*)junk1, (byte*)junk1, sizeof(junk1)); cfbEncryption.ProcessData((byte*)&encrypt_key, (byte*)&encrypt_key, sizeof(m_key)); cfbEncryption.ProcessData((byte*)junk2, (byte*)junk2, sizeof(junk2)); // Write Key File file = fopen(filename, "wb"); if (file == 0) { WSK_LOG("Failed To Open The Key File"); return false; } // Write Encrypted Key Data fwrite(&version, sizeof(version), 1, file); // File Version fwrite(junk1, sizeof(junk1), 1, file); // write junk fwrite(&encrypt_key, sizeof(m_key), 1, file); // Write Key // fwrite(&m_key, sizeof(m_key), 1, file); // Unencrypted Key fwrite(key, sizeof(key), 1, file); // AES KEY fwrite(iv, sizeof(iv), 1, file); // AES iv fwrite(junk2, sizeof(junk2), 1, file); // write junk // Close file fclose(file); return true; }
AESParams genAESParams(){ AESParams params; AutoSeededRandomPool rnd; SecByteBlock key(0x00, AES::DEFAULT_KEYLENGTH); SecByteBlock iv(0x00, AES::BLOCKSIZE); rnd.GenerateBlock(iv, AES::BLOCKSIZE); rnd.GenerateBlock(key, AES::DEFAULT_KEYLENGTH); params.key = key; params.iv = iv; return params; }
int main(){ AutoSeededRandomPool rand; SecByteBlock key(AES::DEFAULT_KEYLENGTH); rand.GenerateBlock(key, key.size()); byte iv[AES::BLOCKSIZE]; rand.GenerateBlock(iv, sizeof(iv)); encryptFile("test.txt", key, iv); decryptFile("encText.txt", key, iv); }
int main() { AutoSeededRandomPool rnd; byte key[AES_LENGTH]; array<byte, AES_LENGTH> akey; vector<byte> vkey(AES_LENGTH); rnd.GenerateBlock(key, sizeof(key)); rnd.GenerateBlock(akey.data(), akey.size()); rnd.GenerateBlock(vkey.data(), vkey.size()); for(int s = 0; s < 100000; s += 100){ vector<byte> msg(s); rnd.GenerateBlock(msg.data(), msg.size()); auto cipher = enc(key, msg); auto recovered = dec(key, cipher); if (recovered != msg) { cerr << "ERR key" << endl; return; } } for(int s = 0; s < 100000; s += 100){ vector<byte> msg(s); rnd.GenerateBlock(msg.data(), msg.size()); auto cipher = enc(akey, msg); auto recovered = dec(akey, cipher); if (recovered != msg) { cerr << "ERR akey" << endl; return; } } for(int s = 0; s < 100000; s += 100){ vector<byte> msg(s); rnd.GenerateBlock(msg.data(), msg.size()); auto cipher = enc(vkey, msg); auto recovered = dec(vkey, cipher); if (recovered != msg) { cerr << "ERR vkey" << endl; return; } } }
void MyAES::GenerateKey() { AutoSeededRandomPool rnd; byte key1[AES::DEFAULT_KEYLENGTH]; rnd.GenerateBlock(key1, AES::DEFAULT_KEYLENGTH); // Generate a random IV byte iv1[AES::BLOCKSIZE]; rnd.GenerateBlock(iv1, AES::BLOCKSIZE); SetKey(key1, iv1, 16); }
CryptoPP::Integer RandomQuadraticResidue(CryptoPP::Integer p) { AutoSeededRandomPool rng; byte randomBytes[64]; rng.GenerateBlock(randomBytes, 64); Integer t = Integer(randomBytes, 64); return ModularExponentiation(t, 2, p); }
void CUIntX::SetRandomValue() { using namespace CryptoPP; AutoSeededRandomPool rng; rng.GenerateBlock(GetData(), GetSize()); }
std::string GenerateRandomSequence(size_t length) { AutoSeededRandomPool rnd; string result; result.resize(length); rnd.GenerateBlock((byte*)result.data(), length); return result; }
static string DES3(const string & text, const char * passwd = "", const char * stored_iv = "", const bool & encryption = true) { const char * enhPwd(enhancePasswd(passwd, int(DES_EDE3::DEFAULT_KEYLENGTH))); SecByteBlock key(strlen(enhPwd)); byte iv[DES_EDE3::BLOCKSIZE]; for(int i(0); i < strlen(enhPwd); ++i) key[i] = enhPwd[i]; if(stored_iv == "") { AutoSeededRandomPool prng; prng.GenerateBlock(iv, sizeof(iv)); } else for(int i(0); i < DES_EDE3::BLOCKSIZE; ++i) iv[i] = (byte)stored_iv[i]; string cipher(""); try { if(encryption) { CBC_Mode< DES_EDE3 >::Encryption e; e.SetKeyWithIV(key, key.size(), iv); StringSource(text, true, new StreamTransformationFilter(e, new StringSink(cipher))); cipher.append("\n").append((char *)iv); } else { CBC_Mode< DES_EDE3 >::Decryption d; d.SetKeyWithIV(key, key.size(), iv); StringSource(text, true, new StreamTransformationFilter(d, new StringSink(cipher))); } } catch(const CryptoPP::Exception & e) { cerr << e.what() << endl; exit(1); } return cipher; }
CUInt128& CUInt128::SetValueRandom() { AutoSeededRandomPool rng; byte byRandomBytes[16]; rng.GenerateBlock(byRandomBytes, 16); SetValueBE( byRandomBytes ); return *this; }
std::vector<unsigned char> ContainerController::get_pseudo_seed() { using namespace CryptoPP; SecByteBlock b(32); AutoSeededRandomPool prng; prng.GenerateBlock(b, b.size()); return{ std::begin(b), std::end(b) }; }
CUInt128& CUInt128::setValueRandom(void) { AutoSeededRandomPool rng; byte randomBytes[16]; rng.GenerateBlock(randomBytes, 16); setValueBE( randomBytes ); return *this; }
unsigned int rcry_setCryptoContext(struct ree_ccontext_t *newContext) { if (newContext == NULL) return 0x65; // This means that a new IV needs to be made. Yay! if (newContext->fresh != 0) { // TODO: Figure out how to make this code less ugly! AutoSeededRandomPool prng; if (target == RCRY_RIJNDAEL) { byte iv[AES::BLOCKSIZE]; prng.GenerateBlock(iv, sizeof(iv)); byte *final_iv = (byte*) malloc(sizeof(iv)); memcpy(final_iv, iv, sizeof(final_iv) * 2); newContext->iv = final_iv; CryptoPP::SecureWipeArray(iv, sizeof(iv)); } else if (target == RCRY_TWOFISH) { byte iv[Twofish::BLOCKSIZE]; prng.GenerateBlock(iv, sizeof(iv)); byte *final_iv = (byte*) malloc(sizeof(iv)); memcpy(final_iv, iv, sizeof(final_iv)); newContext->iv = final_iv; CryptoPP::SecureWipeArray(iv, sizeof(iv)); } else return 0x66; // Make sure to update the fresh variable. newContext->fresh = 0; } // Now go ahead, zero the old context and write over the new! CryptoPP::SecureWipeArray(&context, sizeof(context)); context = newContext; cout << "TARGET: " << target << endl; return 0; }
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 DaStorKeyGenerator::work() { AutoSeededRandomPool rnd; cout << "gen key in memery." << endl; rnd.GenerateBlock(keyData, AES::DEFAULT_KEYLENGTH); saveKeyFile(); saveKeyCpp(); }
const QByteArray& AESAlgorithmCBCMode::generateKey(int keySize, int blockSize) { byte *key; byte *iv; keySize = keySize < 0 ? m_allowedKeySizes.first() : keySize; blockSize = blockSize < 0 ? m_allowedBlockSizes.first() : blockSize; if (!m_allowedKeySizes.contains(keySize) || !m_allowedBlockSizes.contains(blockSize)) { throw SymmetricAlgorithmException("Unsupported IV or Key length"); } m_key.clear(); m_iv.clear(); AutoSeededRandomPool prng; key = new byte[keySize]; iv = new byte[blockSize]; try { //generate random key and iv prng.GenerateBlock(key, keySize); prng.GenerateBlock(iv, blockSize); //try to set them m_aesEncryption.SetKeyWithIV(key, keySize, iv); m_aesDecryption.SetKeyWithIV(key, keySize, iv); m_key = QByteArray(reinterpret_cast<const char*>(key), keySize); m_iv = QByteArray(reinterpret_cast<const char*>(iv), blockSize); } catch (const CryptoPP::Exception& e) { delete[] key; delete[] iv; throw SymmetricAlgorithmException(e.what()); } delete[] key; delete[] iv; return m_key; }
string RNEncryptor::generateIv(int length) { AutoSeededRandomPool prng; byte iv[length + 1]; prng.GenerateBlock(iv, length); iv[length] = '\0'; string ivString = string((char *)iv); return ivString; }
byte* generateRandomSalt(int size=DEFAULT_SALT_SIZE) { // Get a random number generator from the OS AutoSeededRandomPool rng; byte* salt = new byte[size]; // fill the variable with zeros memset(salt, 0, size); rng.GenerateBlock(salt, size); return salt; }
byte* CryptoBackend::generateIV() { AutoSeededRandomPool rnd; byte *iv = (byte*) calloc(AES::BLOCKSIZE, sizeof(byte)); if (iv == NULL) { } rnd.GenerateBlock(iv, AES::BLOCKSIZE); return iv; }
byte* CryptoBackend::generateSalt() { AutoSeededRandomPool rnd; byte *salt = (byte*) calloc(DEFAULT_SALT_LEN, sizeof(byte)); if (salt == NULL) { } rnd.GenerateBlock(salt, DEFAULT_SALT_LEN); return salt; }
void initAES() { SecByteBlock key(0x00, AES::DEFAULT_KEYLENGTH); rng.GenerateBlock(key, key.size()); byte initVector[AES::BLOCKSIZE]; rng.GenerateBlock(initVector, AES::BLOCKSIZE); printf("sending aes... "); string aesInform; for (int i = 0; i < AES::DEFAULT_KEYLENGTH; i++) aesInform.push_back(*(key.BytePtr() + i)); for (int i = 0; i < AES::BLOCKSIZE; i++) aesInform.push_back(initVector[i]); aesInform = encryptRSA(aesInform); aesInform = base64_encode((unsigned char*)(aesInform.c_str()), aesInform.size()); send(mainSocket, aesInform.c_str(), aesInform.size(), 0); printf("sended\n"); cfbEncryption = new CFB_Mode<AES>::Encryption(key, key.size(), initVector); cfbDecryption = new CFB_Mode<AES>::Decryption(key, key.size(), initVector); recvDecrypted(); if (strncmp("vtyulb", buf, 6) == 0) printf("server authorized\n"); else { printf("false server??\n"); initNET(); initAES(); return; } }
unsigned int rcry_generateKey(ree_crypto_t ctype, byte **key) { AutoSeededRandomPool prng; if (ctype == RCRY_RIJNDAEL) { (*key) = (byte*) malloc(AES::MAX_KEYLENGTH); prng.GenerateBlock((*key), sizeof((*key))); } else if (ctype == RCRY_TWOFISH) { (*key) = (byte*) malloc(AES::MAX_KEYLENGTH); prng.GenerateBlock((*key), sizeof((*key))); } else { cerr << "Unknown encryption scheme. Please pick one from the available or go f**k yourself!" << endl; return 0xFA; } 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(); }
size_t CRandomPasswordGen::getRandomSymbolIndex(void) const { uint32_t RandomNumber; size_t const Symbols = mAlphabet.getMaxLength(); size_t const MaxRandomNumber = ((uint64_t)1 << (uint64_t)sizeof(RandomNumber)*8)-1; size_t const Blocks = MaxRandomNumber / Symbols; do { RandomGenerator.GenerateBlock(reinterpret_cast<uint8_t *>(&RandomNumber), sizeof(RandomNumber)); } while(RandomNumber > (Blocks * Symbols)-1); size_t const Index = RandomNumber % Symbols; return Index; }
void testPuzzle() { Puzzle p(2); cout << "base: " << p.getBase() << endl; AutoSeededRandomPool rnd; SecByteBlock key(0x00, AES::MAX_KEYLENGTH); rnd.GenerateBlock(key, key.size()); // setup & solve Integer n; auto ckey = p.setup(key, 10, n); auto keyback = p.solve(ckey, 10, n); assert(key == keyback); cout << "Key converted back successfully!" << endl; // funcdur auto samples = p.funcdur(1000, seconds(5), nanoseconds(1000)); for (auto& s : samples) cout << s.first << ": " << s.second.count() << endl; }
string makeNonce() { const unsigned int BLOCKSIZE = 4; byte scratch[BLOCKSIZE]; AutoSeededRandomPool prng; prng.GenerateBlock(scratch, BLOCKSIZE); unsigned int nonceInt = 0; for (int i = (BLOCKSIZE - 1); i >= 0; i--) { nonceInt += (nonceInt << 8) + scratch[i]; } ostringstream oss; oss << nonceInt; string nonceStr = oss.str(); return nonceStr; }
void CTwofishModule::encrypt(Tools::CSecureMemory &rCypherText, Tools::CSecureMemory const &rPlainText) const { FASSERT(mKey.getSize() == gKeySize); FASSERT((rPlainText.getSize() % gBlockSize) == 0); Tools::CSecureMemory IV; IV.allocate(gIVSize); RandomGenerator.GenerateBlock(&IV[0], IV.getSize()); CBC_Mode<Twofish>::Encryption Cipher; Cipher.SetKeyWithIV(&mKey[0], mKey.getSize(), &IV[0], IV.getSize()); rCypherText.allocate(rPlainText.getSize() + IV.getSize()); std::memcpy(&rCypherText[0], &IV[0], IV.getSize()); Cipher.ProcessData(&rCypherText[IV.getSize()], &rPlainText[0], rPlainText.getSize()); ASSERT(((rCypherText.getSize() - gIVSize) % gBlockSize) == 0); return; }
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; }