PageSourceSearch

https://daemyungresort.com/js/dmenc/rsa.js

js daemyungresort.com collected 2026-10-02 11:22:13 UTC 1,448 bytes, 73 lines download raw bytes

1//Depends on jsbn.js and rng.js
2
3//Version 1.1: support utf-8 encoding in pkcs1pad2
4
5//convert a (hex) string to a bignum object
6function parseBigInt(str,r) {
7	return new BigInteger(str,r);
8}
9
10function linebrk(s,n) {
11	var ret = "";
12	var i = 0;
13	while(i + n < s.length) {
14		ret += s.substring(i,i+n) + "\n";
15		i += n;
16	}
17	return ret + s.substring(i,s.length);
18}
19
20function byte2Hex(b) {
21	if(b < 0x10)
22		return "0" + b.toString(16);
23	else
24		return b.toString(16);
25}
26
27//"empty" RSA key constructor
28function RSAKey() {
29	this.n = null;
30	this.e = 0;
31	this.d = null;
32	this.p = null;
33	this.q = null;
34	this.dmp1 = null;
35	this.dmq1 = null;
36	this.coeff = null;
37}
38
39//Set the public key fields N and e from hex strings
40function RSASetPublic(N,E) {
41	if(N != null && E != null && N.length > 0 && E.length > 0) {
42		this.n = parseBigInt(N,16);
43		this.e = parseInt(E,16);
44	}
45	else
46		alert("Invalid RSA public key");
47}
48
49//Perform raw public operation on "x": return x^e (mod n)
50function RSADoPublic(x) {
51	return x.modPowInt(this.e, this.n);
52}
53
54function RSAEncrypt(text) {
55	var ba = new Array();	
56	for(i=0; i<text.length; i++)
57		ba[i] = text.charCodeAt(i);
58
59	m = new BigInteger(ba);
60
61	var c = this.doPublic(m);
62	if(c == null) return null;
63	var h = c.toString(16);
64	if((h.length & 1) == 0) return h; else return "0" + h;
65}
66
67
68//protected
69RSAKey.prototype.doPublic = RSADoPublic;
70
71//public
72RSAKey.prototype.setPublic = RSASetPublic;
73RSAKey.prototype.encrypt = RSAEncrypt;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.