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

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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值