欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

性能优化专题五--加密算法

程序员文章站 2022-07-02 10:38:51
...

SHA1算法:

import org.apache.commons.codec.digest.Sha2Crypt;
import org.junit.Test;

public class SHA {
    @Test
    public void test() {
        String result = Sha2Crypt.sha256Crypt("buder".getBytes());
        System.out.println(result);
    }
}

buder加密后的结果: 

性能优化专题五--加密算法

RSA算法:

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

import javax.crypto.Cipher;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

public class RSA {
    public static String ALGORITHM = "RSA";

    //指定key的位数
    public static int KEYSIZE = 1024;//65536

    //指定公钥存放的文件
    public static String PUBLIC_KEY_FILE = "public_key.dat";

    //指定私钥存放的文件
    public static String PRIVATE_KEY_FILE = "private_key.dat";


    public static void generateKeyPair() throws Exception {
        SecureRandom sr = new SecureRandom();
        //需要一个KeyPairGenerator来生成钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
        keyPairGenerator.initialize(KEYSIZE, sr);
        //生成
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        Key publicKey = keyPair.getPublic();
        Key privateKey = keyPair.getPrivate();

        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
        ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));

        objectOutputStream1.writeObject(publicKey);
        objectOutputStream2.writeObject(privateKey);
        objectOutputStream2.close();
        objectOutputStream1.close();

    }

    /**
     * 加密
     */
    public static String encrypt(String source) throws Exception {
        generateKeyPair();
        //取出公钥
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
        Key key = (Key) ois.readObject();
        ois.close();
        //开始使用
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] b = source.getBytes();
        byte[] b1 = cipher.doFinal(b);
        //转一下base64
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(b1);

    }

    /**
     * 解密
     */
    public static String decrypt(String source) throws Exception {

        //取出公钥
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
        Key key = (Key) ois.readObject();
        ois.close();
        //开始使用
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(source);
        byte[] b1 = cipher.doFinal(b);

        return new String(b1);

    }

    @Test
    public void test() throws Exception {
        String content = "buder123";
        String password = encrypt(content);
        System.out.println("密文" + password);

        //到了服务器以后
        String target = decrypt(password);
        System.out.println("明文" + target);
    }
}

 buder123加解密后的结果:

性能优化专题五--加密算法

AES算法:

import org.junit.Test;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AES {
    public static String ALGORITHM = "AES";

    public static byte[] encrypt(String content, String password) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
        //用用户密码作为随机数初始化
        kgen.init(128, new SecureRandom(password.getBytes()));
        //得到一个**
        SecretKey secretKey = kgen.generateKey();
        //对钥密进行基本的编码
        byte[] enCodeFormat = secretKey.getEncoded();
        //转换成AES专用的**
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM);
        //创建一个密码器
        Cipher cipher = Cipher.getInstance(ALGORITHM);


        byte[] byteContent = content.getBytes();
        //开始加密了
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] result = cipher.doFinal(byteContent);

        return result;

    }

    public static byte[] decrypt(byte[] content, String password) throws Exception {
        //创建AES的key生产者
        KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
        //利用用户密码作为随机数初始化
        kgen.init(128, new SecureRandom(password.getBytes()));
        //根据用户密码,生成一个**  (所有对称算法通用的)
        SecretKey secretKey = kgen.generateKey();
        //对**进行基本的编码
        byte[] enCodeFormat = secretKey.getEncoded();
        //转换成AES专用的** RoundKey
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM);
        //创建一个密码器
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        //解密
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] result = cipher.doFinal(content);
        return result;
    }

    @Test
    public void test() throws Exception {
        String content = "buder666";
        String password = "123";

        byte[] encryptByte = encrypt(content, password);
        System.out.println("加密的数据:" + new String(encryptByte));

        byte[] decrypt = decrypt(encryptByte, password);
        System.out.println("解密后的效果:" + new String(decrypt));

    }

}

buder666加解密后的结果:

性能优化专题五--加密算法

完整地址

在项目的Test测试文件下:https://github.com/buder-cp/base_component_learn/tree/master/performanceOPT/buderdn08