轉自: http://comtu.github.io/blog/2014/12/01/Java_security.htmlcss
目錄:html
Base64
消息摘要算法
MD SHA MAC 對稱加密算法 DES 3DES AES PBE 非對稱加密算法 DH RSA ElGamal 數字簽名算法 RSA DSA ECDSA 其它算法 IDEA CRC
Base64git
public static void jdkBase64(String msg) { try { BASE64Encoder encoder = new BASE64Encoder(); String encode = encoder.encode(msg.getBytes()); System.out.println("encode:" + encode); BASE64Decoder decoder = new BASE64Decoder(); byte[] b = decoder.decodeBuffer(encode); System.out.println("decoder:" + new String(b)); } catch (Exception e) { } } public static void commonsCodesBase64(String msg) { byte[] encodeBytes = org.apache.commons.codec.binary.Base64.encodeBase64(msg.getBytes()); System.out.println("encode:" + new String(encodeBytes)); byte[] decodeBytes = org.apache.commons.codec.binary.Base64.decodeBase64(encodeBytes); System.out.println("decode:" + new String(decodeBytes)); } public static void bouncyCastleBase64(String msg) { byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(msg.getBytes()); System.out.println("encode:" + new String(encodeBytes)); byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encodeBytes); System.out.println("decode:" + new String(decodeBytes)); }
算法: MD2 實現方:JDK MD4 實現方:BouncyCastle 簡稱BC MD5 實現方:JDK
public static void jdkMD2(String msg) { try { MessageDigest md = MessageDigest.getInstance("MD2"); byte[] md2Bytes = md.digest(msg.getBytes()); System.out.println("JDK MD2:" + org.apache.commons.codec.binary.Hex.encodeHexString(md2Bytes)); } catch (Exception e) { e.printStackTrace(); } } public static void jdkMD5(String msg) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] md5Bytes = md.digest(msg.getBytes()); System.out.println("JDK MD5:" + org.apache.commons.codec.binary.Hex.encodeHexString(md5Bytes)); } catch (Exception e) { e.printStackTrace(); } } // BouncyCastle public static void bcMD4(String msg) { try { Security.addProvider(new BouncyCastleProvider());// 爲JDK設置算法 MessageDigest md = MessageDigest.getInstance("MD4"); byte[] md4Bytes = md.digest(msg.getBytes()); System.out.println("JDK MD5:" + org.apache.commons.codec.binary.Hex.encodeHexString(md4Bytes)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } Digest digest = new MD4Digest(); digest.update(msg.getBytes(), 0, msg.getBytes().length); byte[] md4Byte = new byte[digest.getDigestSize()]; digest.doFinal(md4Byte, 0); System.out.println("BC MD4: " + org.bouncycastle.util.encoders.Hex.toHexString(md4Byte)); } // BouncyCastle public static void bcMD5(String msg) { Digest digest = new MD5Digest(); digest.update(msg.getBytes(), 0, msg.getBytes().length); byte[] md5Byte = new byte[digest.getDigestSize()]; digest.doFinal(md5Byte, 0); System.out.println("BC MD5: " + org.bouncycastle.util.encoders.Hex.toHexString(md5Byte)); } //commons codec public static void ccMD5(String msg){ System.out.println("CC MD5:"+DigestUtils.md5Hex(msg.getBytes())); } //commons codec public static void ccMD2(String msg){ System.out.println("CC MD2:"+DigestUtils.md2Hex(msg.getBytes())); } //----------------- public static final String MD5_TYPE_16 = "t_16"; public static final String MD5_TYPE_32 = "T_32"; public static String md5s(String plainText, String type) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(plainText.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } String str = buf.toString(); if (type.equals(MD5_TYPE_16)) { str = str.substring(8, 24);