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