Common encryption for java, android and objectivec -
i want use encryption , decryption algorithms common java, android , objective-c. tried aes in java , android, both returns different output. there common algorithm java, android , objective-c.
here java code
import java.security.*; import java.security.spec.invalidkeyspecexception; import javax.crypto.*; import javax.crypto.spec.secretkeyspec; import sun.misc.*; public class aesencrp { private static final string algo = "aes"; private static final byte[] keyvalue = new byte[] { 'w', 'e', 'l', 'c', 'o', 'm', 'e','t', 'o', 'e', 'n','c', 'r', 'y', 'p', 't' }; public static string encrypt(string data) throws exception { key key = generatekey(); cipher c = cipher.getinstance(algo); c.init(cipher.encrypt_mode, key); byte[] encval = c.dofinal(data.getbytes()); string encryptedvalue = new base64encoder().encode(encval); return encryptedvalue; } public static string decrypt(string encrypteddata) throws exception { key key = generatekey(); cipher c = cipher.getinstance(algo); c.init(cipher.decrypt_mode, key); byte[] decordedvalue = new base64decoder().decodebuffer(encrypteddata); byte[] decvalue = c.dofinal(decordedvalue); string decryptedvalue = new string(decvalue); return decryptedvalue; } private static key generatekey() throws exception { key key = new secretkeyspec(keyvalue, algo); return key; } public static void main(string[] args) throws exception { // string password = "1"; // string passwordenc = aesencrp.encrypt(password); string passworddec = aesencrp.decrypt("1"); // system.out.println("plain text : " + password); // system.out.println("encrypted text : " + passwordenc); system.out.println("decrypted text : " + passworddec); } }
and here android code
import java.security.securerandom; import javax.crypto.cipher; import javax.crypto.keygenerator; import javax.crypto.secretkey; import javax.crypto.spec.secretkeyspec; import android.util.base64; public class encodedecodeaes { private final static string hex = "0123456789abcdef"; private final static int jelly_bean_4_2 = 17; private final static byte[] key = { 'w', 'e', 'l', 'c', 'o', 'm', 'e','t', 'o', 'e', 'n','c', 'r', 'y', 'p', 't' }; // static { // security.addprovider(new bouncycastleprovider()); // } 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); string base64 = new string(base64.encodetostring(fromhex.getbytes(), 0)); return base64; } public static string decrypt(string seed, string encrypted) throws exception { byte[] seedbyte = seed.getbytes(); system.arraycopy(seedbyte, 0, key, 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"); // , "sc"); 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); // kgen.init(128, sr); } catch (exception e) { // log.w(log, "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"); // /ecb/pkcs7padding", "sc"); 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"); // /ecb/pkcs7padding", "sc"); 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)); } }
i used same java code in android , instead of base64encoder used base64 , works perfectly.
private static byte[] keyvalue = new byte[]{ 'w', 'e', 'l', 'c', 'o', 'm', 'e','t', 'o', 'e', 'n','c', 'r', 'y', 'p', 't' }; private string seedwith16chars = new string(keyvalue); private string texttoencrypt = "1"; private textview seed; private textview text; private textview encryptedvalue; private textview decryptedvalue; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); seed = (textview) findviewbyid(r.id.seedname); seed.settext(seedwith16chars); text = (textview) findviewbyid(r.id.texttoencrypt); text.settext(texttoencrypt); encryptedvalue = (textview) findviewbyid(r.id.encryptedtext); decryptedvalue = (textview) findviewbyid(r.id.decryptedtext); try { // value got when did run 2.3.3 device galaxy sii running android 4.0.4 string encrypted = ""; // uncomment line bellow , comment line above run on android 4.1.2 or older. // string encrypted = encodedecodeaes.encrypt(seedwith16chars, texttoencrypt); log.e("encrypt", encrypted); encrypted = encrypt(texttoencrypt);//encodedecodeaes.encrypt(seedwith16chars, texttoencrypt); encryptedvalue.settext("encrypt "+encrypted); string decrypted = decrypt(encrypted);//encodedecodeaes.decrypt(seedwith16chars, encrypted); decryptedvalue.settext("decrypt "+decrypted); log.e("decrypt", decrypted); } catch (exception e) { log.e("exception", e.getlocalizedmessage()); } } public static string encrypt(string data) throws exception { key key = generatekey(); cipher c = cipher.getinstance("aes"); c.init(cipher.encrypt_mode, key); byte[] encval = c.dofinal(data.getbytes()); string encryptedvalue = base64.encodetostring(encval, 0); return encryptedvalue; } public static string decrypt(string encrypteddata) throws exception { key key = generatekey(); cipher c = cipher.getinstance("aes"); c.init(cipher.decrypt_mode, key); byte[] decordedvalue = base64.decode(encrypteddata, 0); byte[] decvalue = c.dofinal(decordedvalue); string decryptedvalue = new string(decvalue); return decryptedvalue; } private static key generatekey() throws exception { key key = new secretkeyspec(keyvalue, "aes"); return key; }
Comments
Post a Comment