浅谈RSA

作者: 没八阿哥的程序 | 来源:发表于2018-11-01 16:01 被阅读8次

    RSA概述

    首先看这个加密算法的命名.很有意思,它其实是三个人的名字.早在1977年由麻省理工学院的三位数学家Rivest、Shamir 和 Adleman一起提出了这个加密算法,并且用他们三个人姓氏开头字母命名.
    RSA加密算法是一种非对称加密算法,其玩法打破了以往所有加密算法的规则.在RSA出现之前,所有的加密方法都是同一种模式:加密解密的规则使用同一种方式.这种长达几个世纪的加密方案有一个致命的缺陷.在传递加密信息时,必须让对方拿到解密的规则才能正常解密.由于加密解密的规则一致,所以保存和传递"密钥",就成了最头疼的问题。
    RSA的出现解决了这个问题.我们来看看RSA是怎么玩的.

    RSA加密/解密

    • 使用公钥加密的数据,利用私钥进行解密
    • 使用私钥加密的数据,利用公钥进行解密

    没错,RSA加密使用了"一对"密钥.分别是公钥私钥,这个公钥和私钥其实就是一组数字!其二进制位长度可以是1024位或者2048位.长度越长其加密强度越大,目前为止公之于众的能破解的最大长度为768位密钥,只要高于768位,相对就比较安全.所以目前为止,这种加密算法一直被广泛使用.

    RSA的弊端

    由于RSA算法的原理都是大数计算,使得RSA最快的情况也比对称加密算法慢上好几倍。速度一直是RSA的缺陷,一般来说RSA只用于小数据的加密.RSA的速度是对应同样安全级别的对称加密算法的1/1000左右。

    RSA终端命令演示

    由于Mac系统内置OpenSSL(开源加密库),所以我们可以直接在终端上使用命令来玩RSA.
    OpenSSL中RSA算法常用指令主要有三个,其他指令此处不介绍。

    命令 含义
    genrsa 生成并输入一个RSA私钥
    rsautl 使用RSA密钥进行加密、解密、签名和验证等运算
    rsa 处理RSA密钥的格式转换等问题

    生成RSA私钥,密钥长度为1024bit

    hank$ openssl genrsa -out private.pem 1024
    Generating RSA private key, 1024 bit long modulus
    ..++++++
    ..........................................++++++
    e is 65537 (0x10001)
    
    

    从私钥中提取公钥

    hank$ openssl rsa -in private.pem -pubout -out public.pem
    writing RSA key
    
    

    我们可以看到生成的私钥和公钥文件如下图:

    密钥文件

    显得非常高大上对吧!那么它里面是什么,我们可以利用终端进行查看.

    //查看私钥文件
    hank$ cat private.pem
    -----BEGIN RSA PRIVATE KEY-----
    MIICWwIBAAKBgQDbGfA0XdkIpK5h2O9mg5o35pitxwiHDnlpBTCTUH+pkGMdDe6d
    9nVQDr61QUEMWAgbnb/irTXh5VigGhHDbG/4kmVy1BgSfLxUx50jmm7jnvnS4Hrb
    65g920x26gaBW+I9n9cHF/QShrqaNXP9DDeqhqNzdmrkaaAQQkQ9liN6awIDAQAB
    AoGAU0gdvNn7WES4oCrEfPQDF8KIQG3KOQPwdFHrr+NGU161veKA0/xNhTvFk8IV
    BqsjkdO5j2EFfTMfJ+Qg4maCfIZN+xknosXRUF3vz5CUz/rXwBupOlOiWFJbB6cV
    /Jee045DjiHjciip/ZVd8A2xnUEg4pIFUujAFPH+22t5TvkCQQD73bRqCQF9sWIA
    tBeNR10Mygx5wrwKvjgCvaawsgx82kuAb3CWR0G81GfU+lK0YaHdmcFHsAHlDncM
    OtY6IPnNAkEA3rKP6+/jUoylsJPWuN9LyuKjtAlsNtbWaYvs8iCNhLyV9hoWjvow
    AAZB1uWy5aLDtQI3v48beExwsJEFAlQtFwJASTkKU21s1or0T/oLgtJFdgtjlx6L
    JqBojjtus53/zWh1XNCJLddngCtMSHnCA5kCwvcJXvsHgf0zlQWh9GJT3QJAY0+q
    EwN1kpiaQzaKqQMbX6zWaDFTitkf4Q2/avLNaYZYMdnMeZJk2X3w2o6wyutc71m/
    1rNRAsLD9lmVrEYxnQJAEAHb0lsRgWe/sXX2attg4NbDsEExqDZ+7GGsyvqZn1Xg
    S/UPdt6rVkVQ3N7ZEPKV6SxwN9LySI4lVWmFWhCn6w==
    -----END RSA PRIVATE KEY-----
    //查看公钥文件
    hank$ cat public.pem
    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbGfA0XdkIpK5h2O9mg5o35pit
    xwiHDnlpBTCTUH+pkGMdDe6d9nVQDr61QUEMWAgbnb/irTXh5VigGhHDbG/4kmVy
    1BgSfLxUx50jmm7jnvnS4Hrb65g920x26gaBW+I9n9cHF/QShrqaNXP9DDeqhqNz
    dmrkaaAQQkQ9liN6awIDAQAB
    -----END PUBLIC KEY-----
    
    

    其实就是一个文本文件,并且一看就知道是base64编码.那么公钥相比私钥要简单很多.我们可以通过命令,将私钥转换成为明文看看.

    //转化为明文信息
    hank$ openssl rsa -in private.pem -text -out private.txt
    writing RSA key
    //查看文本信息
    hank$ cat private.txt
    Private-Key: (1024 bit)
    modulus:
        00:db:19:f0:34:5d:d9:08:a4:ae:61:d8:ef:66:83:
        9a:37:e6:98:ad:c7:08:87:0e:79:69:05:30:93:50:
        7f:a9:90:63:1d:0d:ee:9d:f6:75:50:0e:be:b5:41:
        41:0c:58:08:1b:9d:bf:e2:ad:35:e1:e5:58:a0:1a:
        11:c3:6c:6f:f8:92:65:72:d4:18:12:7c:bc:54:c7:
        9d:23:9a:6e:e3:9e:f9:d2:e0:7a:db:eb:98:3d:db:
        4c:76:ea:06:81:5b:e2:3d:9f:d7:07:17:f4:12:86:
        ba:9a:35:73:fd:0c:37:aa:86:a3:73:76:6a:e4:69:
        a0:10:42:44:3d:96:23:7a:6b
    publicExponent: 65537 (0x10001)
    privateExponent:
        53:48:1d:bc:d9:fb:58:44:b8:a0:2a:c4:7c:f4:03:
        17:c2:88:40:6d:ca:39:03:f0:74:51:eb:af:e3:46:
        53:5e:b5:bd:e2:80:d3:fc:4d:85:3b:c5:93:c2:15:
        06:ab:23:91:d3:b9:8f:61:05:7d:33:1f:27:e4:20:
        e2:66:82:7c:86:4d:fb:19:27:a2:c5:d1:50:5d:ef:
        cf:90:94:cf:fa:d7:c0:1b:a9:3a:53:a2:58:52:5b:
        07:a7:15:fc:97:9e:d3:8e:43:8e:21:e3:72:28:a9:
        fd:95:5d:f0:0d:b1:9d:41:20:e2:92:05:52:e8:c0:
        14:f1:fe:db:6b:79:4e:f9
    prime1:
        00:fb:dd:b4:6a:09:01:7d:b1:62:00:b4:17:8d:47:
        5d:0c:ca:0c:79:c2:bc:0a:be:38:02:bd:a6:b0:b2:
        0c:7c:da:4b:80:6f:70:96:47:41:bc:d4:67:d4:fa:
        52:b4:61:a1:dd:99:c1:47:b0:01:e5:0e:77:0c:3a:
        d6:3a:20:f9:cd
    prime2:
        00:de:b2:8f:eb:ef:e3:52:8c:a5:b0:93:d6:b8:df:
        4b:ca:e2:a3:b4:09:6c:36:d6:d6:69:8b:ec:f2:20:
        8d:84:bc:95:f6:1a:16:8e:fa:30:00:06:41:d6:e5:
        b2:e5:a2:c3:b5:02:37:bf:8f:1b:78:4c:70:b0:91:
        05:02:54:2d:17
    exponent1:
        49:39:0a:53:6d:6c:d6:8a:f4:4f:fa:0b:82:d2:45:
        76:0b:63:97:1e:8b:26:a0:68:8e:3b:6e:b3:9d:ff:
        cd:68:75:5c:d0:89:2d:d7:67:80:2b:4c:48:79:c2:
        03:99:02:c2:f7:09:5e:fb:07:81:fd:33:95:05:a1:
        f4:62:53:dd
    exponent2:
        63:4f:aa:13:03:75:92:98:9a:43:36:8a:a9:03:1b:
        5f:ac:d6:68:31:53:8a:d9:1f:e1:0d:bf:6a:f2:cd:
        69:86:58:31:d9:cc:79:92:64:d9:7d:f0:da:8e:b0:
        ca:eb:5c:ef:59:bf:d6:b3:51:02:c2:c3:f6:59:95:
        ac:46:31:9d
    coefficient:
        10:01:db:d2:5b:11:81:67:bf:b1:75:f6:6a:db:60:
        e0:d6:c3:b0:41:31:a8:36:7e:ec:61:ac:ca:fa:99:
        9f:55:e0:4b:f5:0f:76:de:ab:56:45:50:dc:de:d9:
        10:f2:95:e9:2c:70:37:d2:f2:48:8e:25:55:69:85:
        5a:10:a7:eb
    -----BEGIN RSA PRIVATE KEY-----
    MIICWwIBAAKBgQDbGfA0XdkIpK5h2O9mg5o35pitxwiHDnlpBTCTUH+pkGMdDe6d
    9nVQDr61QUEMWAgbnb/irTXh5VigGhHDbG/4kmVy1BgSfLxUx50jmm7jnvnS4Hrb
    65g920x26gaBW+I9n9cHF/QShrqaNXP9DDeqhqNzdmrkaaAQQkQ9liN6awIDAQAB
    AoGAU0gdvNn7WES4oCrEfPQDF8KIQG3KOQPwdFHrr+NGU161veKA0/xNhTvFk8IV
    BqsjkdO5j2EFfTMfJ+Qg4maCfIZN+xknosXRUF3vz5CUz/rXwBupOlOiWFJbB6cV
    /Jee045DjiHjciip/ZVd8A2xnUEg4pIFUujAFPH+22t5TvkCQQD73bRqCQF9sWIA
    tBeNR10Mygx5wrwKvjgCvaawsgx82kuAb3CWR0G81GfU+lK0YaHdmcFHsAHlDncM
    OtY6IPnNAkEA3rKP6+/jUoylsJPWuN9LyuKjtAlsNtbWaYvs8iCNhLyV9hoWjvow
    AAZB1uWy5aLDtQI3v48beExwsJEFAlQtFwJASTkKU21s1or0T/oLgtJFdgtjlx6L
    JqBojjtus53/zWh1XNCJLddngCtMSHnCA5kCwvcJXvsHgf0zlQWh9GJT3QJAY0+q
    EwN1kpiaQzaKqQMbX6zWaDFTitkf4Q2/avLNaYZYMdnMeZJk2X3w2o6wyutc71m/
    1rNRAsLD9lmVrEYxnQJAEAHb0lsRgWe/sXX2attg4NbDsEExqDZ+7GGsyvqZn1Xg
    S/UPdt6rVkVQ3N7ZEPKV6SxwN9LySI4lVWmFWhCn6w==
    -----END RSA PRIVATE KEY-----
    
    

    通过公钥加密数据,私钥解密数据

    //生成明文文件
    hank$ vi message.txt
    //查看文件内容
    hank$ cat message.txt
    密码:123456
    //通过公钥进行加密
    hank$ openssl rsautl -encrypt -in message.txt -inkey public.pem -pubin -out enc.txt
    //通过私钥进行解密
    hank$ openssl rsautl -decrypt -in enc.txt -inkey private.pem -out dec.txt
    
    

    通过私钥加密数据,公钥解密数据

    //通过私钥进行加密
    hank$ openssl rsautl -sign -in message.txt -inkey private.pem -out enc.txt
    //通过公钥进行解密
    hank$ openssl rsautl -verify -in enc.txt -inkey public.pem -pubin -out dec.txt
    
    

    相关文章

      网友评论

          本文标题:浅谈RSA

          本文链接:https://www.haomeiwen.com/subject/hhexxqtx.html