java进行3DES加解密

 

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class mainclazz {
	
	public static void main(String[] args) {
		// key 生成规则  k1 + k2 + k3 其中 k1=k3,每个key长度为8Byte,共16Byte。X9.25标准则k1k2k3都需要独立设定,共24Byte
		byte[] key =  "111111112222222211111111".getBytes();
		byte[] data = {'a','v','d','e','4','b',0,0};//不足8Byte,则填充0
		byte[] iv = {0,0,0,0,0,0,0,0};//初始向量
		try{
			SecretKeySpec kSpec = new SecretKeySpec(key, "DESede");
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
        		Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        		cipher.init(Cipher.ENCRYPT_MODE, kSpec, ivSpec);
        		byte[] encrypt = cipher.doFinal(data);
        		System.out.println(DatatypeConverter.printHexBinary(encrypt));
        		cipher.init(Cipher.DECRYPT_MODE, kSpec, ivSpec);
        		byte[] decrypt = cipher.doFinal(encrypt);
        		System.out.println(new String(decrypt));
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
		}
        
}

 

posted on 2017-11-27 14:06  阮減显  阅读(313)  评论(0编辑  收藏  举报

导航