中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java非對稱加密(公鑰加密,私鑰解密)

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import javax.crypto.Cipher;

/**
 * 公鑰加密,私鑰解密(非對稱加密)
 * 
 */
public class AsymmetricEncryption {

    public static void main(String[] args) throws Exception {
        publicEnrypy();
        privateEncode();
    }

    /**
     * 加密的方法,使用公鑰進行加密
     * @throws Exception
     */
    public static void publicEnrypy() throws Exception {

        Cipher cipher = Cipher.getInstance("RSA");

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

        // 生成鑰匙對
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        // 得到公鑰
        Key publicKey = keyPair.getPublic();

        // 得到私鑰
        Key privateKey = keyPair.getPrivate();

        // 設(shè)置為加密模式
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        // 對數(shù)據(jù)進行加密
        byte[] result = cipher.doFinal("aaa".getBytes());

        //把私鑰保存到硬盤上
        saveKey(privateKey);

        //把加密后的數(shù)據(jù)保存到硬盤上
        saveData(result);
    }

    /**
     * 解密的方法,使用私鑰進行解密
     * @throws Exception
     */
    public static void privateEncode() throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");

        // 從硬盤中讀取私鑰
        Key privateKey = loadKey();

        //設(shè)置為解密模式,用私鑰解密
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        // 從硬盤中讀取加密后的數(shù)據(jù)
        byte[] data = loadData();

        //對加密后的數(shù)據(jù)進行解密,返回解密后的結(jié)果
        byte[] result = cipher.doFinal(data);

        System.out.println(new String(result));
    }

    /**
     * 從硬盤中加載加密后的文件
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static byte[] loadData() throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream(new File(
                "E://data.data"));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;

        while ((len = fileInputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }

        fileInputStream.close();

        return outputStream.toByteArray();
    }

    /**
     * 從硬盤中加載私鑰
     * @return
     * @throws IOException
     * @throws FileNotFoundException
     * @throws ClassNotFoundException
     */
    private static Key loadKey() throws IOException, FileNotFoundException,
            ClassNotFoundException {
        ObjectInputStream inputStream = new ObjectInputStream(
                new FileInputStream(new File("E://private_key")));
        Key privateKey = (Key) inputStream.readObject();
        return privateKey;
    }

    /**
     * 把加密后的密文保存到硬盤上
     * @param result
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void saveData(byte[] result) throws FileNotFoundException,
            IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File(
                "E://data.data"));
        fileOutputStream.write(result);
    }

    /**
     * 把私鑰保存到硬盤上
     * @param privateKey
     * @throws IOException
     * @throws FileNotFoundException
     */
    private static void saveKey(Key privateKey) throws IOException,
            FileNotFoundException {
        ObjectOutputStream outputStream = new ObjectOutputStream(
                new FileOutputStream(new File("E://private_key")));
        outputStream.writeObject(privateKey);
    }

}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:漢諾塔算法java實現(xiàn)

下一篇:java常用字符串操作函數(shù)