java - Encryption compatible between Android and C# -
i've found plenty of examples how encryption in c#, , couple android, i'm particularly looking way handle encrypting (using aes, tripledes, etc.) android, , wind being decrypted in c#. found example encoding aes in android , encoding/decoding aes in c# not sure if these compatible (c# requires iv, nothing specified in android example). also, recommendation on way of encoding encrypted string transmission on http (base64?) helpful. thanks.
got http://oogifu.blogspot.com/2009/01/aes-in-java-and-c.html.
here java class:
package com.neocodenetworks.smsfwd; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import android.util.log; public class crypto { public static final string tag = "smsfwd"; private static cipher aescipher; private static secretkey secretkey; private static ivparameterspec ivparameterspec; private static string cipher_transformation = "aes/cbc/pkcs5padding"; private static string cipher_algorithm = "aes"; // replace me 16-byte key, share between java , c# private static byte[] rawsecretkey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; private static string messagedigest_algorithm = "md5"; public crypto(string passphrase) { byte[] passwordkey = encodedigest(passphrase); try { aescipher = cipher.getinstance(cipher_transformation); } catch (nosuchalgorithmexception e) { log.e(tag, "no such algorithm " + cipher_algorithm, e); } catch (nosuchpaddingexception e) { log.e(tag, "no such padding pkcs5", e); } secretkey = new secretkeyspec(passwordkey, cipher_algorithm); ivparameterspec = new ivparameterspec(rawsecretkey); } public string encryptasbase64(byte[] cleardata) { byte[] encrypteddata = encrypt(cleardata); return net.iharder.base64.base64.encodebytes(encrypteddata); } public byte[] encrypt(byte[] cleardata) { try { aescipher.init(cipher.encrypt_mode, secretkey, ivparameterspec); } catch (invalidkeyexception e) { log.e(tag, "invalid key", e); return null; } catch (invalidalgorithmparameterexception e) { log.e(tag, "invalid algorithm " + cipher_algorithm, e); return null; } byte[] encrypteddata; try { encrypteddata = aescipher.dofinal(cleardata); } catch (illegalblocksizeexception e) { log.e(tag, "illegal block size", e); return null; } catch (badpaddingexception e) { log.e(tag, "bad padding", e); return null; } return encrypteddata; } private byte[] encodedigest(string text) { messagedigest digest; try { digest = messagedigest.getinstance(messagedigest_algorithm); return digest.digest(text.getbytes()); } catch (nosuchalgorithmexception e) { log.e(tag, "no such algorithm " + messagedigest_algorithm, e); } return null; } } i used http://iharder.sourceforge.net/current/java/base64/ base64 encoding.
here's c# class:
using system; using system.text; using system.security.cryptography; namespace smsfwdclient { public class crypto { private icryptotransform rijndaeldecryptor; // replace me 16-byte key, share between java , c# private static byte[] rawsecretkey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; public crypto(string passphrase) { byte[] passwordkey = encodedigest(passphrase); rijndaelmanaged rijndael = new rijndaelmanaged(); rijndaeldecryptor = rijndael.createdecryptor(passwordkey, rawsecretkey); } public string decrypt(byte[] encrypteddata) { byte[] newcleardata = rijndaeldecryptor.transformfinalblock(encrypteddata, 0, encrypteddata.length); return encoding.ascii.getstring(newcleardata); } public string decryptfrombase64(string encryptedbase64) { return decrypt(convert.frombase64string(encryptedbase64)); } private byte[] encodedigest(string text) { md5cryptoserviceprovider x = new system.security.cryptography.md5cryptoserviceprovider(); byte[] data = encoding.ascii.getbytes(text); return x.computehash(data); } } } i hope helps else!
Comments
Post a Comment