Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add encryptOAEP to support encrypt with padding: OAEP and oaepHash: sha256 #317

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ else {
- Note that you have to provide the hash function. In this example we use one from the [CryptoJS](https://github.com/brix/crypto-js) library, but you can use whichever you want.
- Also, unless you use a custom hash function, you should provide the hash type to the `sign` method. Possible values are: `md2`, `md5`, `sha1`, `sha224`, `sha256`, `sha384`, `sha512`, `ripemd160`.

- You can encrypt text with padding: RSA_PKCS1_OAEP_PADDING and oaepHash: sha256 by doing the following in code.

```javascript
// Encrypt with the public key...
var encrypt = new JSEncrypt();
encrypt.setPublicKey($('#pubkey').val());
var encrypted = encrypt.encryptOAEP($('#input').val());
```

Other Information
========================

Expand Down
95 changes: 53 additions & 42 deletions bin/jsencrypt.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions bin/jsencrypt.min.js

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions bin/jsencrypt.min.js.LICENSE.txt

This file was deleted.

10 changes: 10 additions & 0 deletions lib/JSEncrypt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ export declare class JSEncrypt {
* @public
*/
encrypt(str: string): string | false;
/**
* Proxy method for RSAKey object's encrypt with padding: RSA_PKCS1_OAEP_PADDING and oaepHash: sha256,
* encrypt the string using the public
* components of the rsa key object. Note that if the object was not set will be created
* on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
* @param {string} str the string to encrypt
* @return {string} the encrypted string encoded in base64
* @public
*/
encryptOAEP(str: string): string | false;
/**
* Proxy method for RSAKey object's sign.
* @param {string} str the string to sign
Expand Down
23 changes: 20 additions & 3 deletions lib/JSEncrypt.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var _a;
import { b64tohex, hex2b64 } from "./lib/jsbn/base64";
import { JSEncryptRSAKey } from "./JSEncryptRSAKey";
var version = typeof process !== 'undefined'
? (_a = process.env) === null || _a === void 0 ? void 0 : _a.npm_package_version
: undefined;
import { oaep_pad } from './lib/jsbn/rsa';
var version = typeof process !== "undefined" ? (_a = process.env) === null || _a === void 0 ? void 0 : _a.npm_package_version : undefined;
/**
*
* @param {Object} [options = {}] - An object to customize JSEncrypt behaviour
Expand Down Expand Up @@ -90,6 +89,24 @@ var JSEncrypt = /** @class */ (function () {
return false;
}
};
/**
* Proxy method for RSAKey object's encrypt with padding: RSA_PKCS1_OAEP_PADDING and oaepHash: sha256,
* encrypt the string using the public
* components of the rsa key object. Note that if the object was not set will be created
* on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
* @param {string} str the string to encrypt
* @return {string} the encrypted string encoded in base64
* @public
*/
JSEncrypt.prototype.encryptOAEP = function (str) {
// Return the encrypted string.
try {
return hex2b64(this.getKey().encrypt(str, oaep_pad));
}
catch (ex) {
return false;
}
};
/**
* Proxy method for RSAKey object's sign.
* @param {string} str the string to sign
Expand Down
3 changes: 2 additions & 1 deletion lib/lib/jsbn/rsa.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BigInteger } from "./jsbn";
export declare function oaep_pad(s: string, n: number): BigInteger;
export declare class RSAKey {
constructor();
doPublic(x: BigInteger): BigInteger;
doPrivate(x: BigInteger): BigInteger;
setPublic(N: string, E: string): void;
encrypt(text: string): string;
encrypt(text: string, paddingFunction?: (s: string, n: number) => BigInteger): string;
setPrivate(N: string, E: string, D: string): void;
setPrivateEx(N: string, E: string, D: string, P: string, Q: string, DP: string, DQ: string, C: string): void;
generate(B: number, E: string): void;
Expand Down
46 changes: 44 additions & 2 deletions lib/lib/jsbn/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// convert a (hex) string to a bignum object
import { BigInteger, nbi, parseBigInt } from "./jsbn";
import { SecureRandom } from "./rng";
import { rstr_sha256 } from './sha256';
// function linebrk(s,n) {
// var ret = "";
// var i = 0;
Expand Down Expand Up @@ -68,6 +69,44 @@ function pkcs1pad2(s, n) {
ba[--n] = 0;
return new BigInteger(ba);
}
// PKCS#1 (OAEP) mask generation function, using SHA-256
function oaep_mgf1_arr(seed, len, hashFunc) {
var mask = "", i = 0;
while (mask.length < len) {
mask += hashFunc(String.fromCharCode.apply(String, seed.concat([
(i & 0xff000000) >> 24,
(i & 0x00ff0000) >> 16,
(i & 0x0000ff00) >> 8,
i & 0x000000ff,
])));
i += 1;
}
return mask;
}
var SHA256_SIZE = 32;
// PKCS#1 (OAEP) pad input string s to n bytes, and return a BigInteger
export function oaep_pad(s, n) {
var hashLen = SHA256_SIZE;
var hashFunc = rstr_sha256;
if (s.length + 2 * hashLen + 2 > n) {
throw "Message too long for RSA";
}
var PS = "", i;
for (i = 0; i < n - s.length - 2 * hashLen - 2; i += 1) {
PS += "\x00";
}
var DB = hashFunc("") + PS + "\x01" + s, seed = new Array(hashLen);
new SecureRandom().nextBytes(seed);
var dbMask = oaep_mgf1_arr(seed, DB.length, hashFunc), maskedDB = [];
for (i = 0; i < DB.length; i += 1) {
maskedDB[i] = DB.charCodeAt(i) ^ dbMask.charCodeAt(i);
}
var seedMask = oaep_mgf1_arr(maskedDB, seed.length, hashFunc), maskedSeed = [0];
for (i = 0; i < seed.length; i += 1) {
maskedSeed[i + 1] = seed[i] ^ seedMask.charCodeAt(i);
}
return new BigInteger(maskedSeed.concat(maskedDB));
}
// "empty" RSA key constructor
var RSAKey = /** @class */ (function () {
function RSAKey() {
Expand Down Expand Up @@ -116,9 +155,12 @@ var RSAKey = /** @class */ (function () {
};
// RSAKey.prototype.encrypt = RSAEncrypt;
// Return the PKCS#1 RSA encryption of "text" as an even-length hex string
RSAKey.prototype.encrypt = function (text) {
RSAKey.prototype.encrypt = function (text, paddingFunction) {
if (typeof paddingFunction === 'undefined') {
paddingFunction = pkcs1pad2;
}
var maxLength = (this.n.bitLength() + 7) >> 3;
var m = pkcs1pad2(text, maxLength);
var m = paddingFunction(text, maxLength);
if (m == null) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions lib/lib/jsbn/sha256.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function rstr_sha256(s: string): string;
Loading