码迷,mamicode.com
首页 > 编程语言 > 详细

Java DES加密实现

时间:2018-07-06 15:56:39      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:stat   null   rac   秘钥   author   get   alt   ati   图片   

下面是代码的实现:

package com.smt.cipher.symmetry;

import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

/**
 * DES  工具
 * @author ZHANGYUKUN
 *
 */
public class AESUtil {
	public static final String  ENCODING = Charset.defaultCharset().name();
	
	
	/**
	 * 加密
	 * @param data 原文
	 * @param cipherStr 秘钥字符字符串(8的倍数)
	 * @return
	 */
	public static String encrypt( String content , String secretkeyStr ) {
		String rt = null;
		try {
			byte[] data = getDESCipher(secretkeyStr, Cipher.ENCRYPT_MODE ).doFinal(  content.getBytes(ENCODING) );
			rt  = new String(Base64.getEncoder().encode(data), ENCODING);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rt;
	}
	
	/**
	 * 解密
	 * @param data 解密
	 * @param cipherStr 秘钥字符串(8的倍数)
	 * @return
	 */
	public static String descrypt(String data , String secretkeyStr) {
		String rt = null;
		try {
			byte[] cipher = getDESCipher(secretkeyStr, Cipher.DECRYPT_MODE ).doFinal( Base64.getDecoder().decode( data.getBytes() ) );
			rt = new String( cipher,ENCODING );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rt;
	}
	
	
	/**
	 * 得到密码
	 * @param secretkeyStr
	 * @param mode
	 * @return
	 */
	public static Cipher getDESCipher( String secretkeyStr,int mode ) {
		int remainder = secretkeyStr.length()%8;
		if( remainder != 0 ) {
			throw new RuntimeException("秘钥字符串必须是8的倍数");
		}
		
		try {
			KeyGenerator keygen =KeyGenerator.getInstance("AES");
			keygen.init(128, new SecureRandom(secretkeyStr.getBytes(ENCODING)));
			SecretKey secretKey=keygen.generateKey();
			
			Cipher  cipher = Cipher.getInstance("AES");
			cipher.init( mode ,  secretKey );
			return cipher;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

}

  

测试代码:

package com.smt.cipher;

import java.security.NoSuchAlgorithmException;

import com.smt.cipher.symmetry.DESUtil;

public class Main2 {

	public static void main(String[] args) throws NoSuchAlgorithmException {
		System.out.println(   DESUtil.encrypt("测试文本DES", "abcdabcd" ) );
		System.out.println(   DESUtil.descrypt( DESUtil.encrypt("测试文本DES", "abcdabcd" ) , "abcdabcd"));
	}

}

  

 

输出结果:

 

技术分享图片

 

Java DES加密实现

标签:stat   null   rac   秘钥   author   get   alt   ati   图片   

原文地址:https://www.cnblogs.com/cxygg/p/9273588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!