Java加密-对称加密算法

本位内容来自JAVA实现对称加密

Java加密-对称加密算法

对称加密算法是加密密钥等于解密密钥。常见的对称加密算法有:

  • DES,3DES
  • AES
  • PBE
  • IDEA

DES

DES(Data Encryption Standard)数据加密标准

这里写图片描述

jdk方式实现DES的加解密

public static void jdkDES() {

    //生成密钥
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        //指定keysize
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();

        //key转换
        DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        Key convertSecretKey = factory.generateSecret(desKeySpec);

        //加密 
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
        byte[] resultBytes =  cipher.doFinal(src.getBytes());
        System.out.println("jdk des encrypt : " + Hex.encodeHexString(resultBytes));

        //解密
        cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
        resultBytes = cipher.doFinal(resultBytes);
        System.out.println("jdk des decrypt : " + new String(resultBytes));

    } catch (Exception e) {
        e.printStackTrace();
    }


    //jdk des encrypt : 719e81b53b891b1309b138c3e8c8a068cc6d476aaf51c288
    //jdk des decrypt : imooc security des

}

DES应用
DES应用

3DES

jdk实现3DES

private static void jdk3des() {
    // 生成密钥
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
        // 指定keysize
        //keyGenerator.init(168);
        //生成默认长度的KEY
        keyGenerator.init(new SecureRandom());
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();

        // key转换
        DESedeKeySpec desKeySpec = new DESedeKeySpec(bytesKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
        Key convertSecretKey = factory.generateSecret(desKeySpec);

        // 加密
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
        byte[] resultBytes = cipher.doFinal(src.getBytes());
        System.out.println("jdk 3des encrypt : " + Hex.encodeHexString(resultBytes));

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
        resultBytes = cipher.doFinal(resultBytes);
        System.out.println("jdk 3des decrypt : " + new String(resultBytes));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

AES

AES是目前使用最多的对称加密算法
AES的优势之一是至今尚未被破解,AES通常用于移动通信系统加密以及基于SSH协议的软件。

AES

jdk实现方式:

public static void jdkAES() {

    try {
        // 生成key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] keybytes = secretKey.getEncoded();

        // key的转换
        Key key = new SecretKeySpec(keybytes, "AES");

        // 加密
        // AES/工作模式/填充方式
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] result = cipher.doFinal(src.getBytes());
        System.out.println("jdk aes encrypt : " + Base64.encodeBase64String(result));

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, key);
        result = cipher.doFinal(result);
        System.out.println("jdk aes decrypt : " + new String(result));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

AES实际应用流程:

AES实际应用流程

PBE

通过前面的例子,会发现AES、DES和3重DES在使用上比较一致。
PBE算法结合了消息摘要算法和对称加密算法的优点
PBE(Password Based Encryption)基于口令加密

jdk实现PBE:

public static void jdkPBE() {
    try {
        //初始化盐
        SecureRandom random = new SecureRandom();
        byte[] salt = random.generateSeed(8);

        //加密
        //定义密码
        String password = "imooc";
        //把密码转换成密钥
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
        Key key = factory.generateSecret(pbeKeySpec);

        //加密过程
        PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, 100);
        Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
        cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
        byte[] result = cipher.doFinal(src.getBytes());
        System.out.println("jdk pbe encrypt : " + Base64.encodeBase64String(result));

        //解密
        cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
        result = cipher.doFinal(result);
        System.out.println("jdk pbe decrypt : " + new String(result));

    } catch (Exception e) {
        e.printStackTrace();
    }


}

PBE实际应用流程:

实际应用流程

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页