bool AES_CBC_Encode(const unsigned char* key, int keyLen, unsigned char* initVector, const unsigned char* input, int inputOctets, unsigned char* outBuffer) { Rijndael oRijndael; oRijndael.init(Rijndael::CBC, Rijndael::Encrypt, key, static_cast<Rijndael::KeyLength>(keyLen), initVector); int nLength = oRijndael.padEncrypt(input, inputOctets, outBuffer); return (nLength > 0)?true:false; }
bool CEncryption::Encrypt(const unsigned char* pInput, int nLenInput, const char* szPassword, unsigned char*& pOutput, int& nLenOutput) { bool bResult = false; TD_TLHEADER hdr; RD_UINT8 uFinalKey[32]; unsigned long uFileSize = 0, uAllocated = 0, pos = 0; int nEncryptedPartSize = 0; ASSERT(NULL != pInput); if(NULL == pInput) return FALSE; ASSERT(0 != nLenInput); if(0 == nLenInput) return FALSE; ASSERT(NULL != szPassword); if(NULL == szPassword) return FALSE; uFileSize = nLenInput + sizeof(TD_TLHEADER); // Allocate enough memory uAllocated = uFileSize + 16; pOutput = new unsigned char[uAllocated]; if(NULL != pOutput) { unsigned long uKeyLen; // Build header structure hdr.dwSignature1 = TD_TLSIG_1; hdr.dwSignature2 = TD_TLSIG_2; hdr.dwKeyEncRounds = m_dwKeyEncRounds; // Make up the master key hash seed and the encryption IV m_random.GetRandomBuffer(hdr.aMasterSeed, 16); m_random.GetRandomBuffer((BYTE *)hdr.aEncryptionIV, 16); m_random.GetRandomBuffer(hdr.aMasterSeed2, 32); // Create MasterKey by hashing szPassword uKeyLen = strlen(szPassword); ASSERT(0 != uKeyLen); if(0 != uKeyLen) { sha256_ctx sha32; sha256_begin(&sha32); sha256_hash((unsigned char *)szPassword, uKeyLen, &sha32); sha256_end(m_pMasterKey, &sha32); // Generate m_pTransformedMasterKey from m_pMasterKey if(TRUE == _TransformMasterKey(hdr.aMasterSeed2)) { // Hash the master password with the generated hash salt sha256_begin(&sha32); sha256_hash(hdr.aMasterSeed, 16, &sha32); sha256_hash(m_pTransformedMasterKey, 32, &sha32); sha256_end((unsigned char *)uFinalKey, &sha32); // Hash the tasklist contents sha256_begin(&sha32); sha256_hash((unsigned char *)pInput, nLenInput, &sha32); sha256_end((unsigned char *)hdr.aContentsHash, &sha32); // Hash the header sha256_begin(&sha32); sha256_hash((unsigned char *)&hdr + 32, sizeof(TD_TLHEADER) - 32, &sha32); sha256_end((unsigned char *)hdr.aHeaderHash, &sha32); bResult = true; } } } if (bResult) { bResult = false; // Now we have all to build up the header memcpy(pOutput, &hdr, sizeof(TD_TLHEADER)); Rijndael aes; // Initialize Rijndael/AES if(RIJNDAEL_SUCCESS == aes.init(Rijndael::CBC, Rijndael::Encrypt, uFinalKey, Rijndael::Key32Bytes, hdr.aEncryptionIV) ) { nEncryptedPartSize = aes.padEncrypt((RD_UINT8 *)pInput, nLenInput, (RD_UINT8 *)pOutput + sizeof(TD_TLHEADER)); // Check if all went correct ASSERT(0 <= nEncryptedPartSize); if(0 <= nEncryptedPartSize) { bResult = true; // data encrypted successfully } nLenOutput = sizeof(TD_TLHEADER) + nEncryptedPartSize; } } if (!bResult) { SAFE_DELETE_ARRAY(pOutput); } return (bResult); }
bool App::saveTree() { // backup dir checking (see also preferences.cpp) if (preferences.saveBackupFile) { QDir backupDir(preferences.backupLocation); if (!backupDir.exists()) { QMessageBox::critical(this, "Invalid directory", "Please, enter the existing\ndirectory in backup location.", 0, 0); return false; } } QFile saveF(Global::applicationFileName("iqnotes", currentFile + ".xml")); QFile rijnF(Global::applicationFileName("iqnotes", currentFile + ".rijn")); QString saveS; QTextStream saveData(saveS, IO_WriteOnly); saveData.setEncoding(saveData.UnicodeUTF8); saveData << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<iqnotes>\n" << "<config>\n" << "<backup save=\"" << (preferences.saveBackupFile ? "yes" : "no") << "\" location=\"" << preferences.backupLocation << "\"/>\n" << "<notes showreminder=\"" << (preferences.showReminder ? "yes" : "no") << "\" />\n" << "<security passwd=\"" << preferences.passwdHex << "\"/>\n" << "</config>\n" << "<entries>\n"; for (Entry *e = entriesList->first(); e; e = entriesList->next()) { saveData << "<entry name=\"" << quoteXML(e->getName()) << "\" defaultpic=\"" << quoteXML(e->getDefaultPic()) << "\">\n"; for (PropertyStruct *ps = e->first(); ps; ps = e->next()) { saveData << "<property name=\"" << quoteXML(ps->getName()) << "\" " << "type=\"" << (ps->getType() == PropertyStruct::ONE_LINE ? "oneLine" : "multiLine") << "\"/>\n"; } saveData << "</entry>\n"; } saveData << "</entries>\n"; notes->saveTree(&saveData); saveData << "</iqnotes>\n"; //SF.close(); QCString dataUTF8 = saveS.utf8(); if (!preferences.passwdHex.length()) { saveF.open(IO_WriteOnly); QTextStream saveData1(&saveF); saveData1.setEncoding(saveData1.UnicodeUTF8); saveData1 << saveS; if (preferences.saveBackupFile) { QFile backupSaveF(preferences.backupLocation + "/" + currentFile + ".xml"); QTextStream saveData2(&backupSaveF); backupSaveF.open(IO_WriteOnly); saveData2 << dataUTF8; } if (rijnF.exists()) rijnF.remove(); } else { Rijndael rijndael; unsigned char *rijnEncrypt = (unsigned char*)malloc(dataUTF8.length() + 16); rijnF.open(IO_WriteOnly); rijndael.init(Rijndael::CBC, Rijndael::Encrypt, preferences.passwd16Bin, Rijndael::Key16Bytes); int len = rijndael.padEncrypt((unsigned char*)(const char*)dataUTF8, dataUTF8.length(), rijnEncrypt); rijnF.writeBlock((const char *)rijnEncrypt, len); if (preferences.saveBackupFile) { QFile rijnBackupF(preferences.backupLocation + "/" + currentFile + ".rijn"); rijnBackupF.open(IO_WriteOnly); rijnBackupF.writeBlock((const char *)rijnEncrypt, len); } free(rijnEncrypt); if (saveF.exists()) saveF.remove(); } return true; }