Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is with SecureRandom generation. It is giving different results on different platforms. It's because of a bug fix on line 320 (in Gingerbread source) of SHA1PRNG_SecureRandomImpl.java in the engineNextBytes() method where</p> <pre><code>bits = seedLength &lt;&lt; 3 + 64; </code></pre> <p>was changed to</p> <pre><code>bits = (seedLength &lt;&lt; 3) + 64; </code></pre> <p>Use SecretKeyFactory() to generate a Secure key instead of secure random.</p> <pre><code>public class Crypto { Cipher ecipher; Cipher dcipher; byte[] salt = { 1, 2, 4, 5, 7, 8, 3, 6 }; int iterationCount = 1979; Crypto(String passPhase) { try { // Create the key KeySpec keySpec = new PBEKeySpec(passPhase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWITHSHA256AND128BITAES-CBC-BC").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } catch (Exception e) { // TODO: handle exception //Toast.makeText(this, "I cought ", Toast.LENGTH_LONG).show(); } } public String encrypt(String str) { String rVal; try { byte[] utf8 = str.getBytes("UTF8"); byte[] enc = ecipher.doFinal(utf8); rVal = toHex(enc); } catch (Exception e) { // TODO: handle exception rVal = "Exception Caught "+e.getMessage(); } return rVal; } public String decrypt(String str) { String rVal; try { byte[] dec = toByte(str); byte[] utf8 = dcipher.doFinal(dec); rVal = new String(utf8, "UTF8"); } catch(Exception e) { rVal = "Error in decrypting :"+e.getMessage(); } return rVal; } private static byte[] toByte(String hexString ) { int len = hexString.length()/2; byte[] result = new byte[len]; for ( int i=0; i&lt;len; i++ ) { result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16 ).byteValue(); } return result; } private static String toHex(byte[] buf) { if (buf == null) return ""; StringBuffer result = new StringBuffer( 2*buf.length); for ( int i=0; i&lt;buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private final static String HEX = "0123456789ABCDEF"; private static void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b &gt;&gt; 4) &amp; 0x0f)).append(HEX.charAt(b &amp; 0x0f)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload