Go Java DES and Base64

Source: Internet
Author: User
Tags base64

Original Address http://blog.csdn.net/tomatozq/article/details/20773559

1,des

    /** * Decryption * @param message * @param key * @return * @t  Hrows Exception */public static string decrypt (string message,string encoding) throws Exception                  {byte[] bytesrc = converthexstring (message);                  Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");                  Deskeyspec Deskeyspec = new Deskeyspec (key.getbytes (encoding));                  Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");                  Secretkey Secretkey = Keyfactory.generatesecret (Deskeyspec);                        Ivparameterspec IV = new IVPARAMETERSPEC (key.getbytes (encoding));                        Cipher.init (Cipher.decrypt_mode, Secretkey, iv);                  byte[] Retbyte = cipher.dofinal (BYTESRC);              return new String (retbyte,encoding);       }/** * Encryption * @param message       * @param key * @return * @throws Exception */public static STR ing encrypt (String message,string encoding) throws Exception {Cipher Cipher = cipher.getinstance ("DES/CB                        C/pkcs5padding ");                        Deskeyspec Deskeyspec = new Deskeyspec (key.getbytes (encoding));                  Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");                  Secretkey Secretkey = Keyfactory.generatesecret (Deskeyspec);                  Ivparameterspec IV = new IVPARAMETERSPEC (key.getbytes (encoding));                        Cipher.init (Cipher.encrypt_mode, Secretkey, iv);                                    byte[] buf = cipher.dofinal (message.getbytes (encoding));                                    String a = tohexstring (BUF). toUpperCase ();              return A;  The/** * string is converted to a 16 binary array * @param SS * @return */            public static byte[] Converthexstring (String ss) {byte digest[] = new Byte[ss.length ()/2]; for (int i = 0; i < digest.length; i++) {String byteString = ss.substring (2 * I                      , 2 * i + 2);                      int bytevalue = Integer.parseint (byteString, 16);                  Digest[i] = (byte) bytevalue;              } return digest; }/** * 16 binary array converted to String * @param b * @return *                  /public static String tohexstring (byte b[]) {StringBuffer hexstring = new StringBuffer (); for (int i = 0; i < b.length; i++) {String plaintext = integer.tohexstring (0xff                      & B[i]);                      if (Plaintext.length () < 2) plaintext = "0" + plaintext;                Hexstring.append (plaintext);  } return hexstring.tostring ();   }

2,base64

    byte[] enc = Base64.encode (enctext.getbytes (encoding), base64.default);      byte[] bytesrc = Base64.decode (enctext.getbytes (encoding), base64.default);  

  

Go Java DES and Base64

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.