java - How to use 3DES algorithm on Android? -


on server side, encyption/decryption of password field done in c#.

now, need implement same functionality in android application. so, followed tutorial: http://ttux.net/post/3des-java-encrypter-des-java-encryption/ below:

    import java.security.messagedigest;  import java.security.spec.keyspec;  import java.util.arrays;   import javax.crypto.cipher;  import javax.crypto.secretkey;  import javax.crypto.secretkeyfactory;  import javax.crypto.spec.desedekeyspec;  import javax.crypto.spec.ivparameterspec;   import org.apache.commons.codec.binary.base64;   public class encrypter {    private keyspec keyspec;    private secretkey key;    private ivparameterspec iv;     public encrypter(string keystring, string ivstring) {      try {        final messagedigest md = messagedigest.getinstance("md5");        final byte[] digestofpassword = md.digest(base64.decodebase64(keystring.getbytes("utf-8")));        final byte[] keybytes = arrays.copyof(digestofpassword, 24);        (int j = 0, k = 16; j < 8;) {      keybytes[k++] = keybytes[j++];        }         keyspec = new desedekeyspec(keybytes);         key = secretkeyfactory.getinstance("desede").generatesecret(keyspec);         iv = new ivparameterspec(ivstring.getbytes());      } catch(exception e) {        e.printstacktrace();      }    }     public string encrypt(string value) {      try {        cipher ecipher = cipher.getinstance("desede/cbc/pkcs5padding","sunjce");        ecipher.init(cipher.encrypt_mode, key, iv);         if(value==null)      return null;         // encode string bytes using utf-8        byte[] utf8 = value.getbytes("utf8");         // encrypt        byte[] enc = ecipher.dofinal(utf8);         // encode bytes base64 string        return new string(base64.encodebase64(enc),"utf-8");      } catch (exception e) {        e.printstacktrace();      }      return null;    }     public string decrypt(string value) {      try {        cipher dcipher = cipher.getinstance("desede/cbc/pkcs5padding","sunjce");        dcipher.init(cipher.decrypt_mode, key, iv);         if(value==null)      return null;         // decode base64 bytes        byte[] dec = base64.decodebase64(value.getbytes());         // decrypt        byte[] utf8 = dcipher.dofinal(dec);         // decode using utf-8        return new string(utf8, "utf8");      } catch (exception e) {        e.printstacktrace();      }      return null;    }  }  

but dont know values need provide keyvalue , ivvalue above code. please me...

use code encrypt string

import javax.crypto.cipher; import javax.crypto.spec.ivparameterspec; import javax.crypto.spec.secretkeyspec;  import android.util.base64; //string encryption public class encryptionhelper {        // encrypts string , encode in base64     public static string encrypttext(string plaintext) throws exception {         // ---- use specified 3des key , iv other source --------------         byte[] plaintext = plaintext.getbytes();//input         byte[] tdeskeydata = constants.getkey().getbytes();// encryption key          byte[] myiv = constants.getinitializationvector().getbytes();// initialization vector          cipher c3des = cipher.getinstance("desede/cbc/pkcs5padding");         secretkeyspec mykey = new secretkeyspec(tdeskeydata, "desede");         ivparameterspec ivspec = new ivparameterspec(myiv);          c3des.init(cipher.encrypt_mode, mykey, ivspec);         byte[] ciphertext = c3des.dofinal(plaintext);         string encryptedstring = base64.encodetostring(ciphertext,                 base64.default);         // return base64coder.encodestring(new string(ciphertext));         return encryptedstring;     }  } 

this how can encrypt string

string encryptedpassword = encryptionhelper.encrypttext(edttext.gettext().tostring()); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -