对称加密算法(DES、3重DES、AES和PBE)

对称加密算法,就是公钥和私钥是一样的。这里主要有DES、3重DES、AES和PBE等,以下为各个对称加密算法的加解密使用方法,所有的类均引自JDK,无需另外导包(视情况可能需要阿帕奇的commons.codec包)。

其中AES为目前使用最多的,因为安全性上从未被破解过。

PBE是基于口令的加密,使用中要加入salt(盐--扰码)进行使用。

 

1、DES

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

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;

/**
 * @Author Rhine
 * @Date 2019/1/17 20:19
 **/
public class ImoocDES {

    private static String src="imooc security des";

    public static void main(String[] args) {
        jdkAES();
    }

    public static void jdkAES(){
        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");
            Key convertSecretKey=factory.generateSecret(desKeySpec);

            //加密
            Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);

            byte[] result=cipher.doFinal(src.getBytes());
            System.out.println("jdk des encrypt: "+ Hex.encodeHexString(result));

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

        }catch (Exception e){

        }
    }
}

 

2、3重DES

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

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;

/**
 * @Author Rhine
 * @Date 2019/1/17 20:08
 **/
public class Imooc3DES {

    private static String src="imooc security 3des";

    public static void main(String[] args) {
        jdk3DES();
    }

    public static void jdk3DES(){
        try {
            //生成key
            KeyGenerator keyGenerator=KeyGenerator.getInstance("DESede");
            keyGenerator.init(168);
            SecretKey secretKey=keyGenerator.generateKey();
            byte[] byteKey=secretKey.getEncoded();

            //key转化
            DESedeKeySpec deSedeKeySpec=new DESedeKeySpec(byteKey);
            SecretKeyFactory factory=SecretKeyFactory.getInstance("DESede");
            Key convertSecretKey=factory.generateSecret(deSedeKeySpec);

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

            //解密
            cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
            result=cipher.doFinal(result);
            System.out.println("jdk 3des decrypt: "+ new String(result));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

3、AES(目前使用最多的,未被破解过)

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

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * @Author Rhine
 * @Date 2019/1/17 20:19
 **/
public class ImoocAES {

    private static String src="imooc security aes";

    public static void main(String[] args) {
        jdkAES();
    }

    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");

            //加密
            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){

        }
    }
}

4、PBE

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

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.Key;
import java.security.SecureRandom;

/**
 * @Author Rhine
 * @Date 2019/1/17 23:17
 **/
public class ImoocPBE {

    private static String src="imooc security pbe";

    public static void main(String[] args) {
        jdkPBE();
    }

    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 pbeParameterSpec=new PBEParameterSpec(salt,100);
            Cipher cipher=Cipher.getInstance("PBEWITHMD5andDES");
            cipher.init(Cipher.ENCRYPT_MODE,key,pbeParameterSpec);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("jdk pbe encrypt: "+Base64.encodeBase64String(result));

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

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

 

你可能感兴趣的:(工作杂病论)