java - How can I find an Android encryption library or class compatable with my C# code? -
i using following class data encryption in c# .net. know equivalent library or algorithm in java android transferring data between android , web-service?
using system; using system.data; using system.configuration; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; using system.security.cryptography; using system.io; using system.text; /// <summary> /// summary description crypto /// </summary> public enum symmprovenum : int { des, rc2, rijndael } public class crypto { string globalkey = "test"; private symmetricalgorithm mobjcryptoservice; /// <remarks> /// constructor using intrinsic .net symmetricalgorithm class. /// </remarks> public crypto() { //switch (netselected) //{ // case symmprovenum.des: // mobjcryptoservice = new descryptoserviceprovider(); // break; // case symmprovenum.rc2: // mobjcryptoservice = new rc2cryptoserviceprovider(); // break; // case symmprovenum.rijndael: // mobjcryptoservice = new rijndaelmanaged(); // break; //} mobjcryptoservice = new rijndaelmanaged(); } /// <remarks> /// constructor using customized symmetricalgorithm class. /// </remarks> //public crypto(symmetricalgorithm serviceprovider) //{ // mobjcryptoservice = serviceprovider; //} /// <remarks> /// depending on legal key size limitations of specific cryptoservice provider /// , length of private key provided, padding secret key space character /// meet legal size of algorithm. /// </remarks> private byte[] getlegalkey(string key) { string key = globalkey; string stemp; if (mobjcryptoservice.legalkeysizes.length > 0) { int lesssize = 0, moresize = mobjcryptoservice.legalkeysizes[0].minsize; // key sizes in bits while (key.length * 8 > moresize) { lesssize = moresize; moresize += mobjcryptoservice.legalkeysizes[0].skipsize; } stemp = key.padright(moresize / 8, ' '); } else stemp = key; // convert secret key byte array return asciiencoding.ascii.getbytes(stemp); } public string encrypt(string source) { string key = globalkey; byte[] bytin = system.text.asciiencoding.ascii.getbytes(source); // create memorystream process can done without i/o files system.io.memorystream ms = new system.io.memorystream(); byte[] bytkey = getlegalkey(key); // set private key mobjcryptoservice.key = bytkey; mobjcryptoservice.iv = bytkey; // create encryptor provider service instance icryptotransform encrypto = mobjcryptoservice.createencryptor(); // create crypto stream transforms stream using encryption cryptostream cs = new cryptostream(ms, encrypto, cryptostreammode.write); // write out encrypted content memorystream cs.write(bytin, 0, bytin.length); cs.flushfinalblock(); // output , trim '\0' bytes byte[] bytout = ms.getbuffer(); int = 0; (i = 0; < bytout.length; i++) if (bytout[i] == 0) break; // convert base64 result can used in xml return system.convert.tobase64string(bytout, 0, i); } public string decrypt(string source) { string key = globalkey; // convert base64 binary byte[] bytin = system.convert.frombase64string(source); // create memorystream input system.io.memorystream ms = new system.io.memorystream(bytin, 0, bytin.length); byte[] bytkey = getlegalkey(key); // set private key mobjcryptoservice.key = bytkey; mobjcryptoservice.iv = bytkey; // create decryptor provider service instance icryptotransform encrypto = mobjcryptoservice.createdecryptor(); // create crypto stream transforms stream using decryption cryptostream cs = new cryptostream(ms, encrypto, cryptostreammode.read); // read out result crypto stream system.io.streamreader sr = new system.io.streamreader( cs ); return sr.readtoend(); } }
i wrote crypto class should work code, given correct configuration.
you can check out here: https://github.com/untouchab1e/yubinotes/blob/master/android/src/com/connectutb/yubinotes/util/crypto.java
you're using des in example, flawed , obsolete algorithm (alternatively use 3des), suggest moving aes provides better encryption.
Comments
Post a Comment