Javax.crypto working differently in different versions of Android OS?

You are misusing a pseudo random number generator and it's seed as a key derivation function - this is really really bad style. The pseudo random number generator "SHA1PRNG" is not a standard like AES - therefore you never know what implementation you get. See also Is there a SHA1PRNG standard?

You are misusing a pseudo random number generator and it's seed as a key derivation function - this is really really bad style. The pseudo random number generator "SHA1PRNG" is not a standard like AES - therefore you never know what implementation you get. See also Is there a SHA1PRNG standard?

It makes me no wonder that you get different results. Getting a deterministic result based on a given seed is not a property you can expect from a pseudo random number functions. If you want to derive a cryptographic key from a password please use a Key Derivation Function like PKCS #5 / PBKDF2.An implementation of PBKDF2 is AFAIR included in Bouncy Castle.

1 So the sample/snippet we used is not very good. Now I use a different key generation algorithm and all is well. (PBEWITHSHAAND256BITAES-CBC-BC, with a sniff of salt and some iv's on the side) – thijs Jun 24 at 13:13.

The answer is in this SO question: BouncyCastle AES error when upgrading to 1.45.

I'd like to thank everyone that contributed to this question. Here is what I ultimately came up with as an example for how to encrypt/decrypt using a password, that seems consistent between Android 2.2 and 2.3. 3. Main Activity: package cc.ndl. Testencryption; import android.app.

Activity; import android.os. Bundle; import android.widget. TextView; public class main extends Activity { TextView tvOutput; static String out; String TEST_STRING = "abcdefghijklmnopqrstuvwxyz"; static String PASSKEY = "ThePasswordIsPassord"; static byte SALT = { 1, 2, 4, 5 }; static int ITERATIONS = 1979; /** Called when the activity is first created.

*/ @Override public void onCreate(Bundle savedInstanceState) { super. OnCreate(savedInstanceState); setContentView(R.layout. Main); tvOutput = (TextView) findViewById(R.id.

TvOutput); } @Override public void onResume() { super.onResume(); out = ""; runTest(); tvOutput. SetText(out); } private void runTest() { out = "Test string: " + TEST_STRING + "\n"; out += "Passkey: " + PASSKEY + "\n"; try { Crypto crypto = new Crypto(PASSKEY); String encryptedData = crypto. Encrypt(TEST_STRING); out += "Encrypted: " + encryptedData + "\n"; out += "Decrypted: " + crypto.

Decrypt(encryptedData); } catch (Exception e) { out += "Error: " + e.getMessage() + "\n"; e.printStackTrace(); } } } Main Layout: Crypto Object: package cc.ndl. Testencryption; import java.security.spec. AlgorithmParameterSpec; import java.security.spec.

KeySpec; import javax.crypto. Cipher; import javax.crypto. SecretKey; import javax.crypto.

SecretKeyFactory; import javax.crypto.spec. PBEKeySpec; import javax.crypto.spec. PBEParameterSpec; public class Crypto { Cipher ecipher; Cipher dcipher; // 8-byte Salt byte salt = { 1, 2, 4, 5, 7, 8, 3, 6 }; // Iteration count int iterationCount = 1979; Crypto(String passPhrase) { try { // Create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.

GetInstance( "PBEWITHSHA256AND128BITAES-CBC-BC"). GenerateSecret(keySpec); ecipher = Cipher. GetInstance(key.getAlgorithm()); dcipher = Cipher.

GetInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); // Create the ciphers ecipher. Init(Cipher. ENCRYPT_MODE, key, paramSpec); dcipher.

Init(Cipher. DECRYPT_MODE, key, paramSpec); } catch (Exception e) { } } public String encrypt(String str) { String rVal; try { // Encode the string into bytes using utf-8 byte utf8 = str. GetBytes("UTF8"); // Encrypt byte enc = ecipher.

DoFinal(utf8); // Encode bytes to base64 to get a string rVal = toHex(enc); } catch (Exception e) { rVal = "Error encrypting: " + e.getMessage(); } return rVal; } public String decrypt(String str) { String rVal; try { // Decode base64 to get bytes byte dec = toByte(str); // Decrypt byte utf8 = dcipher. DoFinal(dec); // Decode using utf-8 rVal = new String(utf8, "UTF8"); } catch (Exception e) { rVal = "Error encrypting: " + e.getMessage(); } return rVal; } private static byte toByte(String hexString) { int len = hexString.length() / 2; byte result = new bytelen; for (int I = 0; I > 4) & 0x0f)). Append(HEX.

CharAt(b & 0x0f)); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions