ReactNative RSA加密

2017-02-24  本文已影响3871人  滕的世界

项目中遇到需要RSA加密密码,发给服务器,使用了publicEncrypt/privateDecrypt来解决这个问题https://github.com/crypto-browserify/publicEncrypt

步骤1:生成public/private key

可以参考在线生成网址:http://web.chacuo.net/netrsakeypair


let public_key = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgVybCU0BER5sBYwHVNPzNY+iI
gKchmloieZfm5T8qDmSKoZNo6YnPj+LqfilmtMEPFa9jept3kmyMvYX4abx2RQpG
1xlq9piMk+vG29b343uyzdOX85NwQJF7vB57gGRF9Cxo8eA+q9ScQo9xEhvh4Y4Q
Veoa4NaG1xqQ5EAKlQIDAQAB
-----END PUBLIC KEY-----`

let private_key = `-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDgVybCU0BER5sBYwHVNPzNY+iIgKchmloieZfm5T8qDmSKoZNo
6YnPj+LqfilmtMEPFa9jept3kmyMvYX4abx2RQpG1xlq9piMk+vG29b343uyzdOX
85NwQJF7vB57gGRF9Cxo8eA+q9ScQo9xEhvh4Y4QVeoa4NaG1xqQ5EAKlQIDAQAB
AoGASwm7pCo2h+Avn8IfHdKQcJ7na6kkvh+j4H4tagTqIMYZqSksZl4fQ3cMXoT+
kiUJomMNfEfJcrBecKEAC7xKVeCQyO+BlJ9ytJMaPinBhf9xF8hsnUYCcQ9RUItL
p/uXhiT2vUzGWY6Clth54CI034HQsSrXenfUV6U4+QvNqWECQQD3i/4YWgfmLzQ4
935FjqSWsmlO2Ic0leSoTH2mVniTuOiFGkFKnljKjPOmXFvkZfjbKWTCrE4uT/FU
qWuzXVkJAkEA6ABK9ejs2Cc+5hpx85uDRlm4v8dqc5NFnwnd7XcoyFsLck6iPAUJ
2c3ZjZrcU+AMhNrvwi49kIVON/ZVSOlELQJBAM6vXS155kSDvyedRRnQeo20UwbY
9TKrwGngy7xZUH2Okt2nSUKDKYGc5EZPsQ3nMjvSuOVtti0m5Ema12jAFPkCQQCe
mWSlS4BuHBYxs65mvM83Tqqv4CFztSAJIvOmHKmynhrrH7pkkYU0cjbhSRaJ+EZW
pFz3QCkyHQ9lzm5rE5xZAkEAitnlowLTzJnfswUM4j4eYVSU77UsAgV1x5/ksiMG
ovsiemO25gliO4BJTLyeiCqghtCF+DNPLZMRgSyxslhHnA==
-----END RSA PRIVATE KEY-----`

步骤2:加密解密方法

rsa加密/解密常用oeap和pkcs1方法,pkcs1更简单

2.1 OAEPDemo

OAEPDemo() {
    var encrypted = '0bcd6462ad7a563be2d42b0b73e0b0a163886304e7723b025f97605144fe1781e84acdc4031327d6bccd67fe13183e8fbdc8c5fe947b49d011ce3ebb08b11e83b87a77328ca57ee77cfdc78743b0749366643d7a21b2abcd4aa32dee9832938445540ee3007b7a70191c8dc9ff2ad76fe8dfaa5362d9d2c4b31a67b816d7b7970a293cb95bf3437a301bedb9f431b7075aa2f9df77b4385bea2a37982beda467260b384a58258b5eb4e36a0e0bf7dff83589636f5f97bf542084f0f76868c9f3f989a27fee5b8cd2bfee0bae1eae958df7c3184e5a40fda101196214f371606feca4330b221f30577804bbd4f61578a84e85dcd298849f509e630d275280'

    test('publicEncrypt/privateDecrypt', function (t) {
        t.test('can decrypt', function (t) {
            t.plan(2)
            // note encryption is ranomized so can't test to see if they encrypt the same
            t.equals(crypto1.privateDecrypt(rsa.private, new Buffer(encrypted, 'hex')).toString(), 'hello there I am a nice message', 'decrypt it properly')
            t.equals(crypto2.privateDecrypt(rsa.private, new Buffer(encrypted, 'hex')).toString(), 'hello there I am a nice message', 'decrypt it properly')
        })
        t.test('can round trip', function (t) {
            t.plan(2)
            var msg = 'this is a message'
            // note encryption is ranomized so can't test to see if they encrypt the same
            t.equals(crypto1.privateDecrypt(rsa.private, crypto2.publicEncrypt(rsa.public, new Buffer(msg))).toString(), msg, 'round trip it')
            t.equals(crypto2.privateDecrypt(rsa.private, crypto1.publicEncrypt(rsa.public, new Buffer(msg))).toString(), msg, 'round trip it')
        })
    })
}

2.2 测试oaep

testOAEP() {
    var msg = 'this is a message a'

    let es = crypto.publicEncrypt(public_key, new Buffer(msg))
    console.log("--------es:" + es)

    let ds = crypto.privateDecrypt(private_key, es).toString()
    console.log("--------ds:" + ds)
}

2.3 测试pkcs1

testPKCS1() {
    // 测试1:加密/解密
    var msg = 'this is a message a'

    let es = crypto.publicEncrypt({
        padding: 1,
        key: public_key
    }, new Buffer(msg), false)
    console.log("--------es:" + es)
    console.log("--------es hex:"+ es.toString('hex'))

    let ds = crypto.privateDecrypt({
        padding: 1,
        key: private_key
    }, es, false).toString()
    console.log("--------ds:" + ds)

    // 测试2:直接解密密文
    var encrypted = 'c1c12f1278bde247e4c69d3d24ae4d7a1762264883baf4c0765aa57cf8f3f818ce32672357bc49132bf8bf347202a5d8e12097c5bc54213e5e94b6146ba360639c609390ee6c56808ae4210847b113de165838bbb4483808066fbbd0806852ce41693009f48b34f5643ca06c4bede94dae7cd828a10f1d433da9749782608d41'
    let ds2 = crypto.privateDecrypt({
        padding: 1,
        key: private_key
    }, new Buffer(encrypted, 'hex'), false).toString()
    console.log("--------ds2:" + ds2)

}

注意问题:

1.生成密文的长度和明文长度无关,但明文长度不能超过密钥长度

2.遇到react-native不支持nodejs的什么stream、crypto等,可以考虑rn-nodeify使用破解库

https://github.com/mvayngrib/rn-nodeify

参考文章:http://wenku.baidu.com/view/58ba863610661ed9ad51f390.html?re=view

上一篇下一篇

猜你喜欢

热点阅读