美文网首页
加密(一)

加密(一)

作者: 南唐忆梦 | 来源:发表于2019-02-15 11:22 被阅读0次

1.pom.xml

<dependencies>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.47</version>
    </dependency>
</dependencies>

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();
        }
    }
}
运行图:

相关文章

  • iOS常见信息加密方式总结

    目录 MD5加密HMAC加密base64加密对称加密RSA加密 - 非对称加密 一.MD5加密 MD5加密是最常用...

  • 4.加密函数编写

    加密算法分类: md5系列加密 ,哈希算法类型 aes加密对称加密,加密/解密是一个密钥 rsa加密 非对称加密,...

  • 密码学简述

    对称加密 对称加密也称为常规加密、私钥或单钥加密,在公钥加密开发之前,是唯一被使用的加密类型。 对称加密原理 一个...

  • iOS逆向基础04-密码学

    一. 加密常识 1.1 对称加密和非对称加密 对称加密DES:加密标准3DES:加强版DESAES:现在的加密标准...

  • 学习笔记:HTTPS协议原理

    对称加密和非对称加密 加密分两种,对称加密和非对称加密。对称加密是指加密的双方使用同一个密钥加密和解密数据。非对称...

  • 对称加密和非对称加密

    对称加密: 对称加密指的就是加密和解密使用同一个秘钥,所以叫做对称加密。对称加密只有一个秘钥。 非对称加密: 加密...

  • 史上最详HTTPS加密解析

    一、概念 加密类型 1、对称加密,如:常见的AES 加密算法 2、非对称加密,如:常见的RSA 加密算法 对称加密...

  • iOS加密1——概述

    一、简单说明数据安全包含通道加密https和上传数据加密全问题(一些算法加密,对称加密和非对称加密)。加密算法通常...

  • iOS加密——概述

    一、简单说明数据安全包含通道加密https和上传数据加密全问题(一些算法加密,对称加密和非对称加密)。加密算法通常...

  • 第十一章、加密算法相关

    一、理解对称加密与非对称加密 对称加密 对称加密是指加密与解密的使用同一个密钥的加密算法。目前常见的加密算法有:D...

网友评论

      本文标题:加密(一)

      本文链接:https://www.haomeiwen.com/subject/iodpeqtx.html