bool Updater::encryptZipFile(const char* szPatchFile, const char* szNewPatchFile) const { //参数检查 assert(szPatchFile); if(!szPatchFile || szPatchFile[0]==0) { setLastError(AXP_ERR_PARAM); return false; } //拷贝文件 if(compareTwoFileName(szPatchFile, szNewPatchFile) != 0) { if(!CopyFile(szPatchFile, szNewPatchFile, FALSE)) { setLastError(AXP_ERR_FILE_WRITE, "File=%s, WinErr: %d", szNewPatchFile, GetLastError()); return false; } } bool isCrced = checkCRCFlag(szNewPatchFile); //加密/解密 if(!encryptFile(szNewPatchFile, isCrced)) return false; //加上校验尾 return attachCRCFlag(szNewPatchFile); }
void Thread::run() { qDebug()<<"run"; if (l_source_files.size()!=l_destination_files.size()) { exec(); emit processFinished(encrypt); return; } if (encrypt) { //encryptFile(v.toString().toLatin1().data(),dest_path.toLatin1().data(),text.toLatin1().data()); for (int i = 0; i< l_source_files.size(); i++) { emit setLabel("Encrypt: " + *l_source_files[i]); encryptFile(l_source_files[i]->toLatin1().data(), l_destination_files[i]->toLatin1().data(), password.toLatin1().data()); } } else { for (int i = 0; i<l_source_files.size(); i++) { emit setLabel("Decrypt: " + *l_source_files[i]); decryptFile(l_source_files[i]->toLatin1().data(), l_destination_files[i]->toLatin1().data(), password.toLatin1().data()); } } emit setLabel(""); emit processFinished(encrypt); exec(); }
/** * Decrypt received facial recognition parameters. * Encrypt facial recognition parameters. */ bool replyToThirdMessage() { cout << "Reply To Third Message" << endl; // Read session key from file SecByteBlock key; readFromFile(COMPUTED_KEY_FILE_NAME, key); // Read in the current initialization vector from file byte curIv[AES::BLOCKSIZE]; // TODO: actually read it in // Set to 0 for now memset(curIv, 0, AES::BLOCKSIZE); // Decrypt received facial recognition params if (!decryptFile(THIRD_MESSAGE_FILE_NAME, RECEIVED_FACIAL_RECOGNITION_FILE_NAME, key, curIv)) { cerr << "Security Error in replyToThirdMessage. MAC could not be verified." << endl; return false; } // Encrypt facial recognition params encryptFile(FACIAL_RECOGNITION_FILE_NAME, THIRD_MESSAGE_REPLY_FILE_NAME, key, curIv); return true; }
void NewEntryDlg::on_pathBtn_clicked() { if(mIsFileMode) encryptFile(); else encryptDir(); }
// main is a menu method int main (int argc, char *argv[]) { int status = 0; if (argv[1] != NULL && (strcmp(argv[1], "-e") == 0 || strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "-h") == 0)) { if (argv[2] != NULL && argv[3] != NULL && argv[4] != NULL) { if (strcmp(argv[1], "-e") == 0) { // check for distance, defaults to 0. if (argc == 6) { status = encryptFile("%t %k %o %d", argv[3], argv[2], argv[4], atoi(argv[5])); } else { status = encryptFile("%t %k %o", argv[3], argv[2], argv[4]); } } else if (strcmp(argv[1], "-d") == 0) { status = decryptFile(argv[3], argv[2], argv[4]); } else if (strcmp(argv[1], "-h") == 0) { if (argv[5] != NULL) { status = hackFile(argv[2], argv[3], argv[4], argv[5]); } else { printf("\nCorrect format is \"assignment2 -h <(targetfile).txt>"); printf(" <(outputFile).txt> <keyDir> <DictionaryDir>\"\n\n"); status = 0; } } } else { printf("\nCorrect format is \"assignment2 -[edh] " "<(keyfile).txt)|(targetFile).txt>"); printf(" <(targetfile).txt|(outputFile).txt> " "<(outputFile).txt|keyDir> [distance|<dictionaryDir>]\"\n\n"); status = 0; } } else { printf("\nNot recognized control. \"-[edh]\"\n\n"); status = 0; } if (statusMessage != NULL) { printf("%s", statusMessage); } return status; }
// Sets the color of the button to green if it was clicked and acts accordingly. void RunManager::handleClick(string key) { // If ECB makes button green and sets the cipher type accordingly. if (key == "ECB") { cipherType = 0; ECBButton.setColor(sf::Color(0, 255, 0)); ECBText.setColor(sf::Color(0, 255, 0)); } // Same but for CBC. if (key == "CBC") { cipherType = 1; CBCButton.setColor(sf::Color(0, 255, 0)); CBCText.setColor(sf::Color(0, 255, 0)); } // Etc. if (key == "CTR") { cipherType = 2; CTRButton.setColor(sf::Color(0, 255, 0)); CTRText.setColor(sf::Color(0, 255, 0)); } // If the encrypt button was clicked the program will encrypt the file based on the location given. if (key == "encrypt") encryptFile(); // If the decrypt button was clicked. else if (key == "decrypt") decryptFile(); // If encrypt file location was clicked. if (key == "eFile") { // Informs program the user is typing in the encryption file location. typing = 1; // Sets encryption file location color. encryptFileSelector.setColor(sf::Color(0, 255, 0)); encryptFileLocationText.setColor(sf::Color(0, 255, 0)); decryptFileSelector.setColor(sf::Color(255, 255, 255)); decryptFileLocationText.setColor(sf::Color(0, 0, 0)); } // Same but for decryption file location. else if (key == "dFile") { typing = 2; decryptFileSelector.setColor(sf::Color(0, 255, 0)); decryptFileLocationText.setColor(sf::Color(0, 255, 0)); encryptFileSelector.setColor(sf::Color(255, 255, 255)); encryptFileLocationText.setColor(sf::Color(0, 0, 0)); } // If the user was typing but selects a button other than the two file locations, it unhighlights them both and infroms the program the user has stopped typing. if (typing && key != "eFile" && key != "dFile") { typing = 0; encryptFileSelector.setColor(sf::Color(255, 255, 255)); encryptFileLocationText.setColor(sf::Color(0, 0, 0)); decryptFileSelector.setColor(sf::Color(255, 255, 255)); decryptFileLocationText.setColor(sf::Color(0, 0, 0)); } }
int main() { char sourceFileName[MAXLENGTH]; int encryptFileLength = MAXLENGTH + 11; char encryptFileName[encryptFileLength]; char decryptFileName[encryptFileLength]; int encryptFile(char fileName[],char encryptFileName[]); int decryptFile(char encryptfileName[],char decryptFileName[]); printf("Enter the name of file\n"); scanf("%s",sourceFileName); if (strlen(sourceFileName) > MAXLENGTH-1 || strlen(sourceFileName) < 1 ) { printf("File name must be within range of 1 to %d\n",MAXLENGTH-1); return 0; } sprintf(encryptFileName,"%s_encrypted",sourceFileName); if (strlen(encryptFileName) > encryptFileLength-1 || strlen(encryptFileName) < 1 ) { printf("File name must be within range of 1 to %d\n",encryptFileLength-1); return 0; } sprintf(decryptFileName,"%s_decrypted",sourceFileName); if (strlen(decryptFileName) > encryptFileLength-1 || strlen(decryptFileName) < 1 ) { printf("File name must be within range of 1 to %d\n",encryptFileLength-1); return 0; } if (encryptFile(sourceFileName,encryptFileName)) { if (!decryptFile(encryptFileName,decryptFileName)) { printf("File decryption failed \n"); return 0; } } else { printf("File encryption failed \n"); return 0; } return 1; }
/** * Process public key and random number received from other user. * Encrypt facial recognition parameters. */ bool thirdMessage() { cout << "Third Message" << endl; // Split received file into random number and public key vector<string> outputFiles; outputFiles.push_back(OTHER_FIRST_MESSAGE_RANDOM_NUMBER); outputFiles.push_back(OTHER_PUBLIC_KEY_FILE_NAME); vector<int> bytesPerFile; bytesPerFile.push_back(RANDOM_NUMBER_SIZE); splitFile(FIRST_MESSAGE_REPLY_FILE_NAME, outputFiles, bytesPerFile); // Read from file SecByteBlock privateKey; SecByteBlock otherPublicKey; readFromFile(PRIVATE_KEY_FILE_NAME, privateKey); readFromFile(OTHER_PUBLIC_KEY_FILE_NAME, otherPublicKey); // Compute shared secret SecByteBlock sharedSecret; if (!diffieHellmanSharedSecretAgreement(sharedSecret, otherPublicKey, privateKey)) { cerr << "Security Error in thirdMessage. Diffie-Hellman shared secret could not be agreed to." << endl; return false; } // Compute key from shared secret SecByteBlock key; generateSymmetricKeyFromSharedSecret(key, sharedSecret); // Write shared secret and computed key to file writeToFile(SHARED_SECRET_FILE_NAME, sharedSecret); writeToFile(COMPUTED_KEY_FILE_NAME, key); // Read in the current initialization vector from file byte curIv[AES::BLOCKSIZE]; // TODO: actually read it in // Set to 0 for now memset(curIv, 0, AES::BLOCKSIZE); // Encrypt facial recognition params encryptFile(FACIAL_RECOGNITION_FILE_NAME, THIRD_MESSAGE_FILE_NAME, key, curIv); return true; }
bool callFunction(std::string get, int scramblers[3+1][26], int shift[3], int rotor[3]){ // make input string lower case for(size_t i = 0, n = get.length(); i < n; i++) get[i] = tolower(get[i]); if(get.compare("end") == 0) return false; if(get.compare("help") == 0) help(); else if(get.compare("generate_key") == 0 or get.compare("gk") == 0) generateKey(); else if(get.compare("generate_scrambler") == 0 or get.compare("gs") == 0) generateScrambler(scramblers); else if(get.compare("set_key") == 0 or get.compare("sk") == 0) setKey(scramblers); else if(get.compare("encrypt") == 0 or get.compare("e") == 0) encrypt(scramblers, shift, rotor); else if(get.compare("set_shift") == 0 or get.compare("ss") == 0) setShift(shift); else if(get.compare("set_rotor") == 0 or get.compare("sr") == 0) setRotor(rotor); else if(get.compare("reset_shift") == 0 or get.compare("rs") == 0) resetShift(shift); else if(get.compare("print_shift") == 0 or get.compare("ps") == 0) printShift(shift); else if(get.compare("print_rotor") == 0 or get.compare("pr") == 0) printRotor(rotor); else if(get.compare("encrypt_file") == 0 or get.compare("ef") == 0) encryptFile(scramblers, shift, rotor); else if(get.compare("capitalize") == 0 or get.compare("c") == 0) capitalize(); else if(get.compare("set_plugboard") == 0 or get.compare("sp") == 0) setPlugboard(); else if(get.compare("save") == 0 or get.compare("s") == 0) save(scramblers, shift, rotor); else if(get.compare("load") == 0 or get.compare("l") == 0) load(scramblers, shift, rotor); else unrecognized(); return true; }
int C3dsTool::Action() { if (m_eAction == kActionExtract) { if (!extractFile()) { printf("ERROR: extract file failed\n\n"); return 1; } } if (m_eAction == kActionCreate) { if (!createFile()) { printf("ERROR: create file failed\n\n"); return 1; } } if (m_eAction == kActionEncrypt) { if (!encryptFile()) { printf("ERROR: encrypt file failed\n\n"); return 1; } } if (m_eAction == kActionUncompress) { if (!uncompressFile()) { printf("ERROR: uncompress file failed\n\n"); return 1; } } if (m_eAction == kActionCompress) { if (!compressFile()) { printf("ERROR: compress file failed\n\n"); return 1; } } if (m_eAction == kActionTrim) { if (!trimFile()) { printf("ERROR: trim file failed\n\n"); return 1; } } if (m_eAction == kActionPad) { if (!padFile()) { printf("ERROR: pad file failed\n\n"); return 1; } } if (m_eAction == kActionDiff) { if (!diffFile()) { printf("ERROR: create patch file failed\n\n"); return 1; } } if (m_eAction == kActionPatch) { if (!patchFile()) { printf("ERROR: apply patch file failed\n\n"); return 1; } } if (m_eAction == kActionSample) { return sample(); } if (m_eAction == kActionHelp) { return Help(); } return 0; }
int main( int argc, char *argv[] ) { std::string inputfile = ""; std::string outputfile = ""; std::string passphrase = ""; std::string publicKey = ""; bool encrypt = false, decrypt = false, keygen = false; // set up command line options and // check for help requests and usage errors po::variables_map var_map; try { init_program_options(argc, argv, &var_map); } catch( po::multiple_occurrences m ) { std::cerr << "Error: Please use each options at most once." << std::endl << std::endl; print_usage( argv[0] ); exit( 2 ); } catch( ... ) { std::cerr << "Error: unknown error in command line options." << std::endl << std::endl; print_usage( argv[0] ); exit( 2 ); } // no options -> print usage message if( argc == 1 ) { print_usage( argv[0] ); exit( 1 ); } // check command line options if( var_map.count("test") > 0 ) { exit( run_tests() ); } encrypt = (var_map.count("encrypt") > 0); decrypt = (var_map.count("decrypt") > 0); keygen = (var_map.count("create-key") > 0); // check that the user only request one out of decrypt/encrypt/keygen if( (int)decrypt + (int)encrypt + (int)keygen > 1 ) { std::cerr << "You need to chose one of encrypt, decrypt and key-gen." << std::endl << std::endl; print_usage( argv[0] ); exit( 2 ); } if (var_map.count("pass") > 0) passphrase = var_map["pass"].as<std::string>(); if (var_map.count("input") > 0) inputfile = var_map["input"].as<std::string>(); if (var_map.count("output") > 0) outputfile = var_map["output"].as<std::string>(); if (var_map.count("key") > 0) publicKey = var_map["key"].as<std::string>(); quiet = (var_map.count("quiet") > 0); // here comes the program logic if( keygen ) { if( passphrase == "" ) passphrase = prompt_password( "passphrase", true ); uint8_t publicKey[32]; if( generate_key( publicKey, passphrase ) ) std::cerr << "Public key: " << b64encode( publicKey ); else std::cerr << "Failed to generate public key" << std::endl; } else if( encrypt ) { if( inputfile == "" && publicKey == "" ) { std::cerr << "Error: Public key needs to be given on " << "commandline when encrypting from stdin" << std::endl; exit( 1 ); } if( publicKey == "" ) publicKey = prompt_password( "public key" ); encryptFile( outputfile, inputfile, passphrase, publicKey ); } else if( decrypt ) { if( inputfile == "" && passphrase == "" ) { std::cerr << "Error: passphrase needs to be given on " << "commandline when decrypting from stdin" << std::endl; exit( 1 ); } if( passphrase == "" ) passphrase = prompt_password( "passphrase", true ); decryptFile( outputfile, inputfile, passphrase ); } else { print_usage( argv[0] ); exit( 0 ); } return 0; }
int main() { int newpwd, newpwda; // 更改密码时,用户两次输入的新密码 char sourcefile[30], // 加密/解密的文件名 targetFile[30], // 解密/加密后要保存的文件名 secretKey[21], // 文件加密的密钥 action; // 要进行的操作 // 操作前先校验使用密码 if(!checkPwd(1)) { printf("Sorry,check failed!please press any key to quit...\n"); getch();//吃掉按键不回显 exit(1); } // 密码校验成功,进入程序 while(1) { system("cls"); // 清屏 printMenu(); // 显示菜单 scanf("%c",&action); // 输入要执行的操作 fflush(stdin); // 清空stdin缓冲区 system("cls"); switch(action) { case 'z': printf("Bye!"); exit(0); break; case 'a': while(1) { printf("Enter the name of sourcefile(route):\n"); scanf("%s", sourcefile); printf("Enter secretkey:\n"); //密钥是用户自己定义的,可以随意给需要加密的文件添加密钥 scanf("%s", secretKey); printf("Enter the name of targetfile(route):\n"); //给加密后的文件命名,并保存 scanf("%s",targetFile); if( encryptFile(sourcefile, secretKey, targetFile) ) { printf("Congratulation!,encrypt [%s] successfully,save in[%s].\n", sourcefile, targetFile); } if(redo() == 'a') // 继续加密文件 { system("cls"); } else // 回到主菜单 { break; } } break; case 'b': while(1) { printf("Enter the name of sourcefile(route):\n"); scanf("%s",sourcefile); printf("Enter secretkey:\n"); scanf("%s",secretKey); printf("Enter the name of targetfile(route):\n"); //对解密的文件系统又可以提供保存路径 scanf("%s", targetFile); if( encryptFile(sourcefile, secretKey, targetFile) ) { printf("Congratulation!,decrypt [%s] successfully,save in[%s].\n", sourcefile, targetFile); } if(redo() == 'a') // 继续解密文件 { system("cls"); } else { break; // 回到主菜单 } } break; case 'c': while(1) { if(!checkPwd(2)) { printf("Sorry,check failed!please press any key to quit...\n"); getch(); exit(0); } printf("Please enter new password\n"); scanf("%d",&newpwd); printf("Please enter new password again\n"); scanf("%d",&newpwda); if(newpwd==newpwda) // 两次输入密码相同才成功 { password = newpwd; printf("Congrarulation!Change password successfully!\n"); } else { printf("Sorry,twice enter is not same,change failed!\n"); } if(redo() == 'a') // 继续修改密码 { system("cls"); } else // 回到主菜单 { break; } } break; default: printf("No such choice,please press any key back to menu...\n"); getch(); } } return 0; }
// header and do while switch that keeps showing you options until you exit int main(){ int choice; printf("Roncon Contact Directory\n"); printf("========================\n"); initList(); load(); do{ choice = showMenu(); switch(choice){ case 0: // exit printf("Goodbye\n"); break; case 1: // view all printf("View all:\n"); if(listHead->next != listTail){ view(); } else{ printf("No contacts\n"); } break; case 2: // search printf("Search:\n"); if(encrypted){ printf("Decrypt file first!\n"); } else{ search(); } break; case 3: // add printf("Add: \n"); if(encrypted){ printf("Decrypt file first!\n"); } else{ add(); } break; case 4: // remove printf("Remove:\n"); if(encrypted){ printf("Decrypt file first!\n"); } else{ rem(); } break; case 5: // edit printf("Edit:\n"); if(encrypted){ printf("Decrypt file first!\n"); } else{ edit(); } break; case 6: // sort printf("Sort:\n"); if(listHead->next != listTail){ sort(); } else{ printf("No contacts\n"); } break; case 7: // encrypt printf("En/Decrypt:\n"); encryptFile(); break; default: printf("Not a valid number, re-enter.\n"); break; } // switch }while(choice != 0); save(); printf("\n\n"); system("pause"); } // main
int clientUploadFile(BIO *conn, char *filename) { /* CALCULATE AND STORE ANY RECORDS OF THIS FILE WE NEED * BEFORE UPLOADING IT */ FILE *ifp = fopen(filename, "rb"); if ( ifp == NULL ) return NO_SUCH_FILE; //generate key and iv for encryption unsigned char *key = randomBytes(32); unsigned char *iv = randomBytes(32); //encrypt the file int status = encryptFile(filename, TEMP_ENCRYPTED_FILENAME, key, iv); if(status == -1) { fprintf(stderr, "Failed to encrypt %s in clientUploadFile()\n", filename); return -1; } //we need to store NUM_HASHES salts and digests for later verification unsigned char *salts[NUM_HASHES]; unsigned char *hashes[NUM_HASHES]; for(int i=0; i<NUM_HASHES; ++i) { //generate a random salt salts[i] = randomBytes(SALT_LENGTH); //compute the digest for the file with that salt hashes[i] = calculateMD5(TEMP_ENCRYPTED_FILENAME, salts[i], SALT_LENGTH); if(hashes[i] == NULL) { fprintf(stderr, "Failed to calculate digest in clientUploadFile()\n"); return -1; } } //store all this data for later status = addRecord(filename, 0, hashes, salts, key, iv); if(status == -1) { fprintf(stderr, "addRecord() failed for in clientUploadFile()\n"); return -1; } //free the memory we allocated above for(int i=0; i < NUM_HASHES; ++i) { free(salts[i]); free(hashes[i]); } free(key); free(iv); /* START THE ACTUAL COMMUNICATION WITH THE SERVER */ //send the code which causes the server to call serverUploadFile() if(writeInt(conn, UPLOAD_FILE_CODE) == -1) return -1; //send the fileSize int fileSize = sizeOfFile(TEMP_ENCRYPTED_FILENAME); if(writeInt(conn, fileSize) == -1) return -1; printf("NOTE: Original size: %f MB. Encrypted size: %f MB.\n", (double)sizeOfFile(filename)/MEGABYTE, (double)fileSize/MEGABYTE); //wait for an int telling us the balance owing unsigned int fee = readInt(conn); if(fee > 0) { printf("Purchase %d more cloud dollar(s) to upload this file.\n", fee); removeRecord(filename); return -1; } else if(fee < 0) return -1; //send the file if(writeFile(conn, TEMP_ENCRYPTED_FILENAME, filename) < 1) return -1; unlink( TEMP_ENCRYPTED_FILENAME ); printf("Succesfully uploaded the file.\n"); return 0; }