Secure Encrypt Decrypt Algurithm can use for Android,C#,PHP,iPhone -
i'm going encrypt data transferring between multi platform apps. question algorithm can used work these platforms?
that should have these parameters:
- dynamic key , iv.
- supported in c#, android, swift, php.
- be secure enough.
it welcome if give me samples or links each platform.
update:
i tried these classes:
android:
public class cryptor { private ivparameterspec ivspec; private secretkeyspec keyspec; private cipher cipher; public cryptor(byte[] key_par,byte[] iv_par) { keyspec = new secretkeyspec(key_par, "aes"); ivspec = new ivparameterspec(iv_par); try { cipher = cipher.getinstance("aes/cbc/zeropadding"); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (nosuchpaddingexception e) { e.printstacktrace(); } } public byte[] encrypt(byte[] input) throws exception { if(text == null || text.length() == 0) throw new exception("empty input"); byte[] encrypted = null; try { cipher.init(cipher.encrypt_mode, keyspec, ivspec); encrypted = cipher.dofinal(input); } catch (exception e) { throw new exception("[encrypt] " + e.getmessage()); } return encrypted; } public byte[] decrypt(string code) throws exception { if(code == null || code.length() == 0) throw new exception("empty string"); byte[] decrypted = null; try { cipher.init(cipher.decrypt_mode, keyspec, ivspec); decrypted = cipher.dofinal(hextobytes(code)); } catch (exception e) { throw new exception("[decrypt] " + e.getmessage()); } return decrypted; } public static string bytestohex(byte[] data) { if (data==null) { return null; } int len = data.length; string str = ""; (int i=0; i<len; i++) { if ((data[i]&0xff)<16) str = str + "0" + java.lang.integer.tohexstring(data[i]&0xff); else str = str + java.lang.integer.tohexstring(data[i]&0xff); } return str; } public static byte[] hextobytes(string str) { if (str==null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; (int i=0; i<len; i++) { buffer[i] = (byte) integer.parseint(str.substring(i*2,i*2+2),16); } return buffer; } } }
php:
class mcrypt { protected $in_iv; protected $in_key; function __construct($in_key , $in_iv) { $this->key = $in_key; $this->iv = $in_iv; } function encrypt($str) { $iv = $this->iv; $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv); mcrypt_generic_init($td, $this->key, $iv); $encrypted = mcrypt_generic($td, $str); mcrypt_generic_deinit($td); mcrypt_module_close($td); return bin2hex($encrypted); } function decrypt($code) { $code = $this->hex2bin($code); $iv = $this->iv; $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv); mcrypt_generic_init($td, $this->key, $iv); $decrypted = mdecrypt_generic($td, $code); mcrypt_generic_deinit($td); mcrypt_module_close($td); return utf8_encode(trim($decrypted)); } protected function hex2bin($hexdata) { $bindata = ''; ($i = 0; $i < strlen($hexdata); $i += 2) { $bindata .= chr(hexdec(substr($hexdata, $i, 2))); } return $bindata; } }
c#:
class cryptor { private byte[] iv; private byte[] key; public cypher(byte[] key, byte[] iv) { this.key = key; this.iv = iv; } public byte[] encryptrj128(byte[] input) { var encoding = new utf8encoding(); byte[] encrypted; using (var rj = new rijndaelmanaged()) { try { rj.padding = paddingmode.zeros; rj.mode = ciphermode.cbc; rj.keysize = 128; rj.blocksize = 128; rj.key = key; rj.iv = iv; var ms = new memorystream(); var cs = new cryptostream(ms, rj.createencryptor(key, iv), cryptostreammode.write); var sr = new streamwriter(cs); sr.write(input); sr.flush(); cs.flushfinalblock(); encrypted = ms.toarray(); } { rj.clear(); } } return encrypted; } public string decryptrj128(byte[] input) { var sret = ""; var encoding = new utf8encoding(); using (var rj = new rijndaelmanaged()) { try { rj.padding = paddingmode.zeros; rj.mode = ciphermode.cbc; rj.keysize = 128; rj.blocksize = 128; rj.key = key; rj.iv = iv; var ms = new memorystream(input); var cs = new cryptostream(ms, rj.createdecryptor(key, iv), cryptostreammode.read); var sr = new streamreader(cs); sret = sr.readline(); } { rj.clear(); } } return sret; } }
test:
this example used test:
key ={ 106,104,103,97,103,94,115,106,102,96,115,53,53,52,55,53} iv ={104,49,52,56,114,102,103,49,48,50,52,97,56,51,118,52} input ={57,102,117,105,65,75,113,105,108,119,113,54,109,73,89,104}
now trying encrypt:
android , php output: 170, 29, 170, 139, 14, 192, 81, 232, 41, 237, 25, 19, 130, 237, 15, 198 c# output: 59 , 85 , 127 , 29 , 161 , 145 , 23 , 127 , 246 , 100 , 157 , 234 , 128 , 65
Comments
Post a Comment