性能優化專題五--加密算法

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章