加密(一)

1.pom.xml


    
        commons-codec
        commons-codec
        1.10
    
    
        org.bouncycastle
        bcprov-jdk15on
        1.47
    

2.ImoocDES.java

package com.mypro.symmetry.des;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
/**
 * Created by wgg on 2017/6/19.
 */
public class ImoocDES {
    public static String src ="ImoocDES";
    public static void main(String[] args) {
        jdkDES();
        bsDES();
    }
    public static void jdkDES(){
        try {
            //生成key
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            keyGenerator.init(56);//给定默认长度
            System.out.println("jdk Provider:"+keyGenerator.getProvider());
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] bytesKey =secretKey.getEncoded();
            System.out.println("bytesKey:"+Hex.encodeHexString(bytesKey));

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

            System.out.println("Key:"+convertSecretKey);

            //加密
            Cipher cipher =Cipher.getInstance("DES/ECB/PKCS5Padding");
            //加密模式,和key
            cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);
            byte[] result = cipher.doFinal(src.getBytes("UTF-8"));

            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,"UTF-8"));

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }


    public static void bsDES(){
        try {
            Security.addProvider(new BouncyCastleProvider());
            //生成key
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES","BC");
            keyGenerator.init(56);//给定默认长度
            System.out.println("bc Provider:"+keyGenerator.getProvider());
            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");
            //加密模式,和key
            cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);
            byte[] result = cipher.doFinal(src.getBytes("UTF-8"));
            System.out.println("BC des encrypt:"+ Hex.encodeHexString(result));

            cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
            result = cipher.doFinal(result);

            System.out.println("BC des decrypt:"+new String(result,"UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
    }
}
运行图:

3.Imooc3DES.java

package com.mypro.symmetry.des;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;

/**
 * Created by wgg on 2017/6/19.
 */
public class Imooc3DES 
    public static String src ="Imooc3DES";
    public static void main(String[] args) {
        jdk3DES();
       // bc3DES();
    }
    public static void jdk3DES(){
        try {
            //生成key
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
            //keyGenerator.init(112);//给定默认长度
            keyGenerator.init(new SecureRandom());
            System.out.println("jdk Provider:"+keyGenerator.getProvider());
            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");
            //加密模式,和key
            cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);
            byte[] result = cipher.doFinal(src.getBytes("UTF-8"));

            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,"UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }
    public static void bc3DES() {

    }
}
运行图:

4.ImoocAES.java

package com.mypro.symmetry.des;

import org.apache.commons.codec.binary.Hex;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * Created by Administrator on 2017/6/19.
 */
public class ImoocAES {

    public static String src ="ImoocAES";
    public static void main(String[] args) {
        jdkAES();
    }
    public static void jdkAES(){
        try {
            //生成key
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            //keyGenerator.init(56);//给定默认长度
            keyGenerator.init(new SecureRandom());
            System.out.println("jdk Provider:"+keyGenerator.getProvider());
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] bytesKey =secretKey.getEncoded();

            Key key = new SecretKeySpec(bytesKey,"AES");

             //加密
            Cipher cipher =Cipher.getInstance("AES/ECB/PKCS5Padding");
            //加密模式,和key
            cipher.init(Cipher.ENCRYPT_MODE,key);
            byte[] result = cipher.doFinal(src.getBytes("UTF-8"));
            System.out.println("jdk aes encrypt:"+ Hex.encodeHexString(result));
            cipher.init(Cipher.DECRYPT_MODE,key);
            result = cipher.doFinal(result);
            System.out.println("jdk aes decrypt:"+new String(result,"UTF-8"));

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
运行图:

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