Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: ryanramage/kanso-encryption
base: df3dacb0ed
...
head fork: ryanramage/kanso-encryption
compare: 3f61faabac
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 28, 2012
Ryan Ramage Work in progress with a keystore. dd07330
Ryan Ramage Add a keystore, which wraps a private key.
Also, a command line, which can be used to generate a keystore, save it
local and show the public key.
3f61faa
1  .gitignore
View
@@ -1,2 +1,3 @@
.idea
.DS_Store
+.kanso
70 command-line.js
View
@@ -0,0 +1,70 @@
+var cryptico = require('./cryptico');
+var sjcl = require('./sjcl');
+
+var fs = require('fs');
+var path = require('path');
+
+var dir_name = '.kanso';
+var file_name = 'keystore';
+
+var password = process.argv[2];
+
+var storage_dir = findStorageDir(process.env);
+
+if (isKeyStored(storage_dir)) {
+ console.log('key already exists in ' + storage_dir);
+} else {
+ var keystoreFile = generateKeystore(password);
+ store(keystoreFile, storage_dir);
+
+}
+
+function generateKeystore(passphrase) {
+ var Bits = 1024;
+ var RSAkey = cryptico.generateRSAKey(passphrase, Bits, true);
+ var PublicKeyString = cryptico.publicKeyString(RSAkey);
+
+ console.log('Public Key:');
+ console.log(PublicKeyString);
+ var keystore = sjcl.encrypt(passphrase, cryptico.rsa_key_to_string(RSAkey), {adata: PublicKeyString} );
+ return keystore;
+}
+
+function findStorageDir(env) {
+ var home = env.HOME;
+ if (!home) home = __dirname;
+ return path.join(home, dir_name);
+}
+
+function isKeyStored(dir) {
+ var stored = false;
+ if (path.existsSync(dir)) {
+ if (path.existsSync(path.join(dir, file_name))) {
+ stored = true;
+ }
+ }
+ return stored;
+}
+
+
+
+function store(keystore, dir) {
+ fs.mkdir(dir, 0700, function(err){
+
+ var location = path.join(dir, file_name)
+
+ fs.writeFile(location, keystore, function (err) {
+ if (err) throw err;
+ console.log('Private key saved at ' + location);
+ });
+ });
+}
+
+
+
+
+
+
+
+
+
26 cryptico.js
View
@@ -2699,6 +2699,8 @@ RSAKey.prototype.doPublic = RSADoPublic;
RSAKey.prototype.setPublic = RSASetPublic;
RSAKey.prototype.encrypt = RSAEncrypt;
+
+
// Version 1.1: support utf-8 decoding in pkcs1unpad2
// Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
@@ -2759,7 +2761,7 @@ function RSASetPrivateEx(N, E, D, P, Q, DP, DQ, C)
this.dmq1 = parseBigInt(DQ, 16);
this.coeff = parseBigInt(C, 16);
}
- else alert("Invalid RSA private key");
+ else throw "Invalid RSA private key";
}
// Generate a new random private key B bits long, using public expt E
@@ -3540,3 +3542,25 @@ exports.publicKeyString = cryptico.publicKeyString;
exports.publicKeyID = cryptico.publicKeyID;
exports.encrypt = cryptico.encrypt;
exports.decrypt = cryptico.decrypt;
+exports.rsa_key_from_string = function(str) {
+ var r2 = JSON.parse(str) ;
+
+ var rsa2 = new RSAKey();
+ rsa2.setPrivateEx(r2.n, r2.e, r2.d, r2.p, r2.q, r2.dmp1, r2.dmq1, r2.coeff);
+
+ return rsa2;
+}
+
+exports.rsa_key_to_string = function(RSAkey) {
+ var result = {
+ n : RSAkey.n.toString(16),
+ e : RSAkey.e.toString(16),
+ d : RSAkey.d.toString(16),
+ p : RSAkey.p.toString(16),
+ q : RSAkey.q.toString(16),
+ dmp1 : RSAkey.dmp1.toString(16),
+ dmq1 : RSAkey.dmq1.toString(16),
+ coeff : RSAkey.coeff.toString(16)
+ }
+ return JSON.stringify(result);
+}
2  kanso.json
View
@@ -8,7 +8,7 @@
"url": "http://github.com/ryanramage"
}
],
- "modules": ["cryptico.js", "sjcl.js"],
+ "modules": ["cryptico.js", "sjcl.js", "keystore.js"],
"modules_attachment": false,
"dependencies": {
"modules": ">=0.0.8"
26 keystore.js
View
@@ -0,0 +1,26 @@
+var cryptico = require('cryptico');
+var sjcl = require('sjcl');
+
+
+exports.generate = function(passphrase) {
+ var Bits = 1024;
+ var RSAkey = cryptico.generateRSAKey(passphrase, Bits, true);
+ var PublicKeyString = cryptico.publicKeyString(RSAkey);
+
+ var keystore = sjcl.encrypt(passphrase, cryptico.rsa_key_to_string(RSAkey), {adata: PublicKeyString} );
+ return keystore;
+}
+
+
+exports.decryptData = function(passphrase, cipherText, keystore) {
+ var key_str = sjcl.decrypt(passphrase, keystore );
+ var RSAKey = cryptico.rsa_key_from_string(key_str);
+ return cryptico.decrypt(cipherText, RSAKey);
+}
+
+exports.signAndEncryptData = function(passphrase, plaintext, recipient_public_key, keystore) {
+ var key_str = sjcl.decrypt(passphrase, keystore );
+ var RSAKey = cryptico.rsa_key_from_string(key_str);
+ return cryptico.encrypt(plaintext, recipiant_public_key, RSAKey);
+}
+

No commit comments for this range

Something went wrong with that request. Please try again.