3.2 RSA算法简介

2018-11-30  本文已影响0人  saillen

非对称加密技术 -- RSA算法

RSA算法是流行最广泛的非对称加密算法,也是唯一的基于因式分解的非对称加密算法。相比DH算法,RSA算法更重要。

发展历史

1978年MIT三位学者提出对称加密算法:RSA算法,随后RSA算法被广泛应用。非对称加密算法的破解一直受人关注:

应用场景和特点

RSA的应用场景和DH算法一样:密钥协商。但是RSA算法比DH算法简单,没有那么复杂。
DH算法的安全隐患明显:公钥传输的过程中是容易泄露和被截获的,RSA算法没有这个担心。

RSA算法不直接参与数据加密,而是参与对称加密算法的密钥交换工作,而且一般情况下,我们不需要自己编程。

一定要遵循:公钥加密,私钥解密;私钥加密,公钥解密的原则;

Java中算法实现

Java支持RSA算法,工作模式为ECB模式,不能选择,密钥长度是512-65535,默认是1024,填充方式有多种可以选择。

public class RSATest {

    public static final String KEY_ALGORITHM = "RSA";
    private static final int KEY_SIZE = 1024;

    public static void main(String[] args) throws Exception {
        // 1.产生密钥对
        KeyPair keyPair = initKey();
        byte[] privateKey = keyPair.getPrivate().getEncoded();
        byte[] publicKey = keyPair.getPublic().getEncoded();

        log("Public Key : %s", toBase64(publicKey));
        log("Private Key : %s", toBase64(privateKey));

        String input = "要加密的数据";
        // 2.私钥加密
        byte[] priKeyData = encrpty(getPrivateKey(privateKey), input.getBytes());
        byte[] rs1 = decrpty(getPublicKey(publicKey), priKeyData);
        log("私钥加密:%s , 公钥解密:%s ", toBase64(priKeyData), new String(rs1));

        byte[] pubKeyData = encrpty(getPublicKey(publicKey), input.getBytes());
        byte[] rs2 = decrpty(getPrivateKey(privateKey), pubKeyData);
        log("公钥加密:%s , 私钥解密:%s ", toBase64(pubKeyData), new String(rs2));
    }

    private static KeyPair initKey() throws Exception {
        KeyPairGenerator keyPairGr = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGr.initialize(KEY_SIZE);
        KeyPair keyPair = keyPairGr.generateKeyPair();
        // RSAPublicKey
        return keyPair;
    }

    private static PrivateKey getPrivateKey(byte[] priKey) throws Exception {
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
        return privateKey;
    }

    private static PublicKey getPublicKey(byte[] pubKey) throws Exception {
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
        return publicKey;
    }

    private static byte[] decrpty(Key key, byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance(key.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(data);
    }

    private static byte[] encrpty(Key key, byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance(key.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    }

    private static void log(String tmp, Object... params) {
        System.out.println(String.format(tmp, params));
    }

    private static String toBase64(byte[] data) {
        return new String(Base64.getEncoder().encode(data));
    }

}
上一篇下一篇

猜你喜欢

热点阅读