void writeDraft(const PeerId &peer, const QString &text) { if (!_working()) return; if (text.isEmpty()) { DraftsMap::iterator i = _draftsMap.find(peer); if (i != _draftsMap.cend()) { clearKey(i.value()); _draftsMap.erase(i); _mapChanged = true; _writeMap(); } _draftsNotReadMap.remove(peer); } else { DraftsMap::const_iterator i = _draftsMap.constFind(peer); if (i == _draftsMap.cend()) { i = _draftsMap.insert(peer, genKey()); _mapChanged = true; _writeMap(WriteMapFast); } QString to = _basePath + toFilePart(i.value()); EncryptedDescriptor data(sizeof(quint64) + sizeof(quint32) + text.size() * sizeof(QChar)); data.stream << quint64(peer) << text; FileWriteDescriptor file(i.value()); file.writeEncrypted(data); _draftsNotReadMap.remove(peer); } }
void writeDraftPositions(const PeerId &peer, const MessageCursor &cur) { if (!_working()) return; if (cur.position == 0 && cur.anchor == 0 && cur.scroll == 0) { DraftsMap::iterator i = _draftsPositionsMap.find(peer); if (i != _draftsPositionsMap.cend()) { clearKey(i.value()); _draftsPositionsMap.erase(i); _mapChanged = true; _writeMap(); } } else { DraftsMap::const_iterator i = _draftsPositionsMap.constFind(peer); if (i == _draftsPositionsMap.cend()) { i = _draftsPositionsMap.insert(peer, genKey()); _mapChanged = true; _writeMap(WriteMapFast); } QString to = _basePath + toFilePart(i.value()); EncryptedDescriptor data(sizeof(quint64) + sizeof(qint32) * 3); data.stream << quint64(peer) << qint32(cur.position) << qint32(cur.anchor) << qint32(cur.scroll); FileWriteDescriptor file(i.value()); file.writeEncrypted(data); } }
//============================================================================= //============================================================================= Crypt::Crypt(QString passphrase) { FUNC_DEBUG; _ikey = 0; _key.clear(); genKey(passphrase); }
void NewX509::on_genKeyBut_clicked() { QString name = description->text(); if (name.isEmpty()) name = commonName->text(); if (name.isEmpty()) name = emailAddress->text(); emit genKey(name); }
QString IItem::getKey() { if(key.isEmpty()) { genKey(); } return key; }
int SetCipherKey(int& a, int& b, int n, int& inverse) { inverse = NO_INVERSE; // integrated validation using for loop for( int mode = UNSET; mode == UNSET; ) { printf("Please select the action you wish to perform: \n\n"); printf("1.) PICK YOUR OWN KEY -> (enter the # 1)\n\n"); printf("2.) GENERATE RANDOM KEY -> (enter the # 2)\n\n"); printf(": "); scanf("%d", &mode); ClearBuf(); if(mode == RAND_KEY) { a = genKey(); b = ( (rand() % n)); inverse = ModularInverse(a, n); if(inverse == ERROR) { printf("Error occurred during random keygen."); return QUIT; } printf("Your new key is A = %d, B = %d", a, b); Pause(); } else if(mode == USER_KEY) { puts("The program will now prompt you to enter your cipher key."); puts("Note: The values you provide for A and B must be coprime."); puts("Please enter a numerical value for A: "); scanf("%d", &a); ClearBuf(); puts("Please enter a numerical value for B: "); scanf("%d", &b); ClearBuf(); // check to make sure we are safe for encryption inverse = ModularInverse(a, n); if( inverse == NO_INVERSE || inverse == ERROR) { printf("Cannot encrypt with key (%d, %d)",a,b); printf("on a (mod %d) ring.\n",n); puts("Restoring key to default..."); a = A, b = B, inverse = INVERSE; Pause(); } } else { mode = UNSET; } } return UNSET; }
void protect::genKeysLists(){ privateKeyList.clear(); for (int i = 0; i < ifList.size(); i++){ hardwareAdress = ifList[i].hardwareAddress(); genPrivateKey(); if (licenseState != _LIC3){ privateKeyList.append(privateKey); genKey(); keyList.append(key); } } }
void menu() { int choice; do{ printf("\n--------------------------------------------------------------\n"); printf("\nAction Menu:\n1. Create a key pair\n2. Encrypt Message\n3. Encrypt Binary File\n4. Decrypt Message\n5. Decrypt Binary File\n6. Exit\nYour choice: "); scanf("%d", &choice); myFlush(); printf("\n--------------------------------------------------------------\n"); }while(choice < 1 || choice > 6); (choice == 1)? genKey() : (choice == 2) ? encrypt() : (choice == 3) ? encryptBin() : (choice == 4) ? decrypt() : (choice == 5) ? decryptBin() : (choice == 6 ) ? printf("Goody Bye.") : 0; }
int main() { int _ar_size; int _ar_i; scanf( "%d", &_ar_size ); printf( "%d\n", _ar_size ); char _ar[_ar_size]; char* _ar_en; int key; char* _ar_de; for( _ar_i = 0; _ar_i < _ar_size; _ar_i++) { scanf( "%s", &_ar[_ar_i] ); printf( "%x\n", _ar[_ar_i] ); } _ar_i = 0; if( (key = genKey( _ar_size ) ) == -1 ) { printf("failed to generate key, check /dev/random"); return 1; } _ar_en = xorStrings( _ar, key ); for( _ar_i = 0; _ar_i < _ar_size; _ar_i++ ) { printf( "EN(%x): %c\n", _ar_en[_ar_i], _ar_en[_ar_i] ); } _ar_i = 0; _ar_de = decryptStrings( _ar_en, key ); for( _ar_i = 0; _ar_i < _ar_size; _ar_i++ ) { printf( "DE(%x): %c\n", _ar_de[_ar_i], _ar_de[_ar_i] ); } printf("done"); return 0; }
void writeImage(const StorageKey &location, const StorageImageSaved &image, bool overwrite) { if (!_working()) return; qint32 size = _storageImageSize(image.data.size()); StorageMap::const_iterator i = _storageMap.constFind(location); if (i == _storageMap.cend()) { i = _storageMap.insert(location, FileDesc(genKey(), size)); _storageFilesSize += size; _mapChanged = true; _writeMap(); } else if (!overwrite) { return; } EncryptedDescriptor data(sizeof(quint64) * 2 + sizeof(quint32) + sizeof(quint32) + image.data.size()); data.stream << quint64(location.first) << quint64(location.second) << quint32(image.type) << image.data; FileWriteDescriptor file(i.value().first, false); file.writeEncrypted(data); if (i.value().second != size) { _storageFilesSize += size; _storageFilesSize -= i.value().second; _storageMap[location].second = size; } }
int main(int argc, char *argv[]) { // For this example, wizard-generates single line code would be good enough, // but very soon it won't be enough for you anyway, so use this more detailed example from start QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv)); QScopedPointer<QQuickView> view(SailfishApp::createView()); // Here's how you will add QML components whenever you start using them // Check https://github.com/amarchen/Wikipedia for a more full example // view->engine()->addImportPath(SailfishApp::pathTo("qml/components").toString()); sshExecuteCmd *sshCmd = new sshExecuteCmd(); sshCmd->setSpinnerState(false); sshExecuteCmd *sshGenKey = new sshExecuteCmd(); sshExecuteCmd *sshDelKey = new sshExecuteCmd(); sshExecuteCmd *sshReadKey = new sshExecuteCmd(); QThread* sshGenKeythread = new QThread; QThread* sshDelKeythread = new QThread; QThread* sshReadKeythread = new QThread; qDebug("Main Thread ID: %d",(int)QGuiApplication::instance()->thread()); sqliteDB *onesshotDB = new sqliteDB(); onesshotDB->createDB(); sshGenKey->moveToThread(sshGenKeythread); QObject::connect(sshGenKeythread, SIGNAL(started()), sshGenKey, SLOT(genKey()),Qt::QueuedConnection); QObject::connect(sshGenKeythread, SIGNAL(started()), sshCmd, SLOT(spinIt())); QObject::connect(sshGenKey, SIGNAL(spinnerStateUpdated()), sshCmd, SLOT(stopSpinningIt())); QObject::connect(sshGenKey, SIGNAL(finished()), sshCmd, SLOT(stopSpinningIt())); sshDelKey->moveToThread(sshDelKeythread); QObject::connect(sshDelKeythread, SIGNAL(started()), sshDelKey, SLOT(deleteKeys()),Qt::QueuedConnection); QObject::connect(sshDelKeythread, SIGNAL(started()), sshCmd, SLOT(spinIt())); QObject::connect(sshDelKey, SIGNAL(keysDeletedUpdated()), sshCmd, SLOT(stopSpinningIt())); sshReadKey->moveToThread(sshReadKeythread); QObject::connect(sshReadKeythread, SIGNAL(started()), sshReadKey, SLOT(readKey()),Qt::QueuedConnection); QObject::connect(sshReadKeythread, SIGNAL(started()), sshCmd, SLOT(spinIt())); QObject::connect(sshReadKey, SIGNAL(pubKeyURLUpdated(QString)), sshCmd, SLOT(stopSpinningIt())); QObject::connect(sshReadKey, SIGNAL(pubKeyURLUpdated(QString)), sshReadKey, SLOT(getPubKey())); QObject::connect(sshReadKey, SIGNAL(pubKeyURLUpdated(QString)), sshReadKey, SLOT(getPubKeyURL())); view->rootContext()->setContextProperty("sshGenKey", sshGenKeythread); view->rootContext()->setContextProperty("sshDelKey", sshDelKeythread); view->rootContext()->setContextProperty("sshReadKey", sshReadKeythread); view->rootContext()->setContextProperty("sshReadKeyValue", sshReadKey); view->rootContext()->setContextProperty("sshCmd", sshCmd); view->setSource(SailfishApp::pathTo("qml/oneSSHot.qml")); view->show(); return app->exec(); }
//********************************************************************** //* Cracks the key then Decrypts the file to filename.txt (removes a //* .bin extention int handle_file(FILE* fp, char bruteforce, char* fname) { uint8_t *inbuf, *outbuf; long size = 0, i = 0, rv = 0, buffsize = 0; uint16_t key; FILE *of = NULL; char* ofname = NULL; int len = strlen(fname), ret = EXIT_SUCCESS; if (!strcmp((fname+len-4), ".bin")) fname[len-4] = '\0'; ofname = malloc(sizeof(char)*(strlen(fname)+5)); strcpy(ofname, fname); strcat(ofname, ".txt"); fseek(fp, 0L, SEEK_END); size = ftell(fp); rewind (fp); if (size > 1) { if (size <= BUF_MAX) buffsize = size; else buffsize = BUF_MAX; inbuf = (uint8_t*)malloc(sizeof(uint8_t)*buffsize); if (inbuf == NULL) { free(ofname); perror("malloc"); return EXIT_FAILURE; } outbuf = (uint8_t*)malloc(sizeof(uint8_t)*buffsize); if (outbuf == NULL) { free(inbuf); free(ofname); perror("malloc"); return EXIT_FAILURE; } i = fread(inbuf, 1, buffsize, fp); if (i == buffsize) { if (bruteforce) key = bf_key(inbuf, buffsize, bruteforce-1); else key = freq_anal(inbuf, buffsize); printf("RawKey = 0x%X, Key = 0x%X\n", key, genKey(key)); decrypt(inbuf,outbuf,key,&buffsize); of = fopen(ofname, "w"); if (fp != NULL) { rv = fwrite(outbuf, 1, buffsize, of); if (rv != buffsize) { fprintf(stderr, "Write Error\n"); ret = EXIT_FAILURE; } else { while (i < size) { buffsize = fread(inbuf, 1, buffsize, fp); decrypt(inbuf,outbuf,key,&buffsize); rv = fwrite(outbuf, 1, buffsize, of); if (rv != buffsize) { fprintf(stderr, "Write Error\n"); ret = EXIT_FAILURE; } if (feof(fp)) break; if (ferror(of) || ferror(fp)) { fprintf(stderr, "File Error\n"); ret = EXIT_FAILURE; break; } i+= buffsize; } } fclose (of); } else { perror("fopen"); ret = EXIT_FAILURE; } } else { fprintf(stderr, "Read Error\n"); ret = EXIT_FAILURE; } free(inbuf); free(outbuf); } else if (size >= 0) printf("Input file is too small\n"); else { perror("ftell"); ret = EXIT_FAILURE; } free(ofname); return ret; }
/*********************parent process tcp connection use to manage************************/ void client_mgr(char *ip, int serverPort, int pipefd, int pid) { int flag = 0; char *p; char name[256], passwd[256]; char realName[512]; int err, fd, i; struct sockaddr_in sa; char buf[4096]; SSL_CTX* ctx; SSL* ssl; //create a TCP socket fd = socket (AF_INET, SOCK_STREAM, 0); CHK_ERR(fd, "socket"); memset (&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(ip); sa.sin_port = htons(serverPort); //connect step err = connect(fd, (struct sockaddr*) &sa, sizeof(sa)); CHK_ERR(err, "connect"); sleep(2); puts("Please input the common name: "); scanf("%s", realName); setupCTX(&ctx); //build SSL on the TCP connection ssl = SSL_new(ctx); CHK_NULL(ssl); SSL_set_fd (ssl, fd); err = SSL_connect(ssl); CHK_SSL(err); //check certificate SSL_CTX_load_verify_locations(ctx, CACERT, NULL); SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); int result = SSL_get_verify_result(ssl); if(result == X509_V_OK || result == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) { printf("The certificate is valid.\n"); } else { printf("Invalid certificate %d\n", result); exit(1); } X509* server_cert = SSL_get_peer_certificate(ssl); CHK_NULL(server_cert); char *str = X509_NAME_oneline(X509_get_subject_name(server_cert),0,0); CHK_NULL(str); OPENSSL_free(str); str = X509_NAME_oneline(X509_get_issuer_name(server_cert),0,0); CHK_NULL(str); OPENSSL_free(str); X509_NAME *xname = X509_get_subject_name(server_cert); X509_NAME_get_text_by_NID(xname, NID_commonName, commonName, 512); if( strcasecmp(commonName, realName) !=0 ) { printf("commonName is wrong.\n"); exit(1); } printf("commonName is right.\n"); printf("Server authentication is successful.\n"); //release! X509_free(server_cert); sleep(2); while(!flag) { //handle the login part printf("username: "******"%s",name); getchar(); //safe mode set_disp_mode(STDIN_FILENO, 0); getpasswd(passwd, sizeof(passwd)); p = passwd; while(*p != '\n') p++; *p = '\0'; //OK! set_disp_mode(STDIN_FILENO, 1); sendName(ssl, name); sendPass(ssl, passwd); SSL_read(ssl, buf, sizeof(buf) - 1); putchar(10); if( buf[0] == 'o' ) { puts("Connect successfully"); flag = 1; } else { puts("wrong password, please try again!"); } } //clean the password for security reason memset(passwd, 0, sizeof(passwd)); genKey(key); sendKey(ssl, key); while (1) { talkToudp(key, pipefd, 'k'); printf("1. ipnut 'q' to quit.\n"); printf("2. input 'c' to change the key.\n"); scanf("%s", buf); if (strlen(buf) == 1) { if (buf[0]=='q') { break; } else if( buf[0]=='r'){ genKey(key); sendKey(ssl, key); } } else { printf("Invalid.\n"); continue; } } memset(key, 0, KEYSIZE); memset(IV, 0, IVSIZE); sendKey(ssl, key); sleep(1); kill(pid, SIGTERM); wait(0); SSL_shutdown(ssl); /* send SSL/TLS close_notify */ close(fd); SSL_free(ssl); SSL_CTX_free(ctx); }
int main() { int i, j, k;// added k on 19/10/15 unsigned char* buffer = (unsigned char*) malloc(sizeof(unsigned char)*BUF_SIZE); unsigned char* state = (unsigned char*) malloc(sizeof(unsigned char)*16); readEncryptFile(buffer); //unsigned char w0[4], w1[4], w2[4], w3[4]; unsigned char rk[11][16]; char key[16] = "abcdefghijklmnop"; strncpy(&rk[0][0], key, 4); strncpy(&rk[0][4], key+4, 4); strncpy(&rk[0][8], key+8, 4); strncpy(&rk[0][12], key+12, 4); for (i=1; i<10; i++) { printf("round %d\n", i); genKey(&rk[i-1], &rk[i], i); } unsigned char block[4][4]; /*= { {0x0,0x1,0x2,0x3}, {0x4,0x5,0x6,0x7}, {0x8,0x9,0x0a,0x0b}, {0x0c,0x0d,0x0e,0x0f} }; invsubbytes(block); for (i=0; i<4; i++) { for (j=0; j<4; j++) { printf("%x ", block[i][j]); } printf("\n"); } */ extractState(buffer, state, 0); for (i=0; i<4; i++) for (j=0; j<4; j++) block[i][j] = state[i*4+j]; //minus round key 10 for (i = 0; i < 4; i++) { for (j = 0; j <4; j++) block[j][i] = block[j][i] + rk[10][i*4+j]; } //repeat for 9 more rounds for (k = 9; k > 0; k--) { invshiftrows(block); invsubbytes(block); for (i = 0; i < 4; i++) { for (j = 0; j <4; j++) block[i][j] = block[i][j] + rk[k][i*4+j]; } //invmixcolumn(); } //repeat last round invshiftrows(block); invsubbytes(block); for (i = 0; i < 4; i++) { for (j = 0; j <4; j++) block[j][i] = block[j][i] + rk[0][i*4+j]; } // ended on 19 Oct free(buffer); free(state); return 0; }