Base64和Md加解密

时间:2022-09-27 15:19:10

Base64和Md加解密

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);
}

//用cc加密base64的算法
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));

}
//用bc的base64加密算法
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");
//是16位的字节数组
byte[] toChapter1Digest = md.digest(src.getBytes());
System.out.println(Hex.encodeHex(toChapter1Digest));
}

public static void jiamimd2() throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD2");
//是16位的字节数组
byte[] toChapter1Digest = md.digest(src.getBytes());
System.out.println(Hex.encodeHex(toChapter1Digest));
}

//bcmd4 bc独有的md4
public static void bcMD4() {
Digest digest = new MD4Digest();
//调用update方法
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));
}

//bc的md值
public static void bcMD5() {
Digest digest = new MD5Digest();
//调用update方法
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));
}

//cc的md5
public static void ccMD5() {
String str = DigestUtils.md5Hex(src.getBytes());
System.out.println(str);
}
//cc的md2 简化了jdk里面md2里面的操作
public static void ccMD2() {
String str = DigestUtils.md2Hex(src.getBytes());
System.out.println(str);
}
}