{"id":224285,"date":"2014-06-02T13:56:02","date_gmt":"2014-06-02T09:56:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=224285"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=224285","title":{"rendered":"<span class=\"post_title\">\u0428\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0432 Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u0422\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0435\u0433\u043e \u0434\u0432\u0435 \u043e\u0441\u043d\u043e\u0432\u043e\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0449\u0438\u0435 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445: \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0438 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u0430 \u043f\u043e \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u2013 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0441\u043a\u0430\u0447\u0430\u0432 \u0435\u0433\u043e \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4><b>\u0428\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/b><\/h4>\n<p>\u0428\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043c\u0435\u0435\u0442 \u0432\u0430\u0436\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043a\u0440\u044b\u0442\u044c \u043e\u0442 \u043f\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0433\u043b\u0430\u0437 \u0442\u043e, \u0447\u0442\u043e \u0438\u043c \u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u0438\u0434\u0435\u0442\u044c. \u041c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0445\u0440\u0430\u043d\u044f\u0442 \u0432\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043d\u0430\u0447\u0438\u043c\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u0438 \u0437\u0430\u0449\u0438\u0442\u0438\u0442\u044c \u0435\u0435 \u2013 \u043f\u0440\u044f\u043c\u0430\u044f \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430.<br \/>  \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0434 Android: \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Java Crypto API \u0438 OpenSSL API (\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043a\u043e\u0434). \u041c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0431\u0430.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/013\/7e5\/38e\/0137e538ea993d9f583b412880bf23e6.png\"\/><\/p>\n<h5>Java Crypto API<\/h5>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Java Crypto API \u043f\u043e\u0434 Android \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0417\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 KeyGenerator \u0432 \u043f\u044d\u043a\u0435\u0434\u0436\u0435 javax.crypto.<\/p>\n<pre><code class=\"java\">mKey = null; try { \tkgen = KeyGenerator.getInstance(&quot;AES&quot;); \tmKey = kgen.generateKey();  } catch (NoSuchAlgorithmException e) { \te.printStackTrace(); } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u0448\u0438\u0444\u0440\u0443\u0435\u043c \u0431\u043b\u043e\u043a\u0438 \u0431\u0430\u0439\u0442\u043e\u0432 \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443 AES \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e javax.crypto.<\/p>\n<pre><code class=\"java\">\/\/ open stream to read origFilepath. We are going to save encrypted contents to outfile \tInputStream fis = new FileInputStream(origFilepath);\t\t \tFile outfile = new File(encFilepath); \tint read = 0; \tif (!outfile.exists()) \t\toutfile.createNewFile(); \t\t \tFileOutputStream encfos = new FileOutputStream(outfile); \t\/\/ Create Cipher using &quot;AES&quot; provider \tCipher encipher = Cipher.getInstance(&quot;AES&quot;); \tencipher.init(Cipher.ENCRYPT_MODE, mKey); \tCipherOutputStream cos = new CipherOutputStream(encfos, encipher); \t\t \t\/\/ capture time it takes to encrypt file \tstart = System.nanoTime(); \tLog.d(TAG, String.valueOf(start)); \t\t \tbyte[] block = new byte[mBlocksize]; \t\t \twhile ((read = fis.read(block,0,mBlocksize)) != -1) { \t\tcos.write(block,0, read); \t} \tcos.close(); \tstop = System.nanoTime(); \t\t \tLog.d(TAG, String.valueOf(stop)); \tseconds = (stop - start) \/ 1000000;\/\/ for milliseconds \tLog.d(TAG, String.valueOf(seconds)); \t\t \tfis.close(); <\/code><\/pre>\n<h5>OpenSSL API<\/h5>\n<p>\u0428\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 OpenSSL \u043f\u043e\u0434 Android \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0421, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 Java \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432\u044b JNI. \u042d\u0442\u043e \u043e\u0442\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0437\u0430\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0448\u0435.<br \/>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043a\u043b\u044e\u0447 \u0438 iv.<\/p>\n<pre><code class=\"cpp\">unsigned char cKeyBuffer[KEYSIZE\/sizeof(unsigned char)]; unsigned char iv[] = &quot;01234567890123456&quot;; int opensslIsSeeded = 0; if (!opensslIsSeeded) { \tif (!RAND_load_file(&quot;\/dev\/urandom&quot;, seedbytes)) { \t\treturn -1; \t} \topensslIsSeeded = 1; }  if (!RAND_bytes((unsigned char *)cKeyBuffer, KEYSIZE )) { } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 (cKeyBuffer) \u0434\u043b\u044f \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c EVP \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430 \u0438 iv. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0430\u0435\u043c \u0431\u043b\u043e\u043a\u0438 \u0431\u0430\u0439\u0442\u043e\u0432 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 EVP_EncryptUpdate. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043f\u043e\u0440\u0446\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432 \u0438\u0437 \u0432\u0430\u0448\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u0440\u043c\u043b\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 EVP_EncryptFinal_ex.<\/p>\n<pre><code class=\"cpp\">if (!(EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), NULL, cKeyBuffer, iv ))) { \tret = -1; \tprintf( &quot;ERROR: EVP_ENCRYPTINIT_EXn&quot;); } \t \/\/ go through file, and encrypt if ( orig_file != NULL ) {    \torigData = new unsigned char[aes_blocksize];     \tencData = new unsigned char[aes_blocksize+EVP_CIPHER_CTX_block_size(e_ctx)]; \/\/ potential for encryption to be 16 bytes longer than original  \tprintf( &quot;Encoding file: %sn&quot;, filename);  \tbytesread = fread(origData, 1, aes_blocksize, orig_file); \t\/\/ read bytes from file, then send to cipher \twhile ( bytesread ) {   \t\tif (!(EVP_EncryptUpdate(e_ctx, encData, &len, origData, bytesread))) { \t\t\tret = -1; \t\t\tprintf( &quot;ERROR: EVP_ENCRYPTUPDATEn&quot;); \t\t} \t\tencData_len = len;  \t\tfwrite(encData, 1, encData_len, enc_file ); \t\t\/\/ read more bytes \t\tbytesread = fread(origData, 1, aes_blocksize, orig_file); \t} \t\/\/ last step encryption \tif (!(EVP_EncryptFinal_ex(e_ctx, encData, &len))) { \t\tret = -1; \t\tprintf( &quot;ERROR: EVP_ENCRYPTFINAL_EXn&quot;); \t} \tencData_len = len;  \tfwrite(encData, 1, encData_len, enc_file );  \t\/\/ free cipher \tEVP_CIPHER_CTX_free(e_ctx); <\/code><\/pre>\n<p>  <a href=\"https:\/\/software.intel.com\/en-us\/android\/articles\/sample-code-data-encryption-application\">\u041e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 Intel IDZ<\/a><br \/>  <a href=\"https:\/\/software.intel.com\/sites\/default\/files\/managed\/13\/2d\/DataEncryption.zip\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/a><\/p>\n<h4><b>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b<\/b><\/h4>\n<p>\u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b (RNG) \u2013 \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0438\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0438\u0441\u0435\u043b \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043a\u0435. RNG \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u043d\u044b\u043c \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043a\u0440\u0438\u043f\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u043c, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0432\u0435\u0440\u0436\u0435\u043d\u043d\u044b\u043c \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u043c \u0430\u0442\u0430\u043a\u0430\u043c \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432 \u0441\u0432\u043e\u0435\u0439 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043b\u0430\u0431\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0430. \u0414\u043b\u044f \u0443\u0441\u0438\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0430 \u0438 \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u0441\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432 \u0446\u0435\u043b\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 RNG.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/1b6\/be4\/ce6\/1b6be4ce6cc4f3d9b936a36ada5b2fc6.png\"\/><\/p>\n<p>  \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0446\u0435\u043b\u044b\u0445 4 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 Android:  <\/p>\n<ul>\n<li>java.util.random<\/li>\n<li>java.security.SecureRandom<\/li>\n<li>\/dev\/urandom<\/li>\n<li>OpenSSL API<\/li>\n<\/ul>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 RNG \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0430, \u0437\u0430\u0449\u0438\u0449\u0430\u044e\u0449\u0435\u0433\u043e \u0432\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 Random \u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f, \u0442\u0430\u043a \u0435\u0433\u043e \u043b\u0435\u0433\u0447\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u0437\u043b\u043e\u043c\u0430\u0442\u044c. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 3 \u043c\u0435\u0442\u043e\u0434\u0430 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0434\u0435\u0436\u043d\u0443\u044e \u0437\u0430\u0449\u0438\u0442\u0443.<\/p>\n<h5>java.util.random<\/h5>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Java Random Number API \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u0412\u044b\u0437\u043e\u0432 Random.nextInt() \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442 4-\u0431\u0430\u0439\u0442\u043d\u043e\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (\u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u2013 2<sup>32<\/sup>). \u042d\u0442\u043e API \u0432\u043f\u043e\u043b\u043d\u0435 \u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430.<\/p>\n<pre><code class=\"java\">for (int i = 0; i &lt; lastVal; i += 2) { \tdataRandomPoints[i] = (rand.nextInt() % widget_width); \tdataRandomPoints[i+1] = (rand.nextInt() % widget_height);  } <\/code><\/pre>\n<h5>java.security.SecureRandom<\/h5>\n<p>SecureRandom \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 java.util.Random \u0432 \u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435, \u0447\u0442\u043e \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 4-\u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. SecureRandom \u043a\u0440\u0438\u043f\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0434\u0435\u0436\u0435\u043d, \u043e\u0434\u043d\u0430\u043a\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 <a href=\"http:\/\/android-developers.blogspot.com\/2013\/08\/some-securerandom-thoughts.html\">\u043d\u0435\u0434\u0430\u0432\u043d\u0435\u0439 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0435\u0439<\/a> \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0442\u0440\u0430\u0432\u043e\u0447\u043d\u0443\u044e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \/dev\/urandom \u0434\u043b\u044f SecureRandom \u043f\u0435\u0440\u0435\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \/dev\/urandom \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f.<\/p>\n<pre><code class=\"java\">SecureRandom srand = new SecureRandom(); shouldDraw = (srand.nextInt() % randomMod ); <\/code><\/pre>\n<p>  <\/p>\n<h5>\/dev\/urandom<\/h5>\n<p>\u0412\u043e \u0432\u0441\u0435\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445 \u0441\u0435\u043c\u0435\u0439\u0441\u0442\u0432\u0430 Linux, \u0432\u043a\u043b\u044e\u0447\u0430\u044f Android, \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u044f\u0434\u0440\u043e\u043c, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c. \u0421\u0440\u0435\u0434\u0438 \u0432\u0441\u0435\u0445 4 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u044d\u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439, \u043e\u043d \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u0440\u0438\u043f\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u044d\u043d\u0442\u0440\u043e\u043f\u0438\u0435\u0439 \u043f\u0443\u0442\u0435\u043c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0448\u0443\u043c\u043e\u0432\u044b\u0445 \u0432\u0435\u043b\u0438\u0447\u0438\u043d \u0438\u0437 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u0435\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u043e\u0432 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432) \u0434\u043b\u044f RNG. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437 \u044f\u0434\u0440\u0430, \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u0444\u0430\u0439\u043b \/dev\/urandom. \/dev\/urandom \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u043e\u043c\u0443 RNG, \u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0432\u043e\u0439 \u0438\u043c\u0435\u0435\u0442\u0441\u044f.<\/p>\n<pre><code class=\"cpp\">unsigned int cKeyBuffer[keysize]; memset(cKeyBuffer, 0, sizeof(unsigned int) * keysize);  FILE *fin; strcpy(filein, &quot;\/dev\/urandom&quot;); fin = fopen(filein, &quot;rb&quot;);  if (fin != NULL) { \tfread(cKeyBuffer, sizeof(int), keysize, fin); \tfclose (fin); } <\/code><\/pre>\n<p>  <\/p>\n<h5>OpenSSL API<\/h5>\n<p>\u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c OpenSSL API \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0432 \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u0421. \u0412 OpenSSL \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u0430\u0442\u0440\u0430\u0432\u043e\u0447\u043d\u044b\u0445 \u0431\u0430\u0439\u0442 \u0438\u0437 \/dev\/urandom \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u0440\u0438\u043f\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b. OpenSSL API \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u043a \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u043e\u043c\u0443 RNG, \u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0432\u043e\u0439 \u0438\u043c\u0435\u0435\u0442\u0441\u044f.<\/p>\n<pre><code class=\"cpp\">int seedbytes = 1024; unsigned int cKeyBuffer[keysize]; memset(cKeyBuffer, 0, sizeof(unsigned int) * keysize);  if (!opensslIsSeeded) {  \tif (!RAND_load_file(&quot;\/dev\/urandom&quot;, seedbytes)) { \t\t__android_log_print(ANDROID_LOG_ERROR, TAG, &quot;Failed to seed OpenSSL RNG&quot;); \t\treturn jKeyBuffer; \t}  \topensslIsSeeded = 1; }  if (!RAND_bytes((unsigned char *)cKeyBuffer, keysize * sizeof(int))) { \t__android_log_print(ANDROID_LOG_ERROR, TAG, &quot;Faled to create OpenSSSL random integers: %ul&quot;, ERR_get_error); } <\/code><\/pre>\n<p>  <a href=\"https:\/\/software.intel.com\/en-us\/android\/articles\/sample-code-random-number-application\">\u041e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 Intel IDZ<\/a><br \/>  <a href=\"https:\/\/software.intel.com\/sites\/default\/files\/managed\/16\/df\/RandomNumber.zip\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/a>      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/company\/intel\/blog\/224285\/\"> http:\/\/habrahabr.ru\/company\/intel\/blog\/224285\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0435\u0433\u043e \u0434\u0432\u0435 \u043e\u0441\u043d\u043e\u0432\u043e\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0449\u0438\u0435 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445: \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0438 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u0430 \u043f\u043e \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u2013 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0441\u043a\u0430\u0447\u0430\u0432 \u0435\u0433\u043e \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-224285","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/224285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=224285"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/224285\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}