编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

安全架构进阶四之非对称加密算法

wxchong 2024-12-07 17:49:29 开源技术 25 ℃ 0 评论

非对称加密算法

非对称加密算法又称为公开密钥加密算法,它需要两个密钥,一个称为公开密钥 public key,即公钥;另一个称为私有密钥 private key,即私钥。公钥和私钥需要配对使用,如果用公钥对数据进行加密,只有用对应的私钥才能进行解密,而如果使用私钥对数据进行加密,那么只有用公钥才能进行解密。因为加密和解密使用的是两个不同的密钥,所以这种算法称为非对称加密算法。

非对称加密算法实现机密信息交换的基本过程是:甲方生成一对密钥并将其中的一把作为公钥向其他人公开,得到该公钥的乙方使用该密钥对机密信息进行加密后再发送给甲方,甲方再使用自己保存的另一把专用密钥即私钥对加密后的信息进行解密,如图:

非对称加密算法的特点:对称加密算法中只有一种密钥,并且是非公开的,如果要解密就得让对方知道密钥,所以保证其安全性就是保证密钥的安全,而一旦密钥在传输过程中泄露,加密信息就不再安全。而非对称加密算法中包含有两种密钥,其中一个是公开的,这样就不需要像对称加密算法那样,需要传输密钥给对方进行数据加密了,大大地提高了加密算法的安全性。非对称加密算法能够保证,即使是在获知公钥,加密算法和加密算法源代码的情况下,也无法获得公钥对应的私钥,因此也无法对公钥加密的密文进行解密。

但是由于非对称加密算法的复杂性,使得其加密解密速度远没有对称加密解密的速度快。为了解决加密速度问题,人们广泛使用对称与非对称加密算法结合使用的办法,优缺点互补,达到时间和安全的平衡;对称加密算法加密速度快,人们用它来加密较长文件,然后用非对称加密算法来给文件密钥加密,解决了对称加密算法的密钥分发问题。

当前使用最为广泛的非对称加密算法非RSA莫属。

RSA算法

RSA非对称加密算法是在1977年由Ron Rivest,Adi Shamirh 和 LenAdleman 开发的,RSA取名来自他们三者的名字。RSA是目前最有影响力的非对称加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但反应过来想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。

基于JAVA的RSA算法的使用:

public class RSATest {

    public static void main(String[] args) throws Exception {
        KeyPair keyPair = getKeyPair();
        String publishKey = getPublishKey(keyPair);
        String privateKey = getPrivateKey(keyPair);

        System.out.println("publishKey=" + publishKey);
        System.out.println("privateKey=" + privateKey);
    }

    /**
     * 密钥对
     */
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        return keyPair;
    }

    /**
     * 获取公钥
     */
    public static String getPublishKey(KeyPair keyPair){
        PublicKey publishKey =   keyPair.getPublic();
        byte[] bytes =  publishKey.getEncoded();

        // Base64编码
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String key =  base64Encoder.encode(bytes);

        return key;
    }

    /**
     * 获取私钥
     */
    public static String getPrivateKey(KeyPair keyPair){
        PrivateKey privateKey =   keyPair.getPrivate();
        byte[] bytes =  privateKey.getEncoded();

        // Base64编码
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String key =  base64Encoder.encode(bytes);

        return key;
    }
}

生成的私钥:

MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAoLsHL7P+4+1e0l0fThS1lPuXAIU0

gh8q+DJCF9GeC5/OtB9wrcowWwyXRviQXuX7FuEMcwz3u5yJ/GOlFy2iFwIDAQABAkAdwaJZ9YPZ

5KcqYO0bwn+/fjh9cyu+b1DANGXCDR+qXPage50G1/KX3YZOwq5hp7qknm5ZbYGX2/zB5ad/YWCJ

AiEA7MeXTMuaNbC1SfFDBW2lVeKXJ7LfJu664/s7E5t9pUsCIQCtxxijx2mn6swluZ2MtSirhtlv

Glx3VK/BmubVyAyS5QIga8FVYLSTxHVbu3/ZcWH5yUEqav2jQnOIh/Iwa3bq0ksCIQCamfNY2fMs

mZ5vvZfl89Gv8DOHPZuf6m7Gvom5DdXp5QIgZNfdvDzjpUo13boHP0uDpCTWqW4F3aVDDwwp9LzS

7RA=

生成的公钥:

MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKC7By+z/uPtXtJdH04UtZT7lwCFNIIfKvgyQhfRnguf

zrQfcK3KMFsMl0b4kF7l+xbhDHMM97ucifxjpRctohcCAwEAAQ==

RSA实战

public class RSATest {

    public static void main(String[] args) throws Exception {
        KeyPair keyPair = getKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        System.out.println("publishKey=" + getPublishKey(keyPair));
        System.out.println("privateKey=" + getPrivateKey(keyPair));

        String data = "this is a test rsa";
        byte[] bytes = publicEncrypt(data.getBytes(), publicKey);
        BASE64Encoder base64Encoder = new BASE64Encoder();
        System.out.println(base64Encoder.encode(bytes));

        bytes = privateDecrypt(bytes, privateKey);
        System.out.println(new String(bytes));
    }

    /**
     * 密钥对
     */
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        return keyPair;
    }

    /**
     * 获取公钥
     */
    public static String getPublishKey(KeyPair keyPair) {
        PublicKey publishKey = keyPair.getPublic();
        byte[] bytes = publishKey.getEncoded();

        // Base64编码
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String key = base64Encoder.encode(bytes);

        return key;
    }

    /**
     * 获取私钥
     */
    public static String getPrivateKey(KeyPair keyPair) {
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();

        // Base64编码
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String key = base64Encoder.encode(bytes);

        return key;
    }

    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = cipher.doFinal(content);

        return bytes;
    }

    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(content);

        return bytes;
    }
}

RSA算法加密生成的密文,使用Base64编码后

XkkqpzN06mxiVygEpgVJTWLpgCkDdYAbEyTjOIaN329ADxaXyk2Gxq3v2uoA1PDoWUO8wTbeZk1u

f7RHoKm07w==

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表