RSA加解密
为了避免因明文传输带来的不完全问题,所有敏感信息均需加密处理(为了兼容旧版本,配置文件开放了rsa节点用来配置是否启用rsa加解密),要查看明文则需要解密处理,涉及RTSP地址(包含密码)、海康安防综合管理平台的appKey和appSecret等。这里,我们统一采用 RSA
加解密算法。如果您使用 js/ts
语言开发,推荐使用 jsencrypt
库。注意:jsencrypt
库存在问题,如果加密文本过长可能会出现“Message too long for RSA”
错误,解决方法参考【这里】。
密钥
密钥使用如下固定密钥,使用公钥加密,使用私钥解密。
公钥:
txt
-----BEGIN PUBLIC KEY-----
MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgG2CKPoayirsqic3eooiV8hm7Q/s
G4QHk1+la7fExuqvb4KK9JFb4YrkKURpZ6zZUUGlV+lKX3cDaW6qTCpQ7WCw7+Nt
FS1HTa3Jx32U0Pk4/UBwx1bvppeWnMtoN8FA5N4n+ofs+mSrHQu4qMsKEVNxqYlR
wycB/jxkmF+oDTDRAgMBAAE=
-----END PUBLIC KEY-----
私钥:
txt
-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgG2CKPoayirsqic3eooiV8hm7Q/sG4QHk1+la7fExuqvb4KK9JFb
4YrkKURpZ6zZUUGlV+lKX3cDaW6qTCpQ7WCw7+NtFS1HTa3Jx32U0Pk4/UBwx1bv
ppeWnMtoN8FA5N4n+ofs+mSrHQu4qMsKEVNxqYlRwycB/jxkmF+oDTDRAgMBAAEC
gYAMR506tohXGeTWsvTtyn4Xfds1ft9zBbpz7kPO/qTjqjqR4+0a47e98GnkPwGe
M1xWRSk/gtSDrwFCpJwX3q1PlSBg9IZnkKWcU+pnkwaVajeTr2mViBDk6ORtCHk8
4Y7YiMyhxT301UHkfZ2BUt6an7N3pM5UEzuZnBdENQXaAQJBAMvNeAePLvaqBmiq
nRqF1I0dem7MAdIYiHfExExnUDpHQAgqQU7o0j4BmmAj1GTf4g6T7INrxkH8gviR
6SfXBfECQQCJjjH1Ilz1gXJyeEQXAlu3lC+Ft5S8WfJ+ayMehZqx+hm65EkzGdM+
/uMZHtSQXVFCRFlAh8R2TtGkkPSFyHjhAkAAlBK1IsjlVlIyVdmgS6TwyZahXd7b
c8pfXKWZ+ekWYUFjRItu5+ZA2sxPXYqqxp/jnrPbI6EqascMYY2h2VBhAkA152Uo
XMj05D9HIF3+bkwmyok860376Xufkrh2c4DFke6XUHJm9g3UN+oio/REhm0bfNFE
+m9AtW5uAOfxrTfBAkAx/2KoEicPt9Rkt49Yol51onw8NCiHayeTty5iFqxBMIBQ
NxwTtWeRM0Ulz7HjBnB1EI40KIgWPuO4xCpazCDH
-----END RSA PRIVATE KEY-----
js版加解密算法
以下代码仅供参考,未处理“Message too long for RSA”
问题,解决方法参考【这里】。
js
import JSEncrypt from 'jsencrypt';
const publicKey = `-----BEGIN PUBLIC KEY-----
MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgG2CKPoayirsqic3eooiV8hm7Q/s
G4QHk1+la7fExuqvb4KK9JFb4YrkKURpZ6zZUUGlV+lKX3cDaW6qTCpQ7WCw7+Nt
FS1HTa3Jx32U0Pk4/UBwx1bvppeWnMtoN8FA5N4n+ofs+mSrHQu4qMsKEVNxqYlR
wycB/jxkmF+oDTDRAgMBAAE=
-----END PUBLIC KEY-----`;
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgG2CKPoayirsqic3eooiV8hm7Q/sG4QHk1+la7fExuqvb4KK9JFb
4YrkKURpZ6zZUUGlV+lKX3cDaW6qTCpQ7WCw7+NtFS1HTa3Jx32U0Pk4/UBwx1bv
ppeWnMtoN8FA5N4n+ofs+mSrHQu4qMsKEVNxqYlRwycB/jxkmF+oDTDRAgMBAAEC
gYAMR506tohXGeTWsvTtyn4Xfds1ft9zBbpz7kPO/qTjqjqR4+0a47e98GnkPwGe
M1xWRSk/gtSDrwFCpJwX3q1PlSBg9IZnkKWcU+pnkwaVajeTr2mViBDk6ORtCHk8
4Y7YiMyhxT301UHkfZ2BUt6an7N3pM5UEzuZnBdENQXaAQJBAMvNeAePLvaqBmiq
nRqF1I0dem7MAdIYiHfExExnUDpHQAgqQU7o0j4BmmAj1GTf4g6T7INrxkH8gviR
6SfXBfECQQCJjjH1Ilz1gXJyeEQXAlu3lC+Ft5S8WfJ+ayMehZqx+hm65EkzGdM+
/uMZHtSQXVFCRFlAh8R2TtGkkPSFyHjhAkAAlBK1IsjlVlIyVdmgS6TwyZahXd7b
c8pfXKWZ+ekWYUFjRItu5+ZA2sxPXYqqxp/jnrPbI6EqascMYY2h2VBhAkA152Uo
XMj05D9HIF3+bkwmyok860376Xufkrh2c4DFke6XUHJm9g3UN+oio/REhm0bfNFE
+m9AtW5uAOfxrTfBAkAx/2KoEicPt9Rkt49Yol51onw8NCiHayeTty5iFqxBMIBQ
NxwTtWeRM0Ulz7HjBnB1EI40KIgWPuO4xCpazCDH
-----END RSA PRIVATE KEY-----`;
/**
* RSA加密
* @param {string} text 明文
*/
export function encode(text) {
if (!text) return '';
let rsa = new JSEncrypt();
rsa.setPublicKey(publicKey);
return rsa.encrypt(text);
}
/**
* RSA解密
* @param {string} text 密文
*/
export function decode(text) {
if (!text) return '';
let rsa = new JSEncrypt();
rsa.setPrivateKey(privateKey);
return rsa.decrypt(text);
}