3DES Encryption-java/oc

Source: Internet
Author: User
Tags base64
One of the most troubling aspects of mobile development 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, and the results are consistent between 3 platforms, and it's not always possible to 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.
Java code    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    */   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";       & nbsp;  /**       * 3des Encryption        *         *  @param  plainText  Plain text         *  @return        *  @throws  Exception         */       public static 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 ());          &NBsp; cipher.init (cipher.decrypt_mode, deskey, ips);               byte[] decryptdata = cipher.dofinal (Base64.decode (EncryptText));               return new string ( decryptdata, encoding);       }  }  

The encryption tool class above uses the Base64 class, which has the following source code:
Java code Import Java.io.ByteArrayOutputStream;   Import java.io.IOException;      Import Java.io.OutputStream; /** * BASE64 Coding Tool Class * * public class Base64 {private static final char[] Legalchars = "Abcdefghijklmnopqrstuvwxy zabcdefghijklmnopqrstuvwxyz0123456789+/". ToCharArray ();

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.