对称加密算法3DES

package com.imooc.security.des;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import org.apache.commons.codec.binary.Hex;

/**
 * 对称加密算法
 * @author Bingo_Ge
 *
 */
public class Imooc3DES {
	private static String src = "imooc security 3des";
	
	public static void main(String[] args) {
		jdk3DES();
	}
	
	private static void jdk3DES() {
	    try {
	        //1.生成KEY-byte
		KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede"); //固定写法
		keyGenerator.init(168); // 初始化KEY长度
		SecretKey secretKey = keyGenerator.generateKey();
		byte[] bytesKey = secretKey.getEncoded();
					
		// 2.KEY转换
		DESedeKeySpec desKeySpec = new DESedeKeySpec(bytesKey);//放入按指定加密方式生成key的byte数组
		SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");//固定写法
		Key converSecretKey = factory.generateSecret(desKeySpec); // 生成转换后的密钥
					
		// 加密
		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); //生成加密类(固定写法)
		cipher.init(cipher.ENCRYPT_MODE, converSecretKey); //填入加解密模式和密钥
		byte[] result = cipher.doFinal(src.getBytes()); //得到加密后结果数组
		System.out.println("jdk 3des encrypt:" + Hex.encodeHexString(result));
					
		//解密
		cipher.init(cipher.DECRYPT_MODE, converSecretKey); //此处为解密模式,还是需要用到原来的key
		result = cipher.doFinal(result); //得到解密后结果数组
		System.out.println("jdk 3des decrypt:" + new String(result));
					
		} catch (Exception e) {
		    e.printStackTrace();
		}
	}

    // 使用自己定义的密钥加密
    public static String encryptThreeDESECB(String src, String key) throws Exception{
        try {
            if(src == null) {
                return null;
            }

            DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("utf-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey secureKey = keyFactory.generateSecret(dks);

            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCSSPadding");
            cipher.init(Cipher.ENCRYPT_MODE, securekey);
            byte[] b = cipher.doFinal(src.getBytes());

            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(b).replaceAll("\r", "").replaceAll("\n", "");
        } catch (Exception e) {
            throw new Exception(e);
        }
    }


    // 使用自己定义的密钥解密
    public static String decryptThreeDESECB(String src, String key) throws Exception {
    try {
        if(src == null) {
            return null;   
        }

        // 通过base64,将字符串转成byte数组
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytesrc = decoder.decodeBuffer(src);
        // 加密的key
        DESedKeySpec dks = newDESedeKeySpec(key.getBytes("utf-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey securekey = keyFactory.generateSecret(dks);

        // /chipher对象解密
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCSSPadding");
        cipher.init(Cipher.DECRYPT_MODE, securekey);
        byte[] retByte = cipher.doFinal(bytesrc);

        return new String(retByte);
        } catch (Exception e) {
            throw new Eception(e);
        }
    }


}

 

你可能感兴趣的:(java加解密)