您的位置:首页 > 其它

对称加密中DES的加密与解密算法

2017-12-20 16:42 507 查看
package Encoder.symmetry;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

/**

 * 对称加密算法  可逆加密算法

 * DES/3DES/AES的不同加密算法

 * @author CUICHUNCHI

 *

 *DES:通过keygenerator获取密钥生成器实例后,设置des的算法密钥为56,便可生成密钥

 *并且每次生成的密钥都不相同,为了存储,将其密钥通过base64编码转为字符串,将密钥字符串解码后实例化SecretKey

 *,只需要实例化SecretKeySpec即可,返回一个SecretKey

 *加密与解密都需要Cipher对象,初始化时,需要传入加密与解密对应的模式,(Cipher.ENCRYPT_MODE/Cipher.DECRYPT_MODE)

 *以及传入对应的SecretKey密钥即可加密与解密

 */

public class DESUtil {

private static final String DES = "DES";

/**
* 生成 DES 的密钥
* @throws NoSuchAlgorithmException 
*/
public static byte[] getKeyDES() throws NoSuchAlgorithmException{
KeyGenerator key = KeyGenerator.getInstance(DES);
key.init(56);
SecretKey generateKey = key.generateKey();
byte[] encoded = generateKey.getEncoded();

// StringBuffer hexValue = new StringBuffer();

       /* for (int i = 0; i < encoded.length; i++) {

            int val = ((int) encoded[i]) & 0xff;

            if (val < 16) { 

                hexValue.append("0");

            }

            hexValue.append(Integer.toHexString(val));

        }*/

        return encoded;
}
//加载生成一个SecrectKeySpec的实例,返回一个密钥SecretKey
private static SecretKey loadKeyDES(byte [] desStr){
SecretKey key = new SecretKeySpec(desStr,"DES");
return key;
}
/**
* 对原始字符进行DES加密
* @param str
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptDES(String str,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(str.getBytes());
return doFinal;
}

/**
* 解密
* @param b
* @return
* @throws NoSuchPaddingException 
* @throws NoSuchAlgorithmException 
* @throws InvalidKeyException 
* @throws BadPaddingException 
* @throws IllegalBlockSizeException 
*/
public static String decryptDES(byte [] b,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(b);
return new String(doFinal);
}

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String str ="崔春驰";
//生成密钥
byte[] keyStr = DESUtil.getKeyDES();
SecretKey key = loadKeyDES(keyStr);
System.out.println("生成的密钥:"+key);
//调用加密 
byte[] encryptDES = encryptDES(str, key);
System.out.println("加密后:"+encryptDES);
String decryptDES = decryptDES(encryptDES, key);
System.out.println("解密:"+decryptDES);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: