对称加密之DES

1、用到的jar包:http://pan.baidu.com/s/1mgIbLvq提取码:ytj9

package com.tan.sucurity;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider;
import org.bouncycastle.util.encoders.Hex;

public class MyDES {

	
	public static void main(String[] args) {
		
      MyDES myDES=new MyDES();
      myDES.jdkDES("imooc secuity des");
      myDES.bcDES("imooc secuity des");
	}
	
	public void jdkDES(String src){
		try {
			//生成一个KEY
			KeyGenerator keyGenerator=KeyGenerator.getInstance("DES");
			
			keyGenerator.init(56);
			SecretKey secretKey=keyGenerator.generateKey();
			byte[] byteKey=secretKey.getEncoded();
			//KEY的转换
			DESKeySpec desKeySpec=new DESKeySpec(byteKey);
			SecretKeyFactory factory=SecretKeyFactory.getInstance("DES");
			SecretKey converSecrettKey=factory.generateSecret(desKeySpec);
			
			//加密
			Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");//加密用到的条件
			cipher.init(Cipher.ENCRYPT_MODE, converSecrettKey);
			byte[] result=cipher.doFinal(src.getBytes());
           
            
			System.out.println("jdk des encrypt:"+Hex.toHexString(result));
			
			//解密操作
			cipher.init(Cipher.DECRYPT_MODE, converSecrettKey);
			result=cipher.doFinal(result);
			System.out.println("jdk des encrypt:"+new String(result));
			
		} catch (NoSuchAlgorithmException e) {
			
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
    
	public void bcDES(String src){
	   Security.addProvider(new BouncyCastleProvider());
	   try {
			//生成一个KEY
			KeyGenerator keyGenerator=KeyGenerator.getInstance("DES","BC");
			keyGenerator.getProvider();
			keyGenerator.init(56);
			SecretKey secretKey=keyGenerator.generateKey();
			byte[] byteKey=secretKey.getEncoded();
			//KEY的转换
			DESKeySpec desKeySpec=new DESKeySpec(byteKey);
			SecretKeyFactory factory=SecretKeyFactory.getInstance("DES");
			SecretKey converSecrettKey=factory.generateSecret(desKeySpec);
			
			//加密
			Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");//加密用到的条件
			cipher.init(Cipher.ENCRYPT_MODE, converSecrettKey);
			byte[] result=cipher.doFinal(src.getBytes());
          
           
			System.out.println("bc des encrypt:"+Hex.toHexString(result));
			
			//解密操作
			cipher.init(Cipher.DECRYPT_MODE, converSecrettKey);
			result=cipher.doFinal(result);
			System.out.println("bc des encrypt:"+new String(result));
			
		} catch (NoSuchAlgorithmException e) {
			
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值