JDK一些加密技术应用


/**
*
*/
package encrypt;

import java.math.BigInteger;
import java.security.MessageDigest;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
* @descript EncryptDemo.java
* @author sinclair
* @date Jun 9, 2010
*/
public class EncryptDemo {
public static final String KEY_SHA = "SHA";
public static final String KEY_MD5 = "MD5";
/**
* MAC算法可选以下多种算法 HmacMD5 HmacSHA1 HmacSHA256 HmacSHA384 HmacSHA512
*/
public static final String KEY_MAC = "HmacMD5";

/**
* @param args
* @throws Exception
*/
public static void main( String[] args ) throws Exception {
String src = "admin";
System.out.println( "原数据:\n" + src );
System.out.println( "--------------------MD5加密------------------" );
BigInteger md5 = new BigInteger( encryptMD5( src.getBytes() ) );
String md5_target = md5.toString( 16 );
System.out.println( "MD5加密:\n" + md5_target );
System.out.println( "--------------------SHA加密------------------" );
BigInteger sha5 = new BigInteger( encryptSHA( src.getBytes() ) );
String sha5_target = sha5.toString( 32 );
System.out.println( "SHA加密:\n" + sha5_target );
System.out.println( "--------------------HMAC加密-----------------" );
String key = initMacKey();
System.out.println( "HMAC密钥:\n" + key );
BigInteger hmac = new BigInteger( encryptHMAC( src.getBytes(), key ) );
String hmac_target = hmac.toString( 16 );
System.out.println( "HMAC加密:\n" + hmac_target );
System.out.println( "--------------------BASE64加密和解密----------" );
byte[] inputData = src.getBytes();
String code = encryptBASE64( inputData );
System.out.println( "BASE64加密:\n" + code );
byte[] output = decryptBASE64( code );
String outputStr = new String( output );
System.out.println( "BASE64解密:\n" + outputStr );
}

/**
* MD5加密
*
* @param input
* @return
* @throws Exception
*/
public static byte[] encryptMD5( byte[] input ) throws Exception {
MessageDigest md5 = MessageDigest.getInstance( KEY_MD5 );
// md5.update( input );
return md5.digest( input );
}

/**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptSHA( byte[] data ) throws Exception {
MessageDigest sha = MessageDigest.getInstance( KEY_SHA );
// sha.update( data );
return sha.digest( data );

}

/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64( byte[] key ) throws Exception {
return ( new BASE64Encoder() ).encodeBuffer( key );
}

/**
* BASE64解密
*
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptBASE64( String key ) throws Exception {
return ( new BASE64Decoder() ).decodeBuffer( key );
}

/**
* 初始化HMAC密钥
*
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance( KEY_MAC );

SecretKey secretKey = keyGenerator.generateKey();
return encryptBASE64( secretKey.getEncoded() );
}

/**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptHMAC( byte[] data, String key ) throws Exception {
SecretKey secretKey = new SecretKeySpec( decryptBASE64( key ), KEY_MAC );
Mac mac = Mac.getInstance( secretKey.getAlgorithm() );
mac.init( secretKey );

return mac.doFinal( data );
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值