當前位置:首頁 » 網頁前端 » rsa小程序前端加密
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

rsa小程序前端加密

發布時間: 2022-07-03 02:36:53

1. 前端rsa 加密頁面卡死怎麼解決

RSA的缺點就是計算速度比較慢,這是硬傷,所以通常加密中並不是直接使用RSA來對所有的信息進行加密,最常見的情況是隨機產生一個對稱加密的密鑰,然後使用對稱加密演算法對信息加密,之後用RSA對剛才的加密密鑰進行加密。你所說的「解決」並不適用於當前的科學技術,只有緩慢地「改進」或者另闢蹊徑。

2. 微信小程序怎麼做 RSA 加密

var input_rsa = this.data.input;
var encrypt_rsa = new RSA.RSAKey();
encrypt_rsa = RSA.KEYUTIL.getKey(publicKey);
encStr = encrypt_rsa.encrypt(input_rsa)
encStr = RSA.hex2b64(encStr);
console.log("加密結果:" + encStr)

資料來自CSDN:http://blog.csdn.net/ufo00001/article/details/72822907

3. 編一個簡單的C語言小程序。。。。。關於RSA演算法的

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//RSA??????
typedef struct RSA_PARAM_Tag
{
unsigned __int64 p, q; //????,?????????
unsigned __int64 f; //f=(p-1)*(q-1),?????????
unsigned __int64 n, e; //??,n=p*q,gcd(e,f)=1
unsigned __int64 d; //??,e*d=1 (mod f),gcd(n,d)=1
unsigned __int64 s; //??,??2^s<=n????s,?log2(n)
} RSA_PARAM;

//????
const static long g_PrimeTable[]=
{
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97
};

const static long g_PrimeCount=sizeof(g_PrimeTable) / sizeof(long);const unsigned __int64 multiplier=12747293821;
const unsigned __int64 adder=1343545677842234541;//????

typedef struct _RandNumber
{
/* */
unsigned __int64 randSeed;/* */
}RandNumber;

static void CreateRandNumber(RandNumber *pRand, unsigned __int64 s);
static unsigned __int64 Random(RandNumber *pRand, unsigned __int64 n);

/* */
void CreateRandNumber(RandNumber *pRand, unsigned __int64 s)
{
if(!s)
{
pRand->randSeed= (unsigned __int64)time(NULL);
}
else
{
pRand->randSeed=s;
}
}

/* */
unsigned __int64 Random(RandNumber *pRand, unsigned __int64 n)
{
pRand->randSeed=multiplier * pRand->randSeed + adder;
return pRand->randSeed % n;
}

static RandNumber g_Rnd;

/*
????,??? x=a*b mod n
*/
unsigned __int64 MulMod(unsigned __int64 a, unsigned __int64 b, unsigned __int64 n)
{
return a * b % n;
}

/*
????,??? x=base^pow mod n
*/
unsigned __int64 PowMod(unsigned __int64 base, unsigned __int64 pow, unsigned __int64 n)
{
unsigned __int64 a=base, b=pow, c=1;
while(b)
{
while(!(b & 1))
{
b>>=1; //a=a * a % n; //?????????64????,?????a*a?a>=2^32????????,??????????64?
a=MulMod(a, a, n);
}

b--; //c=a * c % n; //??????,??64???????32????????????????
c=MulMod(a, c, n);
}
return c;
}

/*
Rabin-Miller????,??????1,????0?
n??????
??:????????????,???????????1/4
*/
long RabinMillerKnl(unsigned __int64 n)
{
unsigned __int64 b, m, j, v, i;
unsigned __int64 TWO = 2;
m=n - 1;
j=0; //0?????m?j,??n-1=m*2^j,??m????,j?????
while(!(m & 1))
{
++j;
m>>=1;
}

//1??????b,2<=b<n-1
b=2 + Random(&g_Rnd, n - 3);

//2???v=b^m mod n
v=PowMod(b, m, n);

//3???v==1,????
if(v == 1)
{
return 1;
}

//4??i=1
i=1;

//5???v=n-1,????
while(v != n - 1)
{
//6???i==l,???,??
if(i == j)
{
return 0;
}

//7?v=v^2 mod n,i=i+1
v=PowMod(v, TWO, n);
++i;

//8????5
}

return 1;
}

/*
Rabin-Miller????,??????loop?
??????1,????0
*/
long RabinMiller(unsigned __int64 n, long loop)
{
long i=0;

//?????????,????
for(i=0; i < g_PrimeCount; i++)
{
if(n % g_PrimeTable[i] == 0)
{
return 0;
}
}

//????Rabin-Miller??loop?,??????????????(1/4)^loop
for(i=0; i < loop; i++)
{
if(!RabinMillerKnl(n))
{
return 0;
}
}

return 1;
}

/*
??????bits?(????)???,??32?
*/
unsigned __int64 RandomPrime(char bits)
{
unsigned __int64 base;
do
{
base= (unsigned long)1 << (bits - 1); //??????1
base+=Random(&g_Rnd, base); //????????
base|=1; //??????1,??????
} while(!RabinMiller(base, 30)); //????-????30?
return base; //?????????
}

/*
???????????
*/
unsigned __int64 EuclidGcd(unsigned __int64 p, unsigned __int64 q)
{
unsigned __int64 a=p > q ? p : q;
unsigned __int64 b=p < q ? p : q;
unsigned __int64 t;
if(p == q)
{
return p; //????,?????????
}
else
{
while(b) //?????,gcd(a,b)=gcd(b,a-qb)
{
a=a % b;
t=a;
a=b;
b=t;
}

return a;
}
}

/*
Stein???????
*/
unsigned __int64 SteinGcd(unsigned __int64 p, unsigned __int64 q)
{
unsigned __int64 a=p > q ? p : q;
unsigned __int64 b=p < q ? p : q;
unsigned __int64 t, r=1;
if(p == q)
{
return p; //????,?????????
}
else
{
while((!(a & 1)) && (!(b & 1)))
{
r<<=1; //a?b?????,gcd(a,b)=2*gcd(a/2,b/2)
a>>=1;
b>>=1;
}

if(!(a & 1))
{
t=a; //??a???,??a,b
a=b;
b=t;
}

do
{
while(!(b & 1))
{
b>>=1; //b???,a????,gcd(b,a)=gcd(b/2,a)
} if(b < a)
{
t=a; //??b??a,??a,b
a=b;
b=t;
} b=(b - a) >> 1; //b?a????,gcd(b,a)=gcd((b-a)/2,a)
} while(b);

return r * a;
}
}

/*
??a?b,?x,??a*x =1 (mod b)
?????a*x-b*y=1??????
*/
unsigned __int64 Euclid(unsigned __int64 a, unsigned __int64 b)
{
unsigned __int64 m, e, i, j, x, y;
long xx, yy;
m=b;
e=a;
x=0;
y=1;
xx=1;
yy=1;
while(e)
{
i=m / e;
j=m % e;
m=e;
e=j;
j=y;
y*=i;
if(xx == yy)
{
if(x > y)
{
y=x - y;
}
else
{
y-=x;
yy=0;
}
}
else
{
y+=x;
xx=1 - xx;
yy=1 - yy;
} x=j;
} if(xx == 0)
{
x=b - x;
} return x;
}

/*
??????RSA????
*/
RSA_PARAM RsaGetParam(RandNumber Rnd)
{
RSA_PARAM Rsa={ 0 };
unsigned __int64 t;
Rsa.p=RandomPrime(16); //????????
Rsa.q=RandomPrime(16);
Rsa.n=Rsa.p * Rsa.q;
Rsa.f=(Rsa.p - 1) * (Rsa.q - 1);
do
{
Rsa.e=Random(&Rnd, 65536); //??2^16,65536=2^16
Rsa.e|=1; //??????1,??????,?f?????,???,?????
} while(SteinGcd(Rsa.e, Rsa.f) != 1);

Rsa.d=Euclid(Rsa.e, Rsa.f);
Rsa.s=0;
t=Rsa.n >> 1;
while(t)
{
Rsa.s++; //s=log2(n)
t>>=1;
}

return Rsa;
}

/*
??-????
*/
void TestRM(void)
{
unsigned long k=0;
unsigned __int64 i = 0;

printf(" - Rabin-Miller prime check.\n\n");

for(i=4197900001; i < 4198000000; i+=2)
{
if(RabinMiller(i, 30))
{
k++;
printf("%ul\n", i);
}
}

printf("Total: %ul\n", k);
}

void Usage(void)
{
printf("0. exit\n");
printf("1. encryption\n");
printf("2. decrypt\n");
printf("enter your choice:");
}

int GetChoice(void)
{
char s[80] = {0};
fgets(s, sizeof(s), stdin);

if ('0' > s[0] || '9' < s[0] || '\n' != s[1])
{
return -1;
}
else
{
return s[0] - '0';
}
}

#define ENCRYPT_CODE 1
#define DECRYPT_CODE 2
#define EXIT_CODE 0

#define MAX_SIZE 1024

int main(void)
{
RSA_PARAM r;
char pSrc[MAX_SIZE]={0};
unsigned long n = 0;
unsigned char *q = NULL;
unsigned char pDec[MAX_SIZE] = {0};
unsigned __int64 pEnc[MAX_SIZE] = {0};
unsigned __int64 tmp = 0;
unsigned long i=0;
int choice = 0;

unsigned __int64 s = 0;
CreateRandNumber(&g_Rnd, s);

r=RsaGetParam(g_Rnd);

/*
printf("p=%ul\n", r.p);
printf("q=%ul\n", r.q);

printf("f=(p-1)*(q-1)=%ul\n", r.f);
printf("n=p*q=%ul\n", r.n);
printf("e=%ul\n", r.e);
printf("d=%ul\n", r.d);
printf("s=%ul\n", r.s);
*/

while (1)
{
memset(pSrc, 0, MAX_SIZE);
memset(pDec, 0, MAX_SIZE);
memset(pEnc, 0, MAX_SIZE);

Usage();

choice = GetChoice();
switch(choice)
{
case ENCRYPT_CODE:
printf("Source:%s", pSrc);
scanf("%s", pSrc);
n = strlen(pSrc);
getchar();

q= (unsigned char *)pSrc;
printf("Encode:");

for(i = 0; i < n; i++)
{
tmp = q[i];
pEnc[i]=PowMod(tmp, r.e, r.n);
printf("%x ", pEnc[i]);
}
printf("\n");
break;

case DECRYPT_CODE:
printf("Enter length of ciphertext:");
scanf("%d", &n);
getchar();

for(i=0; i < n; i++)
{
printf("Enter ciphertext(%d):", i+1);
scanf("%x", &pEnc[i]);
getchar();
}

printf("Decode:");
for(i=0; i < n; i++)
{
pDec[i]=PowMod(pEnc[i], r.d, r.n);
printf("%x ", (unsigned long)pDec[i]);
}
printf("\n");
printf("%s\n", (char *)pDec);
break;

case EXIT_CODE:
break;
default:
break;
}

if (EXIT_CODE == choice)
{
break;
}
else
{
continue;
}
}

return 0;
}

4. RSA有加密演算法源碼 如何寫一個小程序

這種著名加密演算法,都有公開現成的代碼,哪用得著自己來寫?
問題是給了具體的公鑰如何改寫源代碼啊
不好意思,我看錯了。這個給出的是公鑰,要求解私鑰。這相當於要破解RSA加密。但由於公鑰的數字極小,所以破解很簡單,不用計算機用筆算都可以很快完成。77=11×7,知道了這個,照著演算法來,其它都不是問題了。

5. JAVA 前端用RSA.js加密 傳到後端解密有亂碼

因為Java默認編碼是unicode,byte[]btInput=s.getBytes();獲得的是默認的unicode的byte數組。需要將這句改為byte[]btInput=s.getBytes("utf-8");就OK啦。參考網址:75637

6. 怎樣可以把微信的小程序加密

1、下載MD5源文件(JS);
2、在小程序模塊中使用require引入外部模塊;也可以在index.html中直接全局引入md5.js文件。
因為源md5.js中沒有隊模塊因為輸出,如果使用require需要export,所以在md5.js中需要加入以下代碼:

mole.exports = {
hexMD5: hex_md5, //需要輸出的加密演算法,我這邊只寫了我需要得兩種
b64Md5: b64_md5,
}

在js文件中使用require引入md5:

const md5 = require('../../assets/js/md5/md5.js');

使用:

let b64 = md5.b64Md5(code); //code需要加密的數據

下面是我的文件結構:

md5.js 代碼如下;

/*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/

/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase =
0;
/* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad =
"";
/* base-64 pad character. "=" for strict RFC compliance */
var chrsz =
8;
/* bits per input character. 8 - ASCII; 16 - Unicode */

/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_md5(s){
return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){
return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){
return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) {
return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) {
return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) {
return binl2str(core_hmac_md5(key, data)); }

/*
* Perform a simple self-test to see if the VM is working
*/
function md5_vm_test()
{
return hex_md5( "abc") ==
"";
}

/*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len)
{
/* append padding */
x[len >>
5] |=
0x80 << ((len) %
32);
x[(((len +
64) >>>
9) <<
4) +
14] = len;

var a =
1732584193;
var b = - 271733879;
var c = - 1732584194;
var d =
271733878;

for( var i =
0; i < x.length; i +=
16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;

a = md5_ff(a, b, c, d, x[i+
0],
7 , - 680876936);
d = md5_ff(d, a, b, c, x[i+
1],
12, - 389564586);
c = md5_ff(c, d, a, b, x[i+
2],
17,
606105819);
b = md5_ff(b, c, d, a, x[i+
3],
22, - 1044525330);
a = md5_ff(a, b, c, d, x[i+
4],
7 , - 176418897);
d = md5_ff(d, a, b, c, x[i+
5],
12,
1200080426);
c = md5_ff(c, d, a, b, x[i+
6],
17, - 1473231341);
b = md5_ff(b, c, d, a, x[i+
7],
22, - 45705983);
a = md5_ff(a, b, c, d, x[i+
8],
7 ,
1770035416);
d = md5_ff(d, a, b, c, x[i+
9],
12, - 1958414417);
c = md5_ff(c, d, a, b, x[i+ 10],
17, - 42063);
b = md5_ff(b, c, d, a, x[i+ 11],
22, - 1990404162);
a = md5_ff(a, b, c, d, x[i+ 12],
7 ,
1804603682);
d = md5_ff(d, a, b, c, x[i+ 13],
12, - 40341101);
c = md5_ff(c, d, a, b, x[i+ 14],
17, - 1502002290);
b = md5_ff(b, c, d, a, x[i+ 15],
22,
1236535329);

a = md5_gg(a, b, c, d, x[i+
1],
5 , - 165796510);
d = md5_gg(d, a, b, c, x[i+
6],
9 , - 1069501632);
c = md5_gg(c, d, a, b, x[i+ 11],
14,
643717713);
b = md5_gg(b, c, d, a, x[i+
0],
20, - 373897302);
a = md5_gg(a, b, c, d, x[i+
5],
5 , - 701558691);
d = md5_gg(d, a, b, c, x[i+ 10],
9 ,
38016083);
c = md5_gg(c, d, a, b, x[i+ 15],
14, - 660478335);
b = md5_gg(b, c, d, a, x[i+
4],
20, - 405537848);
a = md5_gg(a, b, c, d, x[i+
9],
5 ,
568446438);
d = md5_gg(d, a, b, c, x[i+ 14],
9 , - 1019803690);
c = md5_gg(c, d, a, b, x[i+
3],
14, - 187363961);
b = md5_gg(b, c, d, a, x[i+
8],
20,
1163531501);
a = md5_gg(a, b, c, d, x[i+ 13],
5 , - 1444681467);
d = md5_gg(d, a, b, c, x[i+
2],
9 , - 51403784);
c = md5_gg(c, d, a, b, x[i+
7],
14,
1735328473);
b = md5_gg(b, c, d, a, x[i+ 12],
20, - 1926607734);

a = md5_hh(a, b, c, d, x[i+
5],
4 , - 378558);
d = md5_hh(d, a, b, c, x[i+
8],
11, - 2022574463);
c = md5_hh(c, d, a, b, x[i+ 11],
16,
1839030562);
b = md5_hh(b, c, d, a, x[i+ 14],
23, - 35309556);
a = md5_hh(a, b, c, d, x[i+
1],
4 , - 1530992060);
d = md5_hh(d, a, b, c, x[i+
4],
11,
1272893353);
c = md5_hh(c, d, a, b, x[i+
7],
16, - 155497632);
b = md5_hh(b, c, d, a, x[i+ 10],
23, - 1094730640);
a = md5_hh(a, b, c, d, x[i+ 13],
4 ,
681279174);
d = md5_hh(d, a, b, c, x[i+
0],
11, - 358537222);
c = md5_hh(c, d, a, b, x[i+
3],
16, - 722521979);
b = md5_hh(b, c, d, a, x[i+
6],
23,
76029189);
a = md5_hh(a, b, c, d, x[i+
9],
4 , - 640364487);
d = md5_hh(d, a, b, c, x[i+ 12],
11, - 421815835);
c = md5_hh(c, d, a, b, x[i+ 15],
16,
530742520);
b = md5_hh(b, c, d, a, x[i+
2],
23, - 995338651);

a = md5_ii(a, b, c, d, x[i+
0],
6 , - 198630844);
d = md5_ii(d, a, b, c, x[i+
7],
10,
1126891415);
c = md5_ii(c, d, a, b, x[i+ 14],
15, - 1416354905);
b = md5_ii(b, c, d, a, x[i+
5],
21, - 57434055);
a = md5_ii(a, b, c, d, x[i+ 12],
6 ,
1700485571);
d = md5_ii(d, a, b, c, x[i+
3],
10, - 1894986606);
c = md5_ii(c, d, a, b, x[i+ 10],
15, - 1051523);
b = md5_ii(b, c, d, a, x[i+
1],
21, - 2054922799);
a = md5_ii(a, b, c, d, x[i+
8],
6 ,
1873313359);
d = md5_ii(d, a, b, c, x[i+ 15],
10, - 30611744);
c = md5_ii(c, d, a, b, x[i+
6],
15, - 1560198380);
b = md5_ii(b, c, d, a, x[i+ 13],
21,
1309151649);
a = md5_ii(a, b, c, d, x[i+
4],
6 , - 145523070);
d = md5_ii(d, a, b, c, x[i+ 11],
10, - 1120210379);
c = md5_ii(c, d, a, b, x[i+
2],
15,
718787259);
b = md5_ii(b, c, d, a, x[i+
9],
21, - 343485551);

a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d);

}

/*
* These functions implement the four basic operations the algorithm uses.
*/
function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
* Calculate the HMAC-MD5, of a key and some data
*/
function core_hmac_md5(key, data)
{
var bkey = str2binl(key);
if(bkey.length >
16) bkey = core_md5(bkey, key.length * chrsz);

var ipad = Array( 16), opad = Array( 16);
for( var i =
0; i <
16; i++)
{
ipad[i] = bkey[i] ^
0x36363636;
opad[i] = bkey[i] ^
0x5C5C5C5C;
}

var hash = core_md5(ipad.concat(str2binl(data)),
512 + data.length * chrsz);
return core_md5(opad.concat(hash),
512 +
128);
}

/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y)
{
var lsw = (x &
0xFFFF) + (y &
0xFFFF);
var msw = (x >>
16) + (y >>
16) + (lsw >>
16);
return (msw <<
16) | (lsw &
0xFFFF);
}

/*
* Bitwise rotate a 32-bit number to the left.
*/
function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> ( 32 - cnt));
}

/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str)
{
var bin = Array();
var mask = ( 1 << chrsz) -
1;
for( var i =
0; i < str.length * chrsz; i += chrsz)
bin[i>> 5] |= (str.charCodeAt(i / chrsz) & mask) << (i% 32);
return bin;
}

/*
* Convert an array of little-endian words to a string
*/
function binl2str(bin)
{
var str =
"";
var mask = ( 1 << chrsz) -
1;
for( var i =
0; i < bin.length *
32; i += chrsz)
str += String.fromCharCode((bin[i>> 5] >>> (i %
32)) & mask);
return str;
}

/*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray)
{
var hex_tab = hexcase ?
"0123456789ABCDEF" :
"0123456789abcdef";
var str =
"";
for( var i =
0; i < binarray.length *
4; i++)
{
str += hex_tab.charAt((binarray[i>> 2] >> ((i% 4)* 8+ 4)) &
0xF) +
hex_tab.charAt((binarray[i>> 2] >> ((i% 4)* 8 )) &
0xF);
}
return str;
}

/*
* Convert an array of little-endian words to a base-64 string
*/
function binl2b64(binarray)
{
var tab =
"+/";
var str =
"";
for( var i =
0; i < binarray.length *
4; i +=
3)
{
var triplet = (((binarray[i >>
2] >>
8 * ( i % 4)) &
0xFF) <<
16)
| (((binarray[i+ 1 >>
2] >>
8 * ((i+ 1)% 4)) &
0xFF) <<
8 )
| ((binarray[i+ 2 >>
2] >>
8 * ((i+ 2)% 4)) &
0xFF);
for( var j =
0; j <
4; j++)
{
if(i *
8 + j *
6 > binarray.length *
32) str += b64pad;
else str += tab.charAt((triplet >>
6*( 3-j)) &
0x3F);
}
}
return str;
}

mole.exports = {
hexMD5: hex_md5,
b64Md5: b64_md5,
}

7. 怎麼能通過js實現rsa加密

很簡單,C#自帶了很多演算法,DES,RSA,這些都可以自己加密解密。前面一個人說的MD5就沒法解密的了,那個是標簽類加密不可逆。可以自己網路我說的兩個,也可以直接問我這代碼怎麼寫。

8. 前台js實現的RSA加密,用C#模擬登陸,實現的加密演算法,密文長度不一樣

很簡單,C#自帶了很多演算法,DES,RSA,這些都可以自己加密解密。前面一個人說的MD5就沒法解密的了,那個是標簽類加密不可逆。可以自己網路我說的兩個,也可以直接問我這代碼怎麼寫。

9. 用JavaScript實現RSA加密和解密

下面是一個我收藏的函數,用於計算rsa的pow和mod有很高的效率。我不知道是什麼演算法,但這個函數真的很好用。
function RSAPowMod(txt, key, n) {
var a, b;
if (key % 2 == 0) {
a = 1
for (var i = 1; i <= key / 2; i++) {
b = (txt * txt) % n
a = (b * a) % n
}
} else {
a = txt
for (var i = 1; i <= key / 2; i++) {
b = (txt * txt) % n
a = (b * a) % n
}
}
return a;
}
txt,key,n都可以是字元串,代表著大數,當然,具體大到什麼范圍我不知道。
大於65535的數我測試過,表示支持不止兩位元組了,那推測是四位元組或者以上。
當我用九位數和十位數的時候,計算效率就不行。中間的,比如七位八位數的,我沒有現成的密鑰對,測試不了。
把對應的變數名改一改就符合要求了。(其實不用改,直接使用函數也是可以的。)