void TcpServer::incomingConnection(qintptr socketDescriptor) { LocalSocket * tcpTemp = new LocalSocket(socketDescriptor,this); QString thisHost; qint16 thisPort; if (isSerCon) { if (!initLocalProxy(thisHost,thisPort,tcpTemp)) return; data.operater = 1; data.socketID = socketDescriptor; data.userID = this->userID; newHost = qMakePair(thisHost,thisPort); if (!serializeData(bytearry,newHost)) return ; data.data = encryptData(aes,bytearry); if (sentServerData()) { connect(tcpTemp,&LocalSocket::readyRead,this,&TcpServer::LocalSocketRead); connect(tcpTemp,&LocalSocket::disconnected,this,&TcpServer::localSockedDisCon); tcpClient->insert(socketDescriptor,tcpTemp); localDataRead(tcpTemp); } else { tcpTemp->disconnectFromHost(); tcpTemp->deleteLater(); } } else { tcpTemp->close(); tcpTemp->deleteLater(); serverSocket->connectToHost(ConfigClass::getClass().serverUrl,ConfigClass::getClass().serverPort); if (serverSocket->waitForConnected(5000)) { isSerCon = true; } return; } }
int autoRun() { getData(); if(MyStatus == GETDATA) { encryptData(saveFileName); } else { infoOutput("Error: 上一步骤运行失败,数据文件获取失败\n"); return -1; } if(MyStatus == ENCRYPT ) { sendToBank(); } else { infoOutput("Error: 上一步骤加密失败。\n"); return -1; } if(MyStatus == SENDTOBANK) { infoOutput("Auto: 自动执行过程成功结束。\n"); return 0; } else { infoOutput("Auto: 自动执行过程失败。\n"); return -1; } }
static int encryptDataLoop(FILE *file_in, FILE *file_out, CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) { CK_SIZE dataLen = 0; CK_CHAR** ppEncData = NULL; CK_SIZE* pEncDataLen = 0; int i = 0; int N = 8; char buffer[8]; CK_RV rv = CKR_OK; CK_CHAR* pData = buffer; do{ N = fread(buffer, 1, 8, file_in); for (i = N; i < 8; i++) { buffer[i] = 0; } if (N==0) return 0; rv = encryptData(hSession, hKey, pData, 8, &ppEncData, &pEncDataLen); CHECK_CK_RV_GOTO(rv, "findObject", end); fwrite(ppEncData, 1, 8, file_out); } while(N==8); end: return 0; }
void TcpServer::localDataRead(LocalSocket * sock) { if (sock->bytesAvailable() <= 0) return; data.operater = 0; data.socketID = sock->getSocketID(); data.userID = this->userID; data.data = encryptData(aes,sock->readAll()); sentServerData(); }
void ClientSocket::remoteData() { RemoteSocket * sock = qobject_cast<RemoteSocket *>(QObject::sender()); Q_ASSERT(sock); data.operater = 0; data.socketID = sock->getSocketID(); data.userID = this->userID; data.data = encryptData(aes,sock->readAll()); sentClientData(); }
const char *ADBColumn::insStr() { if (workStr) free(workStr); switch (intDataType) { case FIELD_TYPE_TINY: case FIELD_TYPE_SHORT: workStr = (char *) calloc(32, sizeof(char)); sprintf(workStr, "%d", atoi(intData)); break; case FIELD_TYPE_LONG: workStr = (char *) calloc(32, sizeof(char)); sprintf(workStr, "%ld", atol(intData)); break; case FIELD_TYPE_LONGLONG: workStr = (char *) calloc(64, sizeof(char)); sprintf(workStr, "%qd", atoll(intData)); break; case FIELD_TYPE_DOUBLE: case FIELD_TYPE_FLOAT: workStr = (char *) calloc(64, sizeof(char)); sprintf(workStr, "%f", atof(intData)); break; // Everything else is a string, so escape it and wrap it in quotes. default: if (intIsEncrypted) { workStr = (char *) calloc(16, sizeof(char)); encryptData(); int tmpLen = strlen(workStr); char *tmpStr = (char *) calloc(tmpLen*2+32, sizeof(char)); mysql_escape_string(tmpStr, workStr, tmpLen); free(workStr); workStr = (char *) calloc(strlen(tmpStr)+64, sizeof(char)); strcpy(workStr, "'"); strcat(workStr, tmpStr); strcat(workStr, "'"); free(tmpStr); } else { int tmpLen = 0; if (intData) tmpLen = strlen(intData); workStr = (char *) calloc(tmpLen * 2 + 16, sizeof(char)); char *tmpStr = (char *) calloc(tmpLen * 2 + 16, sizeof(char)); if (intData) mysql_escape_string(tmpStr, intData, tmpLen); strcpy(workStr, "'"); strcat(workStr, tmpStr); strcat(workStr, "'"); free(tmpStr); } break; } return workStr; }
bool CloudReconnectWorker::getDecryptedConnectionData(std::string& decryptedData) { bool newEncryptedDataFileUsed = false; std::string encryptedDataFile; std::string newEncryptedDataFile; m_cloudStatus.getPathCloudServerDataFile(encryptedDataFile); m_cloudStatus.getPathNewCloudServerDataFile(newEncryptedDataFile); std::ifstream binaryFile(newEncryptedDataFile.c_str(), std::ifstream::binary); if (binaryFile.is_open()) { LOG_VNotice(core, "Using new encrypted data to reconnect.\n"); binaryFile.close(); newEncryptedDataFileUsed = true; } else { LOG_VNotice(core, "Using old encrypted data to reconnect.\n"); std::ifstream binaryFile(encryptedDataFile.c_str(), std::ifstream::binary); if (binaryFile.is_open()) { binaryFile.close(); } else { LOG_VError(core, "Could not open encrypted binary file [%s]\n", encryptedDataFile.c_str()); return false; } } bool decryptOk = false; EncryptData encryptData(*m_pSpotSession); if(newEncryptedDataFileUsed) { decryptOk = encryptData.decryptConnectionData(newEncryptedDataFile, decryptedData); if(decryptOk) { replaceOldEncryptedConnectionData(encryptedDataFile, newEncryptedDataFile); } } else { decryptOk = encryptData.decryptConnectionData(encryptedDataFile, decryptedData); } return decryptOk; }
int addEntry(char *fileName, char *msg) { int logType = NORMAL_MSG; char *encData; char *msgAuthCode; _logAuthKey = hash(_logAuthKey); //A+1 = H(A) _sessionKey = createKey(NORMAL_MSG, _logAuthKey); //K encData = encryptData(msg, _sessionKey, strlen(msg)); //MSG Authentication _hashChain = createY(_hashChain, encData, logType); //Y+1 = H(y, encData, logtype) msgAuthCode = genMAC(_logAuthKey, _hashChain); //Z = MAC(Y) struct ALogEntry *newEntry = createALogEntry(logType, encData, _hashChain, msgAuthCode); writeAEntry(newEntry, fileName); }
int closeLog(char *fn) { char *encData; char *msgAuthCode; char *msg = intToStr(getTimeStamp()); _logAuthKey = hash(_logAuthKey); //A+1 = H(A) _sessionKey = createKey(NORMAL_CLOSE, _logAuthKey); //K encData = encryptData(msg, _sessionKey, strlen(msg)); //MSG Authentication _hashChain = createY(_hashChain, encData, NORMAL_CLOSE); //Y+1 = H(y, encData, logtype) msgAuthCode = genMAC(_logAuthKey, _hashChain); //Z = MAC(Y) struct ALogEntry *finalLog = createALogEntry(NORMAL_CLOSE, encData, _hashChain, msgAuthCode); writeAEntry(finalLog, fn); currentFile=NULL; }
int writeToSDEncrypted(char* buffer, char* fileName) { alt_up_sd_card_dev *device_reference = NULL; short int myFileHandle; int i; char *encryptedBuffer = encryptData(buffer); printf("\nebuffer %s\n", encryptedBuffer); printf("Opening SDCard\n"); if ((device_reference = alt_up_sd_card_open_dev( "/dev/Altera_UP_SD_Card_Avalon_Interface_0")) == NULL) { printf("SDCard Open FAILED\n"); return -1; } else { printf("SDCard Open PASSED\n"); } if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { myFileHandle = alt_up_sd_card_fopen(fileName, false); if (myFileHandle == -1) { myFileHandle = alt_up_sd_card_fopen(fileName, true); } if (myFileHandle != -1) { printf("File Opened\n"); for (i = 0; i < strlen(encryptedBuffer); i++) { if (alt_up_sd_card_write(myFileHandle, encryptedBuffer[i]) == false) { printf("Error writing to file...\n"); return -1; } } printf("Done!!!\n"); alt_up_sd_card_fclose(myFileHandle); } else { printf("File NOT Opened\n"); } } } return 0; }
/** * @brief Encrypts data. * @note Uses the default profile's key. * @param data Data to encrypt. * @return Encrypted data. */ QByteArray Core::encryptData(const QByteArray &data) { return encryptData(data, Nexus::getProfile()->getPasskey()); }
void EncryptWrapper::run(){ emit getData(encryptData()); }
int main(int argc,char **argv) { init();//初始化 if (argc == 2) { if(0==strcmp(argv[1],"-s")) { Silent_Mode = 1; //puts("SilentMode\n"); } } if(1 == Silent_Mode) //自动执行模式 { infoOutput("Auto: Start\n"); if(0 != autoRun()) { infoOutput("Error: BatchRunning Error!CheckLog!"); return 1; } else { infoOutput("Auto: Successful\n"); return 0; } } if (0 == Silent_Mode) //交互式模式 { while(1) { showMenu(); char choose = '\0'; printf("\n请选择:<1-q>:"); choose = getCommand(COMMAND); //getCommand(x)函数选项COMMAND和WAIT,用于返回第一个字符,并清空缓冲区 switch(choose) { case 'i': importKey(); break; case 'q': if(logfp != NULL) fclose(logfp); infoOutput("----------程序退出-----------\n"); exit(1); break; case '1': getData(); unlink("store.dat"); infoOutput("Download: 清除临时文件完成。"); getCommand(WAIT); break; case '2': if(strcmp(saveFileName,"store_wanxiang_")!=0) { encryptData(saveFileName); } else { infoOutput("Error: 数据文件尚未下载,请先下载。\n"); } getCommand(WAIT); break; case '3': sendToBank(); getCommand(WAIT); break; case 'a': showAbout(); break; } } } }
int main (int argc, char **argv) { //Some code adapted from linuxhowtos.org/C_C++/socket.htm and lecture //Check number of command line arguments if (argc != 2) { fprintf(stderr, "otp_end_d: Invalid number of arguments\n"); exit (1); } //Initialize socket file descriptors and port # int sockfd, newsockfd, portno; //Initialize size of the address of client socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; int bytesin; //Create a new socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("otp_enc_d socket failed"); exit(1); } //Convert port # to integer portno = atoi(argv[1]); //Set up the server struct serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); //Bind the socket to a port if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1) { perror("otp_enc_d bind failed"); exit(1); } //List on the socket with up to 5 connections in queue if (listen(sockfd, 5) == -1) { perror("otp_enc_d listen failed"); exit(1); } //Get sizeof clilen clilen = sizeof(cli_addr); int status; pid_t pid; //Fork for multiple connections while (1) { //Accept call from client newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd == -1) { perror("otp_enc_d accept failed"); exit(1); } pid = fork(); if (pid == 0) { //This is the child process close(sockfd); encryptData(newsockfd); exit(0); } else if (pid > 0) { //This is the parent process //Wait for process to complete so no zombies pid_t cpid = waitpid(pid, &status, 0); if (cpid == -1) { perror("Wait failed foreground"); exit(1); } } else { //An error occured close(newsockfd); perror("fork failed"); exit(1); } } //Close the sockets close(sockfd); return 0; }
bool XorEncryptor::decryptData(std::fstream &original, std::fstream &result) { return encryptData(original, result); }
int createLog(char *fn) { logID = createRandomNum(); stepNum = 0; ////////////////STARTUP from U//////////////// //create first message //INIT values char *x = "aaaaaaaaaaaaa"; char *hashX = hash(x); _hashChain = (char *)malloc(20+1); // the initial hash chain memset(_hashChain, 'a', 20+1); _logAuthKey = intToStr(createRandomNum()); //A A0 = _logAuthKey; char *msgAuthCode; //Z //_sessionKey = createFirstKey(); //K _sessionKey = createKey(LOG_INIT, _logAuthKey); //create msg for T struct Msg *msg = createMsg(stepNum, ID_UNTRUSTED, PUB_KEY_T, PRIV_KEY_U, _sessionKey, x); //create first log entry char *data = logToStr(createLogEntry(LOG_INIT, logID, msg)); //char *data = logToStr2(createLogEntry(LOG_INIT, logID, msg)); char *encData = encryptData(data, _sessionKey, strlen(data)); _hashChain = createY(_hashChain, encData, LOG_INIT); msgAuthCode = genMAC(_logAuthKey, _hashChain); struct ALogEntry *firstLog = createALogEntry(LOG_INIT, encData, _hashChain, msgAuthCode); writeAEntry(firstLog, fn); //////////////END STARTUP from U//////////////// /////////////RECIEVE T////////////// //verify the message int result = verifyMsg(msg, PRIV_KEY_T, PUB_KEY_U); //printf("Result from T:%d\n", result); //TODO: check valid certificate //increment protocol step ID; int p = msg->p + 1; //create X1 char *x0 = getX(msg, PRIV_KEY_T, PUB_KEY_U); char *x1 = "ZZZ"; //create session key char *sessionKeyT = createKey(RESP_MSG, _logAuthKey); //create msg struct Msg *msg1 = createMsg(p, ID_TRUSTED, PUB_KEY_U, PRIV_KEY_T, sessionKeyT, x1); /////////////END RECIEVE T////////////// /////////////FINALIZE INIT U/////////////////// //verify the msg result = verifyMsg(msg1, PRIV_KEY_U, PUB_KEY_T); //printf("Result from U:%d\n", result); //get the data data = logToStr(createLogEntry(RESP_MSG, logID, msg1)); //update hash chains and keys _logAuthKey = hash(_logAuthKey); //A+1 = H(A) _sessionKey = createKey(NORMAL_MSG, _logAuthKey); //K encData = encryptData(data, _sessionKey, strlen(data)); //MSG Authentication _hashChain = createY(_hashChain, encData, RESP_MSG); //Y+1 = H(y, encData, logtype) msgAuthCode = genMAC(_logAuthKey, _hashChain); //Z = MAC(Y) struct ALogEntry *secondLog = createALogEntry(RESP_MSG, encData, _hashChain, msgAuthCode); writeAEntry(secondLog, fn); }