加解密---对称加密算法

1、概述:

    1.1  加密秘钥=解密秘钥

    1.2  属于初等加密算法

2、对称加密算法:

    1.1  DES

        Data Encryption Standard   数据加密标准

package com.exiuge.mytest;


import org.bouncycastle.util.encoders.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;

public class JdkDES {

    private static String src="hello,rose";

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

    public static void enDeCode(String src){
        try {
            //生成key
            KeyGenerator keyGenerator=KeyGenerator.getInstance("DES");
            keyGenerator.init(56);
            SecretKey secretKey =keyGenerator.generateKey();
            byte[] bytesKey=secretKey.getEncoded();

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

            //加密
            Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5padding");
            cipher.init(Cipher.ENCRYPT_MODE,convertKey);
            byte[] desBytes=cipher.doFinal(src.getBytes());
            System.out.println(Hex.toHexString(desBytes));
            System.out.println(new String(desBytes,"utf-8"));

            //解密
            cipher.init(Cipher.DECRYPT_MODE,convertKey);
            byte[] srcBytes=cipher.doFinal(desBytes);
            System.out.println(new String(srcBytes));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
package com.exiuge.mytest;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.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;
import java.security.Security;

public class BCDES {

    private static String src="hello,rose";

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

    public static void enDeCode(String src){
        try {
            Security.addProvider(new BouncyCastleProvider());
            //生成key
            KeyGenerator keyGenerator=KeyGenerator.getInstance("DES","BC");
            keyGenerator.init(56);
            SecretKey secretKey =keyGenerator.generateKey();
            byte[] bytesKey=secretKey.getEncoded();

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

            //加密
            Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5padding");
            cipher.init(Cipher.ENCRYPT_MODE,convertKey);
            byte[] desBytes=cipher.doFinal(src.getBytes());
            System.out.println(Hex.toHexString(desBytes));
            System.out.println(new String(desBytes,"utf-8"));

            //解密
            cipher.init(Cipher.DECRYPT_MODE,convertKey);
            byte[] srcBytes=cipher.doFinal(desBytes);
            System.out.println(new String(srcBytes));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

    1.2  3DES(Triple DES、DESede)

package com.exiuge.mytest;

import org.bouncycastle.util.encoders.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;
import java.security.SecureRandom;

public class Jdk3DES {
    private static String src="hello,rose";

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

    public static void enDeCode(String src){
        try {
            //生成key
            KeyGenerator keyGenerator=KeyGenerator.getInstance("DESede");
            //keyGenerator.init(168);
            //生成一个默认长度的秘钥
            keyGenerator.init(new SecureRandom());
            SecretKey secretKey =keyGenerator.generateKey();
            byte[] bytesKey=secretKey.getEncoded();

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

            //加密
            Cipher cipher=Cipher.getInstance("DESede/ECB/PKCS5padding");
            cipher.init(Cipher.ENCRYPT_MODE,convertKey);
            byte[] desBytes=cipher.doFinal(src.getBytes());
            System.out.println(Hex.toHexString(desBytes));

            //解密
            cipher.init(Cipher.DECRYPT_MODE,convertKey);
            byte[] srcBytes=cipher.doFinal(desBytes);
            System.out.println(new String(srcBytes));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

    1.3  AES

        AES运行效率比DES高、目前还未被破解;

        常用于  移动通信系统的加密  、基于SSH协议的软件(SSH Client、secureCRT);

        

        

package com.exiuge.mytest;

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;
import java.security.SecureRandom;

public class JdkAES {

    private static String src="hello,rose";

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

    public static void enDeCode(String src){
        try {
            //生成key
            KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
            keyGenerator.init(new SecureRandom());
            SecretKey secretKey =keyGenerator.generateKey();
            byte[] keyEncoded=secretKey.getEncoded();

            //转换key
            Key convertKey =new SecretKeySpec(keyEncoded,"AES");

            //加密
            Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE,convertKey);
            byte[] desBytes=cipher.doFinal(src.getBytes());
            System.out.println(Base64.encodeBase64String(desBytes));

            //解密
            cipher.init(Cipher.DECRYPT_MODE,convertKey);
            byte[] srcBytes=cipher.doFinal(desBytes);
            System.out.println(new String(srcBytes));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

    1.4  PBE(Password Based Encryption)

          基于口令加密

        

            

           

package com.exiuge.mytest;

import org.bouncycastle.util.encoders.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;

public class JdkPBE {

    private static String src="hello,rose";

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

    public static void enDeCode(String src){
        try {
            //初始化盐(随机数)
            SecureRandom secureRandom=new SecureRandom();
            byte[] saltBytes=secureRandom.generateSeed(8);

            //口令
            String pwd="123456";
            PBEKeySpec pbeKeySpec=new PBEKeySpec(pwd.toCharArray());
            SecretKeyFactory secretKeyFactory=SecretKeyFactory.getInstance("PBEWITHMD5andDES");
       //秘钥 Key key
=secretKeyFactory.generateSecret(pbeKeySpec); //加密 PBEParameterSpec pbeParameterSpec=new PBEParameterSpec(saltBytes,100); Cipher cipher=Cipher.getInstance("PBEWITHMD5andDES"); cipher.init(Cipher.ENCRYPT_MODE,key,pbeParameterSpec); byte[] desBytes=cipher.doFinal(src.getBytes()); System.out.println(Base64.toBase64String(desBytes)); //解密 cipher.init(Cipher.DECRYPT_MODE,key,pbeParameterSpec); byte[] srcBytes=cipher.doFinal(desBytes); System.out.println(new String(srcBytes)); }catch (Exception e){ e.printStackTrace(); } } }   

    1.5  IDEA

    

 

posted on 2019-02-20 10:29  anpeiyong  阅读(706)  评论(0编辑  收藏  举报

导航