前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JAVA使用几种对称加密算法

JAVA使用几种对称加密算法

作者头像
前Thoughtworks-杨焱
发布2021-12-08 09:27:38
6250
发布2021-12-08 09:27:38
举报
文章被收录于专栏:杨焱的专栏杨焱的专栏

今天到慕课网看了几集视频,对加密的操作过程还是不知道为什么有这么多步骤以及每个步骤是做什么;但是照着出来了,以后用到的时候翻得看看,直接拿上用了。

AES:

代码语言:javascript
复制
package com.fengyunhe;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

/**
 * 功能:
 * 作者: yangyan
 * 时间: 2015/3/22 .
 */
public class AES {
    private static String src = "i love you";

    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
//        生成KEY
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] encoded = secretKey.getEncoded();


//        转换key
        Key key = new SecretKeySpec(encoded, "AES");
//        加密
        Cipher cipher
                = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(src.getBytes());

        System.out.println("jdk AES encrypt:" + HexBin.encode(bytes));

//        解密

        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes1 = cipher.doFinal(bytes);

        System.out.println("jds AES decrypt:" + new String(bytes1));

    }
}

DES:

代码语言:javascript
复制
package com.fengyunhe;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * 功能: DES 加解密
 * 作者: yangyan
 * 时间: 2015/3/21 .
 */
public class Des {
    private static String src = "i love you";

    public static void main(String[] args) {
        try {
//            生成KEY
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            keyGenerator.init(56);
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] encoded = secretKey.getEncoded();

//            KEY转换
            DESKeySpec desKeySpec = new DESKeySpec(encoded);
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
            Key convertKey = factory.generateSecret(desKeySpec);

//           加密
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, convertKey);
            byte[] result = cipher.doFinal(src.getBytes());

            System.out.println("jdk des encrypt:" + HexBin.encode(result));

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

            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {


        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }

}

DESTriple:

代码语言:javascript
复制
package com.fengyunhe;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;

import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * 功能:
 * 作者: yangyan
 * 时间: 2015/3/22 .
 */
public class Des3 {
    private static String src = "i love you";

    public static void main(String[] args) {
        try {
//            生成KEY
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
            keyGenerator.init(168);
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] encoded = secretKey.getEncoded();

//            KEY转换
            DESedeKeySpec desKeySpec = new DESedeKeySpec(encoded);
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
            Key convertKey = factory.generateSecret(desKeySpec);

//           加密
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, convertKey);
            byte[] result = cipher.doFinal(src.getBytes());

            System.out.println("jdk 3des encrypt:" + HexBin.encode(result));

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

            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {


        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }
}

PBE:

代码语言:javascript
复制
package com.fengyunhe;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;

import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

/**
 * 功能:
 * 作者: yangyan
 * 时间: 2015/3/22 .
 */
public class PBE {

    private static String src = "i love you";

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
//      初始化salt
        SecureRandom random = new SecureRandom();
        byte[] salt = random.generateSeed(8);

//      口令于秘钥
        String password = "yangyan";

        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5andDES");
        SecretKey secretKey = factory.generateSecret(pbeKeySpec);

//  加密
        PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);
        Cipher cipher = Cipher.getInstance("PBEWithMD5andDES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
        byte[] bytes = cipher.doFinal(src.getBytes());
        System.out.println("encrypt by PBE: " + HexBin.encode(bytes));


//        解密

        cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpec);
        byte[] bytes1 = cipher.doFinal(bytes);
        System.out.println("decrypt by PBE:" + new String(bytes1));

    }
}
本文参与?腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com