mKey = null; try { kgen = KeyGenerator.getInstance("AES"); mKey = kgen.generateKey(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
// open stream to read origFilepath. We are going to save encrypted contents to outfile InputStream fis = new FileInputStream(origFilepath); File outfile = new File(encFilepath); int read = 0; if (!outfile.exists()) outfile.createNewFile(); FileOutputStream encfos = new FileOutputStream(outfile); // Create Cipher using "AES" provider Cipher encipher = Cipher.getInstance("AES"); encipher.init(Cipher.ENCRYPT_MODE, mKey); CipherOutputStream cos = new CipherOutputStream(encfos, encipher); // capture time it takes to encrypt file start = System.nanoTime(); Log.d(TAG, String.valueOf(start)); byte[] block = new byte[mBlocksize]; while ((read = fis.read(block,0,mBlocksize)) != -1) { cos.write(block,0, read); } cos.close(); stop = System.nanoTime(); Log.d(TAG, String.valueOf(stop)); seconds = (stop - start) / 1000000;// for milliseconds Log.d(TAG, String.valueOf(seconds)); fis.close();
unsigned char cKeyBuffer[KEYSIZE/sizeof(unsigned char)]; unsigned char iv[] = "01234567890123456"; int opensslIsSeeded = 0; if (!opensslIsSeeded) { if (!RAND_load_file("/dev/urandom", seedbytes)) { return -1; } opensslIsSeeded = 1; } if (!RAND_bytes((unsigned char *)cKeyBuffer, KEYSIZE )) { }
if (!(EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), NULL, cKeyBuffer, iv ))) { ret = -1; printf( "ERROR: EVP_ENCRYPTINIT_EXn"); } // go through file, and encrypt if ( orig_file != NULL ) { origData = new unsigned char[aes_blocksize]; encData = new unsigned char[aes_blocksize+EVP_CIPHER_CTX_block_size(e_ctx)]; // potential for encryption to be 16 bytes longer than original printf( "Encoding file: %sn", filename); bytesread = fread(origData, 1, aes_blocksize, orig_file); // read bytes from file, then send to cipher while ( bytesread ) { if (!(EVP_EncryptUpdate(e_ctx, encData, &len, origData, bytesread))) { ret = -1; printf( "ERROR: EVP_ENCRYPTUPDATEn"); } encData_len = len; fwrite(encData, 1, encData_len, enc_file ); // read more bytes bytesread = fread(origData, 1, aes_blocksize, orig_file); } // last step encryption if (!(EVP_EncryptFinal_ex(e_ctx, encData, &len))) { ret = -1; printf( "ERROR: EVP_ENCRYPTFINAL_EXn"); } encData_len = len; fwrite(encData, 1, encData_len, enc_file ); // free cipher EVP_CIPHER_CTX_free(e_ctx);
for (int i = 0; i < lastVal; i += 2) { dataRandomPoints[i] = (rand.nextInt() % widget_width); dataRandomPoints[i+1] = (rand.nextInt() % widget_height); }
SecureRandom srand = new SecureRandom(); shouldDraw = (srand.nextInt() % randomMod );
unsigned int cKeyBuffer[keysize]; memset(cKeyBuffer, 0, sizeof(unsigned int) * keysize); FILE *fin; strcpy(filein, "/dev/urandom"); fin = fopen(filein, "rb"); if (fin != NULL) { fread(cKeyBuffer, sizeof(int), keysize, fin); fclose (fin); }
int seedbytes = 1024; unsigned int cKeyBuffer[keysize]; memset(cKeyBuffer, 0, sizeof(unsigned int) * keysize); if (!opensslIsSeeded) { if (!RAND_load_file("/dev/urandom", seedbytes)) { __android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to seed OpenSSL RNG"); return jKeyBuffer; } opensslIsSeeded = 1; } if (!RAND_bytes((unsigned char *)cKeyBuffer, keysize * sizeof(int))) { __android_log_print(ANDROID_LOG_ERROR, TAG, "Faled to create OpenSSSL random integers: %ul", ERR_get_error); }
Source: https://habr.com/ru/post/224285/
All Articles