MD5算法加密~16位、32位、64位
来源:互联网 发布:java 线程间共享变量 编辑:程序博客网 时间:2024/04/29 10:18
package com.ts.u;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
/**
* MD5 encrypt algorithm. <br>
* Encryption irreversible.
* @author Tony_tian
* @version 0.0.1 <br> 2015-10-15 12:57:12
* */
public final class MD5 {
/**Determine encrypt algorithm MD5*/
private static final String ALGORITHM_MD5 = "MD5";
/**UTF-8 Encoding*/
private static final String UTF_8 = "UTF-8";
/**
* MD5 16bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_16bit(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
return MD5.MD5_32bit(readyEncryptStr).substring(8, 24);
}else{
return null;
}
}
/**
* MD5 32bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_32bit(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
//Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
//Use specified byte update digest.
md.update(readyEncryptStr.getBytes());
//Get cipher text
byte [] b = md.digest();
//The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
//byte array switch hexadecimal number.
for(int offset = 0,bLen = b.length; offset < bLen; offset++){
String haxHex = Integer.toHexString(b[offset] & 0xFF);
if(haxHex.length() < 2){
su.append("0");
}
su.append(haxHex);
}
return su.toString();
}else{
return null;
}
}
/**
* MD5 32bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_32bit1(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
//The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
//Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
byte [] b = md.digest(readyEncryptStr.getBytes());
int temp = 0;
//byte array switch hexadecimal number.
for(int offset = 0,bLen = b.length; offset < bLen; offset++){
temp = b[offset];
if(temp < 0){
temp += 256;
}
int d1 = temp / 16;
int d2 = temp % 16;
su.append(Integer.toHexString(d1) + Integer.toHexString(d2)) ;
}
return su.toString();
}else{
return null;
}
}
/**
* MD5 32bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_32bit2(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
//The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
//Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
//Use specified byte update digest.
md.update(readyEncryptStr.getBytes());
byte [] b = md.digest();
int temp = 0;
//byte array switch hexadecimal number.
for(int offset = 0,bLen = b.length; offset < bLen; offset++){
temp = b[offset];
if(temp < 0){
temp += 256;
}
if(temp < 16){
su.append("0");
}
su.append(Integer.toHexString(temp));
}
return su.toString();
}else{
return null;
}
}
/**
* MD5 16bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
* */
public static final String MD5_64bit(String readyEncryptStr) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(md.digest(readyEncryptStr.getBytes(UTF_8)));
}
public static void main(String[] args) {
try {
String md516 = MD5.MD5_16bit("kaka123");
System.out.println("16bit-md5:\n" + md516); //
String md532 = MD5.MD5_32bit("kaka123");
String md5321 = MD5.MD5_32bit1("kaka123");
String md5322 = MD5.MD5_32bit2("kaka123");
System.out.println("32bit-md5:"); //5d052f1e32af4e4ac2544a5fc2a9b992
System.out.println("1: " + md532);
System.out.println("2: " + md5321);
System.out.println("3: " + md5322);
String md564 = MD5.MD5_64bit("kaka123");
System.out.println("64bit-md5:\n" + md564); //
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
/**
* MD5 encrypt algorithm. <br>
* Encryption irreversible.
* @author Tony_tian
* @version 0.0.1 <br> 2015-10-15 12:57:12
* */
public final class MD5 {
/**Determine encrypt algorithm MD5*/
private static final String ALGORITHM_MD5 = "MD5";
/**UTF-8 Encoding*/
private static final String UTF_8 = "UTF-8";
/**
* MD5 16bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_16bit(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
return MD5.MD5_32bit(readyEncryptStr).substring(8, 24);
}else{
return null;
}
}
/**
* MD5 32bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_32bit(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
//Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
//Use specified byte update digest.
md.update(readyEncryptStr.getBytes());
//Get cipher text
byte [] b = md.digest();
//The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
//byte array switch hexadecimal number.
for(int offset = 0,bLen = b.length; offset < bLen; offset++){
String haxHex = Integer.toHexString(b[offset] & 0xFF);
if(haxHex.length() < 2){
su.append("0");
}
su.append(haxHex);
}
return su.toString();
}else{
return null;
}
}
/**
* MD5 32bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_32bit1(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
//The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
//Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
byte [] b = md.digest(readyEncryptStr.getBytes());
int temp = 0;
//byte array switch hexadecimal number.
for(int offset = 0,bLen = b.length; offset < bLen; offset++){
temp = b[offset];
if(temp < 0){
temp += 256;
}
int d1 = temp / 16;
int d2 = temp % 16;
su.append(Integer.toHexString(d1) + Integer.toHexString(d2)) ;
}
return su.toString();
}else{
return null;
}
}
/**
* MD5 32bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* */
public static final String MD5_32bit2(String readyEncryptStr) throws NoSuchAlgorithmException{
if(readyEncryptStr != null){
//The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
//Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
//Use specified byte update digest.
md.update(readyEncryptStr.getBytes());
byte [] b = md.digest();
int temp = 0;
//byte array switch hexadecimal number.
for(int offset = 0,bLen = b.length; offset < bLen; offset++){
temp = b[offset];
if(temp < 0){
temp += 256;
}
if(temp < 16){
su.append("0");
}
su.append(Integer.toHexString(temp));
}
return su.toString();
}else{
return null;
}
}
/**
* MD5 16bit Encrypt Methods.
* @param readyEncryptStr ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
* */
public static final String MD5_64bit(String readyEncryptStr) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(md.digest(readyEncryptStr.getBytes(UTF_8)));
}
public static void main(String[] args) {
try {
String md516 = MD5.MD5_16bit("kaka123");
System.out.println("16bit-md5:\n" + md516); //
String md532 = MD5.MD5_32bit("kaka123");
String md5321 = MD5.MD5_32bit1("kaka123");
String md5322 = MD5.MD5_32bit2("kaka123");
System.out.println("32bit-md5:"); //5d052f1e32af4e4ac2544a5fc2a9b992
System.out.println("1: " + md532);
System.out.println("2: " + md5321);
System.out.println("3: " + md5322);
String md564 = MD5.MD5_64bit("kaka123");
System.out.println("64bit-md5:\n" + md564); //
} catch (Exception e) {
e.printStackTrace();
}
}
}
0 0
- MD5算法加密~16位、32位、64位
- MD5算法加密~16位、32位、64位
- MD5算法加密~16位、32位、64位
- Asp md5加密16位,32位
- MD5 16位 32 位加密实例
- MD5加密 32位 与 16位
- MD5 32位、16位加密
- md5 的16位32位加密
- java MD5 加密(16位/32位)
- MD5 32位加密
- MD5 32位加密
- md5加密32位
- MD5 32位加密
- MD5 32位加密
- md5 32位加密
- MD5 32位加密
- 64位MD5加密
- md5 16 位加密
- Linux动态链接库的使用
- SaaS、PaaS和IaaS的区别
- HDU 1175 连连看 BFS
- iOS自适应行高
- ubuntu14.04 编译 vlc-android
- MD5算法加密~16位、32位、64位
- Android 中对任务设置超时
- android KitKat Translucent 4.4版本支持消息栏半透明
- AES ECB加密实现(java/php/python)
- 网格部件中,根据某一个列的条件设置整行是否可编辑的参考示例
- LeetCode(144) Binary Tree Preorder Traversal解题报告
- java Swing JFileChooser 的使用
- Android自定义控件使用到XML的时候报错no such method
- 银联IC卡卡片规范-AID应用标识符