首页 > 编程语言 > 详细

java加解密算法--DES

时间:2020-02-11 23:32:02      阅读:108      评论:0      收藏:0      [点我收藏+]
  • ECB
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

public class decrypt {
    public static byte[] initSecretKey() throws NoSuchAlgorithmException {
        //指定算法秘钥生成器
        KeyGenerator kg = KeyGenerator.getInstance("des");
        //初始化秘钥生成器,使其具有确定到秘钥大小
        kg.init(56);
        //生成秘钥
        SecretKey secretkey = kg.generateKey();
        return secretkey.getEncoded();
    }

    public static byte[] encrypt(byte[] key, String src) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        //生成秘钥
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

        /**
         * 加密实际操作Cipher
         */
        //创建Cipher对象
        Cipher cipher = Cipher.getInstance("des/ecb/PKCS5Padding");
        //初始化Cipher
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] data = src.getBytes();
        //加密
        byte[] encryptedData = cipher.doFinal(data);

    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException {
        //byte[] secretKey = initSecretKey();
        //或者代码中约定key
        String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
        byte[] secretKey = key.getBytes();
        String str = "abc";
        byte[] encryptedData = encrypt(secretKey, str);
        String decrypteData = decrypt(secretKey, encryptedData);
    }

    public static String decrypt(byte[] key, byte[] encryptedData) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { 
        //jdk初始化秘钥转化des秘钥 
         DESKeySpec desKeySpec = new DESKeySpec(key); 
        // 创建秘钥工厂实例 
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des"); 
        // 生成秘钥 
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); 
        /** * 解密实际操作Cipher */ 
        //创建Cipher对象 
        Cipher cipher = Cipher.getInstance("des/ecb/PKCS5Padding");
        //初始化Cipher 
        cipher.init(Cipher.DECRYPT_MODE,secretKey); 
        //加密 
        byte[] dencryptedData = cipher.doFinal(encryptedData); 
        return new String(dencryptedData); 
    }
}

  

  • CBC

与ECB差别,加解密都要使用初始化向量,且初始化向量一致

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

ECB

public class decrypt {
    public static byte[] initSecretKey() throws NoSuchAlgorithmException {
        //指定算法秘钥生成器
        KeyGenerator kg = KeyGenerator.getInstance("des");
        //初始化秘钥生成器,使其具有确定到秘钥大小
        kg.init(56);
        //生成秘钥
        SecretKey secretkey = kg.generateKey();
        return secretkey.getEncoded();
    }

    public static byte[] encrypt(byte[] key, String src) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        //生成秘钥
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

        /**
         * 加密实际操作Cipher
         */
        //创建Cipher对象
        Cipher cipher = Cipher.getInstance("des/ecb/PKCS5Padding");
        //初始化Cipher
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] data = src.getBytes();
        //加密
        byte[] encryptedData = cipher.doFinal(data);

    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException {
        //byte[] secretKey = initSecretKey();
        //或者代码中约定key
        String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
        byte[] secretKey = key.getBytes();
        String str = "abc";
        byte[] encryptedData = encrypt(secretKey, str);
        String decrypteData = decrypt(secretKey, encryptedData);
    }

    public static String decrypt(byte[] key, byte[] encryptedData) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
        //jdk初始化秘钥转化des秘钥 
        DESKeySpec desKeySpec = new DESKeySpec(key);
        // 创建秘钥工厂实例 
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        // 生成秘钥 
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        /** * 解密实际操作Cipher */
        //创建Cipher对象 
        Cipher cipher = Cipher.getInstance("des/ecb/PKCS5Padding");
        //初始化Cipher 
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        //加密 
        byte[] dencryptedData = cipher.doFinal(encryptedData);
        return new String(dencryptedData);
    }
}
  

        CBC
        与ECB差别,加解密都要使用初始化向量,且初始化向量一致

        import sun.misc.BASE64Decoder;

        import javax.crypto.*;
        import javax.crypto.spec.DESKeySpec;
        import javax.crypto.spec.IvParameterSpec;
        import java.io.IOException;
        import java.security.InvalidAlgorithmParameterException;
        import java.security.InvalidKeyException;
        import java.security.Key;
        import java.security.NoSuchAlgorithmException;
        import java.security.spec.InvalidKeySpecException;

public class encrypt {

    public static byte[] initSecretKey() throws NoSuchAlgorithmException {
        //指定算法秘钥生成器
        KeyGenerator kg = KeyGenerator.getInstance("des");
        //初始化秘钥生成器,使其具有确定到秘钥大小
        kg.init(56);
        //生成秘钥
        SecretKey secretkey = kg.generateKey();
        return secretkey.getEncoded();
    }

    public static byte[] encrypt(byte[] key, String src,byte[] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        //生成秘钥
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

        /**
         * 加密实际操作Cipher
         */
        //创建Cipher对象
        Cipher cipher = Cipher.getInstance("des/cbc/PKCS5Padding");
        //创建初始化向量
        IvParameterSpec iv = new IvParameterSpec(keyIv);
        //初始化Cipher
        cipher.init(Cipher.ENCRYPT_MODE,secretKey,iv);
        byte[] data = src.getBytes();
        //加密
        byte[] encryptedData = cipher.doFinal(data);

    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException, InvalidAlgorithmParameterException {
        //byte[] secretKey = initSecretKey();
        //或者代码中约定key
        String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
        byte[] secretKey = key.getBytes();
        byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8}; 
        String str = "abc"; 
        byte[] encryptedData = encrypt(secretKey, str,iv); 
        String decrypteData = decrypt(secretKey, encryptedData,iv); 
    } 
    public static String decrypt(byte[] key,byte[] encryptedData,byte[] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException { 
        //jdk初始化秘钥转化des秘钥 
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例 
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des"); 
        //生成秘钥 
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); 
        /** * 解密实际操作Cipher */ 
        //创建Cipher对象 
        Cipher cipher = Cipher.getInstance("des/cbc/PKCS5Padding"); 
        //创建初始化向量 
        IvParameterSpec iv = new IvParameterSpec(keyIv); 
        //初始化Cipher 
        cipher.init(Cipher.DECRYPT_MODE,secretKey,iv); 
        //加密 
        byte[] dencryptedData = cipher.doFinal(encryptedData); 
        return new String(dencryptedData); 
    } 
}

  

DES与3DES同样到工作模式和填充模式相同,它们加解密区别:

  • jdk初始化秘钥转化des秘钥

  DES:

DESKeySpec desKeySpec = new DESKeySpec(key);

  3DES:

DESedeKeySpec spec = new DESedeKeySpec(key);
  • 创建秘钥工厂实例  

  DES:

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");

  3DES:

SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");

  

java加解密算法--DES

原文:https://www.cnblogs.com/ivy-xu/p/12297194.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!