JAVA使用几种对称加密算法

2021-12-08 09:27:38 浏览数 (2)

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

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

    }
}

0 人点赞