encryb / simplecrypto

Simple wrapper around WebCrypto implementations
Apache License 2.0
21 stars 4 forks source link

encryption problem with asym example code #6

Open pwFoo opened 5 years ago

pwFoo commented 5 years ago

Hi @encryb Error using the example asym code from readme:

Arguments(2) ["Could not import private encrypt key", ReferenceError: key is not defined

source code

    simpleCrypto.asym.generateEncryptKey(logError, function(keys){
        simpleCrypto.asym.encrypt(key.publicKey, data, logError, function(encrypted) {
            simpleCrypto.asym.decrypt(key.privateKey, encrypted, logError.bind(null, done), function(decrypted){});
        });
    });

Looks like typo "key" instead of "keys"? Changed code:

    simpleCrypto.asym.generateEncryptKey(logError, function(keys){
        simpleCrypto.asym.encrypt(keys.publicKey, data, logError, function(encrypted) {
            simpleCrypto.asym.decrypt(keys.privateKey, encrypted, logError.bind(null, done), function(decrypted){});
        });
    });

New error message

Arguments(2) ["Could not AES Encrypt", TypeError: Failed to execute 'encrypt' on 'SubtleCrypto': The provided value is not of type '(Array…, callee: ƒ, Symbol(Symbol.iterator): ƒ]
0: "Could not AES Encrypt"
1: TypeError: Failed to execute 'encrypt' on 'SubtleCrypto': The provided value is not of type '(ArrayBuffer or ArrayBufferView)' at Object.encrypt (https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:189:36) at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:961:34 at Object.importKeys (https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:909:21) at Object.encrypt (https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:952:34) at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:936:38 at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:894:24 at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:174:25
message: "Failed to execute 'encrypt' on 'SubtleCrypto': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'"
stack: "TypeError: Failed to execute 'encrypt' on 'SubtleCrypto': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'↵    at Object.encrypt (https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:189:36)↵    at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:961:34↵    at Object.importKeys (https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:909:21)↵    at Object.encrypt (https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:952:34)↵    at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:936:38↵    at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:894:24↵    at https://gitcdn.xyz/cdn/encryb/simplecrypto/83dc5a31c5b8013fbae19a5d729d6e46a47d74e7/src/simplecrypto.js:174:25"

Any idea why?

pwFoo commented 5 years ago

Got it fixed...

var data = new Uint8Array([5,4,3,2,1]);
//var data = ["Hi my friend!"];

var logError = function() {
    console.log(arguments);
}

simpleCrypto.asym.generateEncryptKey(logError, function(key){
    console.log("KEY:\n", key)
    simpleCrypto.asym.encrypt(key.publicKey, data, logError, function(encrypted) {
        console.log("ENCRYPTED:\n", encrypted);
        simpleCrypto.asym.decrypt(key.privateKey, encrypted, logError.bind(null, "done"), function(decrypted){
            console.log("DECRYPTED:\n", decrypted);
        });
    });
});

But it doesn't work with strings and needs "ArrayBuffer or ArrayBufferView". I'm know different libraries, but all work with strings (text for example). So how to use simplecrypto with (long) text messages (optional with embedded images)? Also found a newer repo here: https://github.com/Stanwar/simplecrypto

@encryb @Stanwar

pwFoo commented 5 years ago

Convert ArrayBuffer <-> String, found a optimized arrayBufferToString to avoid the RangeError: Maximum call stack size exceeded error.

var arrayBufferToString = function(buffer) {
    //return String.fromCharCode.apply(null, new Uint8Array(buffer));   // RangeError: Maximum call stack size exceeded
    var decoder = new TextDecoder ();
    return decoder.decode (buffer);
}

var stringToArrayBuffer = function(str) {
    return (new Uint8Array([].map.call(str,function(x){return x.charCodeAt(0)}))).buffer;
}

I generated a ~30MB string and ecrynt / sign keys, encrypted+signed and verified+decrypted the long data and it was lightning fast! 🥇 String generated with:

var string = "1234567890";
var iterations = 13;
for (var i = 0; i < iterations; i++) {
    string += string+string;
}

Modified example code to add sign+verify and additional debugging output

simpleCrypto.asym.generateKeys(logError, function(keys){
    console.log("ENCRYPTION KEY:\n", keys.encrypt);
    console.log("SIGNING KEY:\n", keys.sign);
    simpleCrypto.asym.encryptAndSign(keys.encrypt.publicKey, keys.sign.privateKey, data, logError, function(encrypted) {
        console.log("ENCRYPTED:\n", encrypted);
        simpleCrypto.asym.verifyAndDecrypt(keys.encrypt.privateKey, keys.sign.publicKey, encrypted, logError.bind(null, "done"), function(decrypted){
            console.log("DECRYPTED ArrayBuffer:\n", decrypted);
            console.log("DECRYPTED STRING:\n", arrayBufferToString(decrypted));
        });
    });
});

After get long text data / string working I'm really impressed about speed of simplecrypto! I have to read about save / load keys to reuse

pwFoo commented 5 years ago

Maybe I move to https://github.com/safebash/opencrypto because it's based on identical webcrypto api. But at the moment I have a problem with increasing data to encrypt because of DOMexception error: https://github.com/safebash/opencrypto/issues/5