//extern "C" JNIEXPORT jint JNICALL Java_com_example_openssldataencryption_MainActivity_encodeFileFromJNI(JNIEnv* env, jobject obj, jstring jFilename ) { extern "C" JNIEXPORT jint JNICALL Java_com_example_dheerajkaushik_ndktest_DataEncryptionOpenSSL_encodeFileFromJNI(JNIEnv* env, jobject obj, jstring jFilenameOut, jstring jFilenameIn ) { int ret = 0; const char* filenameIn = env->GetStringUTFChars(jFilenameIn, 0); const char* filenameOut = env->GetStringUTFChars(jFilenameOut, 0); ret = encodeFile(filenameOut, filenameIn); // release env->ReleaseStringUTFChars(jFilenameIn, filenameIn); env->ReleaseStringUTFChars(jFilenameOut, filenameOut); return ret; }
//extern "C" JNIEXPORT jint JNICALL Java_com_example_openssldataencryption_MainActivity_encodeFileFromJNI(JNIEnv* env, jobject obj, jstring jFilename ) { extern "C" JNIEXPORT jint JNICALL Java_android_intel_sdp_DataEncryption_DataEncryptionOpenSSL_encodeFileFromJNI(JNIEnv* env, jobject obj, jstring jFilenameOut, jstring jFilenameIn ) { int ret = 0; const char* filenameIn = env->GetStringUTFChars( jFilenameIn, 0); const char* filenameOut = env->GetStringUTFChars( jFilenameOut, 0); ret = encodeFile(filenameOut, filenameIn); // release env->ReleaseStringUTFChars( jFilenameIn, filenameIn); env->ReleaseStringUTFChars( jFilenameOut, filenameOut); return ret; }
int main(int argc, char *argv[]) { if (!strcmp(argv[1], "-e")) { printf("encoding %s to %s\n", argv[2], argv[3]); encodeFile(argv[2], argv[3]); } else if (!strcmp(argv[1], "-d")) { printf("decoding %s to %s\n", argv[2], argv[3]); decodeFile(argv[2], argv[3]); } return(0); }
/* * Takes in the following parameters * File - The file to en-/decode * Key - file containing the key used for en-/decoding the file */ int main (int argc, char **argv) { // Check if the program was started with two parameters (file and key) if (argc == 3){ FileContainer files; int securityDistance; if (checkFiles(argv) != 0) { files = openAndReadKey(argv[2]); char action = getUserInput(); int choice = encodeOrDecode(action); switch (encodeOrDecode(action)) { case 0: printMessage(TYPE_INFO, CHOICE_EXIT); free(files.key); exit(0); case 1: securityDistance = getWantedSecLvl(); printMessage(TYPE_INFO, INFO_ENCODING_START); encodeFile(files, argv[1], securityDistance); printMessage(TYPE_INFO, INFO_ENCODING_END); break; case 2: printMessage(TYPE_INFO, INFO_DECODING_START); decodeFile(files, argv[1]); printMessage(TYPE_INFO, INFO_DECODING_END); break; default: break; } } } else { printMessage(TYPE_ERROR, ERROR_PARAMETERS); } return 0; }
/** * Constructor. Connect all of the object and the job control. */ KAudioCreator::KAudioCreator( QWidget* parent, const char* name) : KMainWindow(parent, name) { janusWidget = new KJanusWidget(this, name, KJanusWidget::Tabbed); setCentralWidget(janusWidget); QVBox *frame = janusWidget->addVBoxPage(i18n("&CD Tracks"), QString::null, SmallIcon("cdaudio_unmount", 32)); tracks = new TracksImp(frame, "Tracks"); ripper = new Ripper ( frame, "Rip" ); encoder = new Encoder( frame, "Encoder" ); frame = janusWidget->addVBoxPage( i18n("&Jobs"), QString::null, SmallIcon( "run", 32 ) ); jobQue = new JobQueImp( frame, "Que" ); resize(500, 440); /*KAction *eject = */new KAction( i18n("&Eject CD"), 0, tracks, SLOT( eject() ), actionCollection(), "eject" ); (void)new KAction( i18n("&Configure KAudioCreator..."), 0, this, SLOT( showSettings() ), actionCollection(), "configure_kaudiocreator" ); KAction *selectAll = new KAction( i18n( "Select &All Tracks"), 0, tracks, SLOT( selectAllTracks() ), actionCollection(), "select_all" ) ; KAction *deselectAll = new KAction( i18n( "Deselect &All Tracks"), 0, tracks, SLOT( deselectAllTracks() ), actionCollection(), "deselect_all" ); selectAll->setEnabled( false ); deselectAll->setEnabled( false ); KActionMenu *actActionMenu = new KActionMenu( i18n("Rip &Selection"), "rip", actionCollection(), "rip" ); actActionMenu->setDelayed(true); //needed for checking "all accounts" actActionMenu->setEnabled( false ); connect( actActionMenu, SIGNAL( activated() ), tracks, SLOT( startSession() ) ); ripMenu = actActionMenu->popupMenu(); connect( ripMenu, SIGNAL( activated(int) ), this, SLOT( slotRipSelection(int)) ); connect( ripMenu, SIGNAL( aboutToShow() ), this, SLOT( getRipMenu()) ); KAction *rip = new KAction( i18n( "Rip &Selection" ), 0, tracks, SLOT( startSession() ), actionCollection(), "rip_selected" ); rip->setEnabled( false ); connect( jobQue, SIGNAL( removeJob(int) ), this, SLOT( updateStatus() ) ); connect( jobQue, SIGNAL( removeJob(int) ), ripper, SLOT( removeJob(int) ) ); connect( jobQue, SIGNAL( removeJob(int) ), encoder, SLOT( removeJob(int)) ); connect( ripper, SIGNAL( updateProgress(int, int) ) , jobQue, SLOT( updateProgress(int,int) ) ); connect( ripper, SIGNAL( addJob(Job*, const QString &) ), jobQue, SLOT( addJob(Job*, const QString &)) ); connect( ripper, SIGNAL( eject(const QString &) ) , tracks, SLOT( ejectDevice(const QString &)) ); connect( ripper, SIGNAL( encodeWav(Job *) ) , encoder, SLOT( encodeWav(Job *)) ); connect( ripper, SIGNAL( jobsChanged() ) , this, SLOT( updateStatus() ) ); connect( encoder, SIGNAL( updateProgress(int, int) ) , jobQue, SLOT( updateProgress(int,int)) ); connect( encoder, SIGNAL( addJob(Job*, const QString&)), jobQue, SLOT( addJob(Job*, const QString &)) ); connect( encoder, SIGNAL( jobsChanged() ) , this, SLOT( updateStatus() ) ); connect( tracks, SIGNAL( hasCD(bool) ) , this, SLOT( hasCD(bool) ) ); connect( tracks, SIGNAL( ripTrack(Job *) ), ripper, SLOT( ripTrack(Job *)) ); connect( tracks, SIGNAL( hasTracks(bool) ), rip, SLOT( setEnabled(bool)) ); connect( tracks, SIGNAL( hasTracks(bool) ), actActionMenu, SLOT( setEnabled(bool)) ); connect( tracks, SIGNAL( hasTracks(bool) ), deselectAll, SLOT( setEnabled(bool)) ); connect( tracks, SIGNAL( hasTracks(bool) ), selectAll, SLOT( setEnabled(bool)) ); (void)new KAction(i18n("Remove &Completed Jobs"), 0, jobQue, SLOT(clearDoneJobs()), actionCollection(), "clear_done_jobs" ); KAction *edit = new KAction(i18n("&Edit Album..."), 0, tracks, SLOT(editInformation()), actionCollection(), "edit_cd"); connect(tracks, SIGNAL(hasCD(bool)), edit, SLOT(setEnabled(bool))); edit->setEnabled( false ); (void)new KAction(i18n("Encode &File..."), 0, this, SLOT(encodeFile()), actionCollection(), "encode_file"); KAction *cddb = new KAction(i18n("&CDDB Lookup"), 0, tracks, SLOT(performCDDB()), actionCollection(), "cddb_now"); connect(tracks, SIGNAL(hasCD(bool)), cddb, SLOT(setEnabled(bool))); cddb->setEnabled( false ); KStdAction::configureNotifications(this, SLOT(configureNotifications()), actionCollection()); KStdAction::quit( this, SLOT(close()), actionCollection(), "quit" ); // Init statusbar statusBar()->insertItem(i18n("No Audio CD detected"), 0 ); hasCD(tracks->hasCD()); setupGUI(); }
// // Use getopts() // int main(int argc, char **argv){ //struct that holds the passed arguments struct arguments facts; //input format //2 argument //--file to read //--number of chunks to create //check input if(argc == 3){ //2 arguments facts = parse_args(argc, argv); } else { //too many arguments printf("Invalid input. Valid arguments:\n"); printf("Filename and number of chunks to make.\n"); return 0; } // Initialize GF GF16init(); //gets file and encodes encodeFile(facts); printf("Done\n"); return 0; //intput //uint16_t *input; /* //output chunks struct chunk output[facts.num_of_chunks]; int i; for(i = 0 ; i < facts.num_of_chunks ; i++){ struct chunk out; out.numEmpty = 0; out.output = NULL; output[i] = out; } */ /* //get coefficients (new) for (i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { //make sure coefficients aren't 0 while(1) { output[i].coef[j] = mt_rand16(); if(output[i].coef[j] != 0) break; } } } // uint16_t a[3][3], b[3], x[3]; //b[0] = a[0][0] * x[0] + a[0][1] * x[1] + a[0][2] * x[2]; //b[1] = a[1][0] * x[0] + a[1][1] * x[1] + a[1][2] * x[2]; //b[2] = a[2][0] * x[0] + a[2][1] * x[1] + a[2][2] * x[2]; printf("Encoding\n"); //encode section for(i = 0, t = 0; i < DATA_LENGTH ; t++, i += 3){ output[0].output[t] = GF16mul(output[0].coef[0], input[i]) ^ GF16mul(output[0].coef[1], input[i+1]) ^ GF16mul(output[0].coef[2], input[i+2]); output[1].output[t] = GF16mul(output[1].coef[0], input[i]) ^ GF16mul(output[1].coef[1], input[i+1]) ^ GF16mul(output[1].coef[2], input[i+2]); output[2].output[t] = GF16mul(output[2].coef[0], input[i]) ^ GF16mul(output[2].coef[1], input[i+1]) ^ GF16mul(output[2].coef[2], input[i+2]); } */ /* printf("Decoding\n"); //variables uint16_t *final; //allocate space for final file if ((final = malloc(DATA_LENGTH * sizeof *input)) == NULL) { // 16bit perror("malloc"); exit(1); } //set up c variables uint16_t c0; uint16_t c1; uint16_t c2; uint16_t c3; uint16_t c4; //do math for c variables c0 = GF16mul(output[1].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[1].coef[1]); c1 = GF16mul(output[1].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[1].coef[2]); c2 = GF16mul(output[2].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[2].coef[1]); c3 = GF16mul(output[2].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[2].coef[2]); c4 = GF16mul(c1, c2) ^ GF16mul(c0, c3); uint16_t *t0 = malloc(DATA_LENGTH/3 * sizeof *t0); uint16_t *t1 = malloc(DATA_LENGTH/3 * sizeof *t1); for(i = 0 ; i < DATA_LENGTH/3 ; i++){ t0[i] = GF16mul(output[1].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[1].output[i]); t1[i] = GF16mul(output[2].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[2].output[i]); } uint16_t *x1 = malloc(DATA_LENGTH/3 * sizeof *x1); uint16_t *x2 = malloc(DATA_LENGTH/3 * sizeof *x2); uint16_t *x3 = malloc(DATA_LENGTH/3 * sizeof *x3); for(i = 0 ; i < DATA_LENGTH/3 ; i++){ x2[i] = GF16div((GF16mul(c2, t0[i]) ^ GF16mul(c0, t1[i])),c4); x1[i] = GF16div((t0[i] ^ GF16mul(c1, x2[i])),c0); x3[i] = GF16div((output[0].output[i] ^ GF16mul(output[0].coef[1], x1[i]) ^ GF16mul(output[0].coef[2], x2[i])),output[0].coef[0]); } for(i = 0, t = 0 ; i < DATA_LENGTH ; i +=3, t++){ //final[i] = x1[t]; //final[i+1] = x2[t]; //final[i+2] = x3[t]; final[i] = x3[t]; final[i+1] = x1[t]; final[i+2] = x2[t]; } printf("Checking...\n"); //check for( i = 0 ; i < DATA_LENGTH ; i++){ if(input[i] != final[i]){ printf("Not equal at %d\n", i); printf("%d and %d\n", input[i], final[i]); //printf("%d and %d\n", input[i+1], final[i+1]); //printf("%d and %d\n", input[i+2], final[i+2]); break; } } */ //printf("Done\n"); /* free(input); free(output[0].output); free(output[1].output); free(output[2].output); */ /* free(final); free(x1); free(x2); free(x3); */ //return 0; }
// // Use getopts() // int main(int argc, char **argv) { //input format //no input for random input //1 argument that is a file and number of chunks to create //3 arguments that are the chunks to use to create a file //check inout int state; if(argc == 1) { //0 argument //random input state = 0; } else if(argc == 3) { //2 arguments //file input //number of chunks to make state = 1; } else if(argc == 4) { //3 arguments //3 chunks to use in creating file state = 2; } else { //too many arguments printf("Invalid input. Valid arguments:\n"); printf("No arguments for random input.\nFilename and number of chunks to make.\nOr three files to reassemble\n"); return 0; } // Initialize GF GF16init(); //set up of variables //intput uint16_t *input; //output chunks struct chunk output[3]; struct chunk out1; struct chunk out2; struct chunk out3; output[0] = out1; output[1] = out2; output[2] = out3; //loop variables int i, j, t; //data length variable int DATA_LENGTH; // Allocate size for input and output //check based on state if( state == 0 ) { //No file. So random file DATA_LENGTH = SPACE / sizeof(uint16_t); //input allocation if ((input = malloc(DATA_LENGTH * sizeof *input)) == NULL) { // 16bit perror("malloc"); exit(1); } //fill input with random numbers for (i = 0; i < DATA_LENGTH ; i++) { input[i] = mt_rand16(); } //output allocation if ((output[0].output = malloc(DATA_LENGTH * sizeof *output[0].output)) == NULL) { // 16bit perror("malloc"); exit(1); } if ((output[1].output = malloc(DATA_LENGTH * sizeof *output[1].output)) == NULL) { // 16bit perror("malloc"); exit(1); } if ((output[2].output = malloc(DATA_LENGTH * sizeof *output[2].output)) == NULL) { // 16bit perror("malloc"); exit(1); } } else if(state == 1) { //file input //file io operations here //... encodeFile(argv, output); } else if(state == 2) { //3 files input for reassembly } //get coefficients (new) for (i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { //make sure coefficients aren't 0 while(1) { output[i].coef[j] = mt_rand16(); if(output[i].coef[j] != 0) break; } } } // uint16_t a[3][3], b[3], x[3]; //b[0] = a[0][0] * x[0] + a[0][1] * x[1] + a[0][2] * x[2]; //b[1] = a[1][0] * x[0] + a[1][1] * x[1] + a[1][2] * x[2]; //b[2] = a[2][0] * x[0] + a[2][1] * x[1] + a[2][2] * x[2]; printf("Encoding\n"); //encode section for(i = 0, t = 0; i < DATA_LENGTH ; t++, i += 3) { output[0].output[t] = GF16mul(output[0].coef[0], input[i]) ^ GF16mul(output[0].coef[1], input[i+1]) ^ GF16mul(output[0].coef[2], input[i+2]); output[1].output[t] = GF16mul(output[1].coef[0], input[i]) ^ GF16mul(output[1].coef[1], input[i+1]) ^ GF16mul(output[1].coef[2], input[i+2]); output[2].output[t] = GF16mul(output[2].coef[0], input[i]) ^ GF16mul(output[2].coef[1], input[i+1]) ^ GF16mul(output[2].coef[2], input[i+2]); } printf("Decoding\n"); //variables uint16_t *final; //allocate space for final file if ((final = malloc(DATA_LENGTH * sizeof *input)) == NULL) { // 16bit perror("malloc"); exit(1); } //set up c variables uint16_t c0; uint16_t c1; uint16_t c2; uint16_t c3; uint16_t c4; //do math for c variables c0 = GF16mul(output[1].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[1].coef[1]); c1 = GF16mul(output[1].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[1].coef[2]); c2 = GF16mul(output[2].coef[0], output[0].coef[1]) ^ GF16mul(output[0].coef[0], output[2].coef[1]); c3 = GF16mul(output[2].coef[0], output[0].coef[2]) ^ GF16mul(output[0].coef[0], output[2].coef[2]); c4 = GF16mul(c1, c2) ^ GF16mul(c0, c3); uint16_t *t0 = malloc(DATA_LENGTH/3 * sizeof *t0); uint16_t *t1 = malloc(DATA_LENGTH/3 * sizeof *t1); for(i = 0 ; i < DATA_LENGTH/3 ; i++) { t0[i] = GF16mul(output[1].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[1].output[i]); t1[i] = GF16mul(output[2].coef[0], output[0].output[i]) ^ GF16mul(output[0].coef[0], output[2].output[i]); } uint16_t *x1 = malloc(DATA_LENGTH/3 * sizeof *x1); uint16_t *x2 = malloc(DATA_LENGTH/3 * sizeof *x2); uint16_t *x3 = malloc(DATA_LENGTH/3 * sizeof *x3); for(i = 0 ; i < DATA_LENGTH/3 ; i++) { x2[i] = GF16div((GF16mul(c2, t0[i]) ^ GF16mul(c0, t1[i])),c4); x1[i] = GF16div((t0[i] ^ GF16mul(c1, x2[i])),c0); x3[i] = GF16div((output[0].output[i] ^ GF16mul(output[0].coef[1], x1[i]) ^ GF16mul(output[0].coef[2], x2[i])),output[0].coef[0]); } for(i = 0, t = 0 ; i < DATA_LENGTH ; i +=3, t++) { //final[i] = x1[t]; //final[i+1] = x2[t]; //final[i+2] = x3[t]; final[i] = x3[t]; final[i+1] = x1[t]; final[i+2] = x2[t];
int main(int argc,char **argv) { int selection; // Komut satırı argümanlarını kontrol etmek için kullanılır. char *filename = NULL; // Kaynak dosya olarak verilen dosya isimlerini tutar. char *target = NULL; // Hedef olarak verilen dosya isimlerini tutar. int posibiltyFlag = 0; // Frekans hesaplaması yapılacağında bu bayrak 1 değerini alır. int encodingFlag = 0; // Encode işlemi yapılacağında bu bayrak 1 değerini alır. int decodingFlag = 0; // Decode işlemi yapılacağında bu bayrak 1 değerini laır. int counterList[MAXINDEX]; // ASCII tablosu indis olarak kabul edilirse kaynak dosyadaki tüm karakterlerin kaç defa tekrarlandığını tutar. int totalCharacterCount = 0; // Kaynak dosyadaki toplam karakter sayısını tutar. HASH **hashTable; // Kod tablosunun tutar. int hashTableLength = 0; // Kod tablosunun uzunluğunu tutar. char *huffmanTree= NULL; // huffman.txt 'den okunan huffman ikili ağacının tutar. int huffmanTreeLength = 0; // huffman.txt 'den okunan huffman ikili ağacının uzunluğunu tutar. int encodedBitCount = 0; // Encode işlemi sırasında kaç bitin encode edildiğini tutar. for(register int i=0; i<MAXINDEX; i++) { counterList[i] = 0; } opterr = 0; while( (selection = getopt(argc, argv, "pef:t:d:")) != -1 ) { switch (selection) { case 'f': filename = optarg; break; case 'p': posibiltyFlag = 1; break; case 'e': encodingFlag = 1; break; case 't': target = optarg; break; case 'd': encodedBitCount = atoi(optarg); printf("\n%d\n",encodedBitCount); decodingFlag = 1; break; default: printf("Eksik veya hatalı bir argüman girildi!"); exit(EXIT_FAILURE); } } // Yapılacak her işlem için kaynak dosyaya ihtiyaç vardır. if(!filename) { printf("You have to enter a filename. Ex -f filename.txt"); exit(EXIT_FAILURE); } // Yapılacak her işlem için hedef dosyaya ihtiyaç vardır. if(!target) { printf("You have to enter a target filename. Ex -t target.txt"); exit(EXIT_FAILURE); } if(posibiltyFlag) { totalCharacterCount = calculateFrequency(counterList,filename); FILE *frequencyFp = fopen(target,"w"); for(register int i=0; i<MAXINDEX; i++) { if(counterList[i]){ #if DEBUG == 1 printf("\n %c -> %.2lf\n",i, (double)counterList[i]/ totalCharacterCount ); fprintf(frequencyFp, "\n %c -> %.2lf",i, (double)counterList[i]/ totalCharacterCount); #endif } } fclose(frequencyFp); } if(encodingFlag) { huffmanTree = readHuffmanTree(&huffmanTreeLength); hashTable = createHashTable(&hashTableLength, huffmanTree, huffmanTreeLength); #if DEBUG == 1 printf("\n---------------Hash Tablosu-------------------\n"); for(register int i=0; i<hashTableLength;i++) { printf("\nKarakter: %c\t\tKod: %s\n", hashTable[i]->letter, hashTable[i]->code); } printf("\n---------------Hash Tablosu-------------------\n"); #endif encodedBitCount = encodeFile(filename, target, hashTable, hashTableLength); #if DEBUG == 1 printf("\n\n"); printf("\t\t Şifrelenen bit sayısı : %d", encodedBitCount); printf("\n\n"); #endif } if(decodingFlag) { huffmanTree = readHuffmanTree(&huffmanTreeLength); hashTable = createHashTable(&hashTableLength, huffmanTree, huffmanTreeLength); #if DEBUG == 1 printf("\n---------------Hash Tablosu-------------------\n"); for(register int i=0; i<hashTableLength;i++) { printf("\nKarakter: %c\t\tKod: %s\n", hashTable[i]->letter, hashTable[i]->code); } printf("\n---------------Hash Tablosu-------------------\n"); #endif decodeFile(filename, target, hashTable, hashTableLength,encodedBitCount); } return EXIT_SUCCESS; }