Android, iphone and Java three platform consistent encryption tools

Source: Internet
Author: User
Tags base64 final

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); }  
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.