您的位置:首页 > 其它

MD5和Base64

2015-11-12 21:55 204 查看
一. 简述

MD5: 全称为message digest algorithm 5(信息摘要算法), 可以进行加密, 但是不能解密, 属于单向加密, 通常用于文件校验

Base64: 把任意序列的8为字节描述为一种不易为人识别的形式, 通常用于邮件、http加密. 登陆的用户名和密码字段通过它加密, 可以进行加密和解密.

二. 代码

1. MD5:

[java] view
plaincopy





public class MD5Utils {

/**

* 使用md5的算法进行加密

* @param plainText 加密明文

* @return 加密密文

*/

public static String getDigest(String plainText) {

byte[] secretBytes = null;

try {

secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());

} catch (NoSuchAlgorithmException e) {

throw new RuntimeException("error happens", e);

}

return new BigInteger(1, secretBytes).toString(16);

}

}

2. Base64:

[java] view
plaincopy





public class Base64Util {

/**

* 使用Base64进行编码

* @param encodeContent 需要编码的内容

* @return 编码后的内容

*/

public static String encode(String encodeContent) {

if (encodeContent == null) {

return null;

}

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(encodeContent.getBytes());

}

/**

* 使用Base64进行编码

* @param encodeContent 需要编码的内容

* @return 编码后的内容

*/

public static String encode(byte[] encodeText) {

return encode(new String(encodeText));

}

/**

* 使用Base64进行解码

* @param encodeContent 需要解码的内容

* @return 解码后的内容

*/

public static String decode(String decodeContent) {

byte[] bytes = null;

if (decodeContent == null) {

return null;

}

try {

bytes = new BASE64Decoder().decodeBuffer(decodeContent);

} catch (IOException e) {

throw new RuntimeException("error happens", e);

} finally {

}

return new String(bytes);

}

}

3. 测试代码:

[java] view
plaincopy





public class Test {

/**

* 先使用MD5算法加密, 再使用base64算法进行编码

* @param args

*/

public static void main(String[] args) {

String plainText = "pwd";

String encodedPassword = MD5Utils.getDigest(Base64Util.encode(plainText));

System.out.println(encodedPassword);

}

}

为什么使用MD5加密后还要使用Base64编码呢? 用Base64算法编码后得到的是32位长度的字符串, 这样有利于在数据库中进行存储.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: