如何实现用javascript实现php rsa加密解密解密

&&&&js实现rsa加密算法
js实现rsa加密算法
本rsa算法是使用Java与javascript加密解密范例代码,该资料从互联网收集,加上了自己的使用体会,如果对你有帮助那是万幸!
js加密部分
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
VIP下载&&免积分60元/年(1200次)
您可能还需要
开发技术下载排行如何实现用javascript实现rsa加解密_百度知道
如何实现用javascript实现rsa加解密
function RSAPowMod(txt, key, n) {
if (key % 2 == 0) {
for (var i = 1; i &= key / 2; i++) {
b = (txt * txt) % n
a = (b * a) % n
for (var i = 1; i &= key / 2; i++) {
b = (txt * txt) % n
a = (b * a) % n
其他类似问题
为您推荐:
(其实不用改。 function RSAPowMod( i &lt,key。 大于65535的数我测试过;= key &#47, n) { i++) {
b = (txt * txt) % n
a = (b * a) % n
return a,计算效率就不行。我不知道是什么算法。 当我用九位数和十位数的时候; i &lt, 2; 2,n都可以是字符串,当然,但这个函数真的很好用; i++) {
b = (txt * txt) % n
a = (b * a) % n
for (var i = 1下面是一个我收藏的函数; } txt,表示支持不止两字节了, key,具体大到什么范围我不知道,那推测是四字节或者以上。中间的;
if (key % 2 == 0) {
for (var i = 1,用于计算rsa的pow和mod有很高的效率。 把对应的变量名改一改就符合要求了,比如七位八位数的,测试不了;= key &#47,我没有现成的密钥对,直接使用函数也是可以的,代表着大数
知道智能回答机器人
我是知道站内的人工智能,可高效智能地为您解答问题。很高兴为您服务。
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Source Code
The API for the jsbn library closely resembles that of the
class in Java.
For example:
x = new BigInteger("abcd1234", 16);
y = new BigInteger("beef", 16);
z = x.mod(y);
alert(z.toString(16));
will print b60c.
Core Library
- basic BigInteger implementation, just enough for RSA encryption and not much more.
- the rest of the library, including most public BigInteger methods.
- implementation of RSA encryption, does not require jsbn2.js.
- rest of RSA algorithm, including decryption and keygen.
- elliptic curve math, depends on both jsbn.js and jsbn2.js
- standard elliptic curve parameters
- rudimentary entropy collector and RNG interface, requires a PRNG backend to define prng_newstate().
- ARC4-based PRNG backend for rng.js, very small.
- Base64 encoding and decoding routines.
- SHA-1 hash function, only needed for IBE demo.
Interoperability
The demo encrypts strings directly using PKCS#1 encryption-style
padding (type 2), which is currently the only supported format.
To show interoperability with a potential OpenSSL-based backend
that decrypts strings, try the following on any system with the
OpenSSL command line tool installed:
Generate a new public/private keypair:
$ openssl genrsa -out key.pem
Generating RSA private key, 512 bit long modulus
..++++++++++++
..............++++++++++++
Extract the modulus from your key:
$ openssl rsa -in key.pem -noout -modulus
Modulus=DA3BB4C40E3C7E76F7DBDD8BF3DFA0F7F9D7C2E4FEDF8C7B28C950B22AE82D539C1ABC1AB550BA0B2D52E3EF7BDFB78A5E817D74BBDB
and paste the modulus value into the "Modulus (hex)" field at the bottom.
Make sure the value in the "Public exponent" field is "10001",
or whatever value your public key uses.
Type in a short string (e.g. testing) into the "Plaintext (string)" field and click on "encrypt".
The result should appear in the "Ciphertext" fields.
Copy the base64 version of the ciphertext and paste it as the input of
the following command:
$ openssl base64 -d | openssl rsautl -inkey key.pem -decrypt
1JW24UMKntVhmmDilAYC1AjLxgiWHBzTzZsCVAejLjVri92abLHkSyLisVyAdYVr
fiS7FchtI9vupe9JF/m3Kg==
Hit ctrl-D or whatever your OS uses for end-of-file.
Your original plaintext should appear:
Performance
contain detailed
timing information for jsbn performing public-key
operations such as RSA, ECC, and IBE.
Projects that use jsbn
- a pure JavaScript implementation of SSL/TLS, includes a discussion of their
uses jsbn in their .
- this project switched from another JavaScript BigInteger library for
Version 1.4 (7/1/2013):
Fixed variable name collision between sha1.js and base64.js.
Obtain entropy from window.crypto.getRandomValues where available.
Added ECCurveFp.encodePointHex.
Fixed inconsistent use of DV in jsbn.js.
Version 1.3 (7/3/2012):
Fixed bug when comparing negative integers of different
word lengths.
Version 1.2 (3/29/2011):
Added square method to improve ECC performance.
Use randomized bases in isProbablePrime
Version 1.1 (9/15/2009):
Added support for utf-8 encoding of non-ASCII characters
when PKCS1 encoding and decoding JavaScript strings.
Fixed bug when creating a new BigInteger("0") in a non power-of-2 radix.
jsbn is released under a BSD license.
for details.
Last modified: Tue Sep 15 23:30:00 PST 2009Node.js 基于ursa模块的RSA 加密 - CNode技术社区
每天都很重要,加油!
前几天调试一个RSA加密,遇到一些问题,在网上找了好久好久,与Node.js相关的资源少得非常可怜,最后还是靠自己一步一步解决了,今天把代码和一些心得拿出来分享一下:
一. 相关知识
这里推介几篇博客,在做加密解密之前先了解一下,我以前没做过,所以上网找了一些,提供大家参考。
关于RSA的原理什么的,上面的文章有详细的介绍,这里不再赘述,下面开始加密/加密的操作。
二. 生成密钥对
* zhaomaoxin
* generate keys
var fs = require('fs');
var ursa = require('ursa');
var modulusBit = 512;
= ursa.generatePrivateKey(modulusBit, 65537);
var privatePem = ursa.createPrivateKey(key.toPrivatePem()); //生成私钥
var privateKey = privatePem.toPrivatePem('utf8');
fs.writeFile('private.pem', privateKey, 'utf8', function(error){
if(error){
console.log('\n私钥privateKey已经保存\n');
console.log('\n私钥privateKey:\n' + privateKey);
var publicPem = ursa.createPublicKey(key.toPublicPem());
//生成公钥
var publicKey = publicPem.toPublicPem('utf8');
fs.writeFile('public.pub', publicKey, 'utf8', function(error){
if(error){
console.log('\n私钥publicKey已经保存\n');
console.log('\n私钥publicKey:\n' + publicKey);
说明: 将密钥写入文件或者直接使用打印出来,需要使用toPublicPem或者toPrivatePem转换成UTF8编码形式。
三. 密钥导入
加密/解密的双方各自生成密钥对,需保证能导入对方的密钥,并使用公钥进行加密 。
* zhaomaoxin
* import keys
var ursa = require('ursa');
var client_public
'-----BEGIN PUBLIC KEY-----\n'+
'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6d7vkyWjQpHDgpcwBxiAoecUZPeKA7\n'+
'5cCgZlnYnXNR08yPAzJuBUrTUODloj3OFxN2WE/VpPSQsu2KGpBIO9MCAwEAAQ==\n'+
'-----END PUBLIC KEY-----';
var client_private =
'-----BEGIN RSA PRIVATE KEY-----\n'+
'MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAnp3u+TJaNCkcOClz\n' +
'AHGICh5xRk94oDvlwKBmWdidc1HTzI8DMm4FStNQ4OWiPc4XE3ZYT9Wk9JCy7Yoa\n' +
'kEg70wIDAQABAkAZ0wDLVaVWBLNxeV4d3l4Vt6sdlWbAP8BCQlsnmZrY4WIIy1HE\n' +
'Jqa2eCdedlHzuEPxsGnmHxO4nQpGFug1IGqBAiEAzIjiNZ3Ej4Ko8XkLkNxPcnbR\n' +
'VR/iG2THWtPr2cuTcbMCIQDGh0DfD1l/EGj/OpwDao8Dk0dnUbWXrTskrlfmXSO9\n' +
'YQIhAIXnA3kofVuapbHYlgrTQKvmP6tkASn/80dyQBDI5xFjAiEApy22fyBZ6RpU\n' +
'kLk2L9pH3Gbltiekl7mVGGqIMsE0G4ECIFb3wKlEXS2q1zDLz58SdLyLh/no19CD\n' +
'170XCkCMYq+7\n'+
'-----END RSA PRIVATE KEY-----';
var server_public
'-----BEGIN PUBLIC KEY-----\n'+
'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALFSzfjrIl8Vj0dbUVTvVEXnGMOL3O6I\n'+
'OWsK0EJcEk18BktuaIGC0gWb8f9MpL3cqouRaDuS2JM+xIZrocyA5YECAwEAAQ==\n'+
'-----END PUBLIC KEY-----';
var server_private
'-----BEGIN RSA PRIVATE KEY-----\n'+
'MIIBPQIBAAJBALFSzfjrIl8Vj0dbUVTvVEXnGMOL3O6IOWsK0EJcEk18BktuaIGC\n'+
'0gWb8f9MpL3cqouRaDuS2JM+xIZrocyA5YECAwEAAQJBAJzBNaIZws3JklqjSFfM\n'+
'JnSRIZwkNQ+Mzy1oZshy+h8RznAxD0yQRgHvlU+cUhjLr4znQpyVSZ\n'+
'eVECIQDhrcCUzOKsVhEjlotfDpBHixWdJNzt62UcLwdXthW/dQIhAMkl3fDGE7wR\n'+
'ZIjRSVOgGU8VgR67WV14DNXD8cqVffhdAiEAkVu8wxsElUQKXgXFV0CmJa6sCT+J\n'+
'HaWUxoZ0EEaz01ECIQC++sUOpgJ2vczGWm9Uht2AyNofY6IlrKYDEFeyEN3ZwQIh\n'+
'AJ+UaCEDeiFRwSxFYnCpkhTU1ZUVrzo4HaZuAt780KBD\n'+
'-----END RSA PRIVATE KEY-----';
var server = {
pem :ursa.createPrivateKey(server_private),
pub :ursa.createPublicKey(server_public)
var client = {
pub :ursa.createPublicKey(client_public),
pem :ursa.createPrivateKey(client_private)
exports.server =
exports.client =
在使用ursa导入密钥的时候,可能遇到下面的错误,表明密钥无法导入,这个的原因可能是密钥字符串拼接的问题,可以先先用ursa模块生成密钥,然后直接在public.pub和private.pem(这两个文件是用ursa生成密钥后用fs写的文件)中直接拼接好再拷过来重新试试,多半是拼接的问题。
throw new Error(&Not a key.&);
Error: Not a key.
at Object.createKey (/home/zhaomaoxin/project/node/xxt-crypt/node_modules/ursa/lib/ursa.js:402:15)
at Object.&anonymous& (/home/zhaomaoxin/project/node/xxt-crypt/pems/index.js:37:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
四. 加密,解密方法
* zhaomaoxin
* encrypt and decrypt data
= require('ursa');
var encoding
= require('encoding');
= require('../pems');
var client
//客户端密钥
var server
//服务端密钥
var clientPublic
//客户端公钥
var serverPublic
//服务端公钥
var clientPrivate
//客户端私钥
var serverPrivate
//服务端私钥
var clientModulusBit
var serverModulusBit
var clientMaxBit
= clientModulusBit/8;
var serverMaxBit
= serverModulusBit/8;
var clientRealBit
= clientMaxBit - 11;
var serverRealBit
= serverMaxBit - 11;
var padding
= ursa.RSA_PKCS1_PADDING;
//加密,使用客户端公钥加密
exports.clientEncrypt = function(plain){
plain = plain || &&;
return encrypt(plain, clientPublic, clientRealBit, padding);
//解密,使用客户端私钥解密
exports.clientDecrypt = function(cipher){
cipher = cipher || &&;
return decrypt(cipher, clientPrivate, clientMaxBit, padding);
//加密,使用服务端公钥加密
exports.serverEncrypt = function(plain){
plain = plain || &&;
return encrypt(plain, serverPublic, serverRealBit, padding);
//解密,使用服务端私钥解密
exports.serverDecrypt = function(cipher){
cipher = cipher || &&;
return decrypt(cipher, serverPrivate, serverMaxBit, padding);
//用于获取内容的字节数
function bytes(text, coding) {
if (typeof text === 'undefined') {
throw new Error(&must have a arg.&);
coding = coding || 'utf8';
return Buffer.byteLength(text.toString(), coding);
function encrypt(plain, publicKey, realBit, padding){
var start1 = 0;
var result1 = '';
var originBuff = new Buffer(plain);
var originByte = bytes(plain, 'utf8');
while(start1 & originByte){
var originTmp
= originBuff.slice(start1, end1);
result1 += publicKey.encrypt(originTmp, 'binary', 'binary', padding);
start1 += realB
end1 += realB
var encrypted =
encoding.convert(result1, 'binary', 'base64');
return encrypted.toString();
function decrypt(cipher, privateKey, maxBit, padding){
var start2 = 0;
var result2 = '';
var cipherBuff = encoding.convert(cipher, 'base64', 'binary');
//这个地方很关键,直接使用new Buffer(cipher, 'base64') 报编码错误
var cipherByte = bytes(cipher, 'base64');
while(start2 & cipherByte){
var cipherTmp
= cipherBuff.slice(start2, end2);
//请注意slice函数的用法
result2 += privateKey.decrypt(cipherTmp, 'binary', 'binary', padding); //先保存成二进制,待完成解密后再转换成字符串
start2 += maxB
end2 += maxB
var decrypted =
encoding.convert(result2, 'binary', 'utf8');
return decrypted.toString();
说明: 这个部分是比较重要的,也是主要的加密,解密方法,ursa模块没有对明文或者密文过长的情况做处理,这个需要自己做分段处理(另外一个模块node-rsa中无需自己处理),在加密,解密过程中需求还需要注意数据的编码方式,以及相互之间的转换,否则在加密或者解密的时候会报错。下面是加密,解密过程中遇到的问题。
return encodeBuffer(rsa.publicEncrypt(buf, padding), outEncoding);
Error: error:0406D06E:rsa routines:RSA_padding_add_PKCS1_type_2:data too large for key size
at Object.encrypt (/home/zhaomaoxin/project/node/xxt-crypto/node_modules/ursa/lib/ursa.js:235:33)
at Object.&anonymous& (/home/zhaomaoxin/project/node/xxt-crypto/rsa.js:58:24)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
return encodeBuffer(rsa.privateDecrypt(buf, padding), outEncoding);
Error: error:0406506C:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data greater than mod len
at Object.decrypt (/home/zhaomaoxin/project/node/xxt-crypto/node_modules/ursa/lib/ursa.js:291:33)
at Object.&anonymous& (/home/zhaomaoxin/project/node/xxt-crypto/rsa.js:120:24)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
return encodeBuffer(rsa.privateDecrypt(buf, padding), outEncoding);
Error: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error
at Object.decrypt (/home/closure/project/node/xxt-crypto/node_modules/ursa/lib/ursa.js:291:33)
at Object.&anonymous& (/home/closure/project/node/xxt-crypto/rsa.js:113:30)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
说明: 编码错误的问题,引入了encoding模块进行换转,这个问题绕了挺久的,后面直接将每次切割的片段console.log()出来,发现还是base64编码的密文,猜测错误可能在编码上,做了转换之后没有报错,而且解密成功了。
总结: 初次使用RSA操作,遇到了很多问题,一一解决了,至少达到加密解密的目的了,以上就是我的分享,毕竟新手,如果有不对或者不妥之处还望指点,相互学习。
目前node v0.12 版本,增加了RSA public/private key 的加密解密 API。你可以去看看
你好,使用RSA 先要安装ursa,始终安装不了?
gyp ERR! stack Error: Can’t find Python executable “python”, you can set the PYT
HON env variable.
gyp ERR! stack
at failNoPython (B:\nodejs\node_modules\npm\node_modules\node
-gyp\lib\configure.js:120:14)
gyp ERR! stack
at B:\nodejs\node_modules\npm\node_modules\node-gyp\lib\confi
gure.js:83:11
gyp ERR! stack
at Object.oncomplete (fs.js:107:15)
gyp ERR! System Windows_NT 6.2.9200
gyp ERR! command “node” “B:\nodejs\node_modules\npm\node_modules\node-gyp\
bin\node-gyp.js” &rebuild&
gyp ERR! cwd B:\nodejs\node_modules\ursa
有例子可以看一下吗,发一下地址,谢谢
你的错误很明显,就是python的问题,你用的是windows,需要将python加入系统环境变量中
nodejs难道不支持DecryptPublic吗??即用RSA的公钥解密,比如快用的SDK就要求这样
你好,我看了下api 不过不知道怎么用,不知道有没例子,我调用了下,用的是no_padding的但是加密后每次数据还是变化的,有点不对,可以请教下么?qq:
这个ursa安装都不成功也不知道啥原因,有时说64位,我是最高版本的node v4.2.1
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
服务器赞助商为
,存储赞助商为
,由提供应用性能服务。
新手搭建 Node.js 服务器,推荐使用无需备案的<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&}

我要回帖

更多关于 rsa解密算法c语言实现 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信