Having previously been an Android, and recently developing an iphone client, the most troubling of all is the inconsistency between the Java, Android and iphone three platforms plus decryption. Because the end of the phone is usually the Java-developed web service,android and iphone clients invoke the same Web Service interface, for data security considerations, to encrypt the data. The headaches are coming, it's hard to write a set of encryption programs, the results are consistent between 3 platforms, and you can't always write a Web service interface for Android and iphone two clients. I'm sure there will be a lot of friends out there, sharing a set of 3DES encryption programs that will allow Java, Android and iphone three platforms to be decrypted.
The first is the Java side of the encryption tool class, it also applies to the Android side, without any modification, you can ensure that Java and the Android side of the encryption and decryption consistent, and Chinese will not garbled.
Package org.liuyq.des3;
Import Java.security.Key;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESedeKeySpec;
Import Javax.crypto.spec.IvParameterSpec; /** * 3DES Encryption Tool class * * @author Liufeng * @date 2012-10-11/public class Des3 {//Key private final
static String Secretkey = "liuyunqiang@lx100$ #365 #$";
Vector private final static String IV = "01234567";
Encryption and decryption unified use of the encoding method private final static String encoding = "Utf-8"; /** * 3DES Encryption * * @param plaintext Plain text * @return * @throws Exception * * Public s
Tatic string Encode (string plaintext) throws Exception {Key deskey = null;
Desedekeyspec spec = new Desedekeyspec (Secretkey.getbytes ());
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");
Deskey = Keyfactory.generatesecret (spec); Cipher CiphER = cipher.getinstance ("desede/cbc/pkcs5padding");
Ivparameterspec ips = new Ivparameterspec (Iv.getbytes ());
Cipher.init (Cipher.encrypt_mode, Deskey, IPs);
byte[] EncryptData = cipher.dofinal (plaintext.getbytes (encoding));
Return Base64.encode (encryptdata);
/** * 3DES Decryption * * @param encrypttext Encrypted text * @return * @throws Exception
*/public static string decode (String encrypttext) throws Exception {Key deskey = null;
Desedekeyspec spec = new Desedekeyspec (Secretkey.getbytes ());
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");
Deskey = Keyfactory.generatesecret (spec);
Cipher Cipher = cipher.getinstance ("desede/cbc/pkcs5padding");
Ivparameterspec ips = new Ivparameterspec (Iv.getbytes ());
Cipher.init (Cipher.decrypt_mode, Deskey, IPs); byte[] Decryptdata = cipher.dofinal (Base64.decode (Encrypttext));
return new String (Decryptdata, encoding); }
}