Base64加密算法
/**
* Created by Jay-Tang on 2016/6/18.
*/
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import java.io.IOException;
public class Base64 {
public static String src = "shuaige";
public static void main(String[] args) throws IOException {
jiami();
ccjiami();
bcjiami();
}
public static void jiami() throws IOException {
BASE64Encoder encoder = new BASE64Encoder();
String strdecoder = encoder.encode(src.getBytes());
System.out.println("加密:" + strdecoder);
BASE64Decoder decoder = new BASE64Decoder();
byte[] strdecoder2 = decoder.decodeBuffer(strdecoder);
String str = new String(strdecoder2);
System.out.print("解密:" + str);
}
public static void ccjiami() {
byte[] encodeBase64 = org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes());
System.out.print(new String(encodeBase64));
byte[] decodeBase64=org.apache.commons.codec.binary.Base64.decodeBase64(encodeBase64);
System.out.print(new String(decodeBase64));
}
public static void bcjiami(){
byte[] encodeBase64 = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
System.out.print(new String(encodeBase64));
byte[] decodeBase64=org.bouncycastle.util.encoders.Base64.decode(encodeBase64);
System.out.print(new String(decodeBase64));
}
}
Md消息摘要算法
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Created by Jay-Tang on 2016/6/18.
*/
public class Md {
public static String src = "shuaige";
public static void main(String args[]) throws NoSuchAlgorithmException {
jiamimd5();
jiamimd2();
bcMD4();
bcMD5();
ccMD5();
ccMD2();
}
public static void jiamimd5() throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] toChapter1Digest = md.digest(src.getBytes());
System.out.println(Hex.encodeHex(toChapter1Digest));
}
public static void jiamimd2() throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD2");
byte[] toChapter1Digest = md.digest(src.getBytes());
System.out.println(Hex.encodeHex(toChapter1Digest));
}
public static void bcMD4() {
Digest digest = new MD4Digest();
digest.update(src.getBytes(), 0, src.getBytes().length);
byte[] md4Bytes = new byte[digest.getDigestSize()];
digest.doFinal(md4Bytes, 0);
System.out.println(org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
}
public static void bcMD5() {
Digest digest = new MD5Digest();
digest.update(src.getBytes(), 0, src.getBytes().length);
byte[] md4Bytes = new byte[digest.getDigestSize()];
digest.doFinal(md4Bytes, 0);
System.out.println(org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
}
public static void ccMD5() {
String str = DigestUtils.md5Hex(src.getBytes());
System.out.println(str);
}
public static void ccMD2() {
String str = DigestUtils.md2Hex(src.getBytes());
System.out.println(str);
}
}