Encryption algorithm giving different results on Android 2.1 and versions above 2.1 -
i have searched lot before posting question. earlier code working in non android 4.2/2.1 devices. googled , introduced below lines of code. solved partially,ie it's working on 4.2 devices not on froyo.
if (android.os.build.version.sdk_int >= jelly_bean_4_2) { sr = securerandom.getinstance("sha1prng", "crypto"); } else { sr = securerandom.getinstance("sha1prng"); } the below given class use encryption
public class encryption { private final static string hex = "0123456789abcdef"; private final static int jelly_bean_4_2 = 17; private final static byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; public static string encrypt(string seed, string cleartext) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] result = encrypt(rawkey, cleartext.getbytes()); string fromhex = tohex(result); return fromhex; } public static string decrypt(string seed, string encrypted) throws exception { byte[] seedbyte = seed.getbytes(); system.arraycopy(seedbyte, 0, constants.seed, 0, ((seedbyte.length < 16) ? seedbyte.length : 16)); string base64 = new string(base64.decode(encrypted, 0)); byte[] rawkey = getrawkey(seedbyte); byte[] enc = tobyte(base64); byte[] result = decrypt(rawkey, enc); return new string(result); } public static byte[] encryptbytes(string seed, byte[] cleartext) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] result = encrypt(rawkey, cleartext); return result; } public static byte[] decryptbytes(string seed, byte[] encrypted) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] result = decrypt(rawkey, encrypted); return result; } private static byte[] getrawkey(byte[] seed) throws exception { keygenerator kgen = keygenerator.getinstance("aes"); securerandom sr = null; if (android.os.build.version.sdk_int >= jelly_bean_4_2) { sr = securerandom.getinstance("sha1prng", "crypto"); } else { sr = securerandom.getinstance("sha1prng"); } sr.setseed(seed); try { kgen.init(256, sr); } catch (exception e) { // "this device doesn't suppor 256bits, trying 192bits."); try { kgen.init(192, sr); } catch (exception e1) { log.w(log, "this device doesn't suppor 192bits, trying 128bits."); kgen.init(128, sr); } } secretkey skey = kgen.generatekey(); byte[] raw = skey.getencoded(); return raw; } private static byte[] encrypt(byte[] raw, byte[] clear) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode, skeyspec); byte[] encrypted = cipher.dofinal(clear); return encrypted; } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, skeyspec); byte[] decrypted = cipher.dofinal(encrypted); return decrypted; } public static string tohex(string txt) { return tohex(txt.getbytes()); } public static string fromhex(string hex) { return new string(tobyte(hex)); } public static byte[] tobyte(string hexstring) { int len = hexstring.length() / 2; byte[] result = new byte[len]; (int = 0; < len; i++) result[i] = integer.valueof(hexstring.substring(2 * i, 2 * + 2), 16).bytevalue(); return result; } public static string tohex(byte[] buf) { if (buf == null) return ""; stringbuffer result = new stringbuffer(2 * buf.length); (int = 0; < buf.length; i++) { appendhex(result, buf[i]); } return result.tostring(); } private static void appendhex(stringbuffer sb, byte b) { sb.append(hex.charat((b >> 4) & 0x0f)).append(hex.charat(b & 0x0f)); } } the code working on non-froyo devices. on froyo encryption giving different result on non-froyo devices.
you misusing pseudo random number generator , it's seed key derivation function - really bad style. pseudo random number generator "sha1prng" not standard aes - therefore never know implementation get. see is there sha1prng standard?
it makes me no wonder different results. getting deterministic result based on given seed not property can expect pseudo random number function.
if want derive cryptographic key password please use key derivation function pkcs #5 / pbkdf2. implementation of pbkdf2 afair included in bouncy castle.
Comments
Post a Comment