Comments (5)
A quick search on NPM: https://www.npmjs.com/search?q=salsa20
Found 5:
- https://www.npmjs.com/package/salsa20
- https://www.npmjs.com/package/js-salsa20
- https://www.npmjs.com/package/node-salsa20
- https://www.npmjs.com/package/natrium
- https://www.npmjs.com/package/@stablelib/salsa20
Have you tried all of them? Or any of them?
from encrypt.
@leocavalcante i had tried the first three, the js-salsa20 seems to be working, but i cannot retrieve a proper string.
code
output
from encrypt.
Got it working here, 3 key points:
-
Use built-in
TextEncoder
andTextDecoder
from Node.js Utils:
StringDecoder
works onBuffer
s, not the same asUint8Array
and thistext-enconding
package has been deprecated and no longer maintained. -
Use a 32-bit length key on Dart-side:
private!!!!!!!!!
will generate a 16 byte longUint8Array
andjs-salsa20
sets a minimum of 32 bytes. Just make sure your key is 32 lengtt long on ASCII chars, like:my32lengthsupersecretnooneknows1
. -
The Dart
encrypt
package outputs a hexadecimal representation of the bytes:
This is also very important, you're trying to usec5cc91943cf0
as a regular String, but is actually your bytes, but hexdec encoded, a little VanillaJS helper to transform hexstrings intoUint8Array
:const hexToBytes = hex => new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)))
.
Full code on Dart-side:
import 'package:encrypt/encrypt.dart';
void main() {
final key = 'my32lengthsupersecretnooneknows1';
final iv = '8bytesiv';
final plainText = 'Secret';
final encrypter = new Encrypter(new Salsa20(key, iv));
final encrypted = encrypter.encrypt(plainText);
final decrypted = encrypter.decrypt(encrypted);
print(encrypted);
print(decrypted);
}
Note it is using a 32 bytes length key, using this new key our Secret
gets a different hexadecimal output: 4287bf31150e
.
Using this output on the Node.js-side:
const Salsa20 = require('js-salsa20')
const { TextEncoder, TextDecoder } = require('util')
const hexToBytes = hex => new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)))
const encoder = new TextEncoder()
const decoder = new TextDecoder()
const key = encoder.encode('my32lengthsupersecretnooneknows1')
const iv = encoder.encode('8bytesiv')
const encrypted = hexToBytes('4287bf31150e')
const encrypter = new Salsa20(key, iv)
const messageBytes = encrypter.decrypt(encrypted)
const message = decoder.decode(messageBytes)
console.log(message)
Note that const encrypted = hexToBytes('4287bf31150e')
isn't using TextEncoder
, it is using our little hex-to-uint8array helper.
The console.log(message)
output is as expected: Secret
.
Let me know if you can reproduce this steps on you side.
from encrypt.
I also like to point out that if you are doing some client-Dart to server-Node.js over HTTP you should favor TLS (SSL/HTTPS) it will encrypt and decrypt things over network for you. Also very important if you Dart code is on client-side note that your key will be also on the client-side, this lib is properly used on Dart for server-side. Never trust sensitive data like keys on client code.
from encrypt.
Thanks a lot! Actually I'm using this lib for my little flutter project, so that I can encrypt the client's credentials and send to my server without being eavesdropped or reverse engineered.
from encrypt.
Related Issues (20)
- What algorithm is RIJNDAEL_256
- publish latest version HOT 5
- length
- type 'KeyParameter' is not a subtype of type 'PaddedBlockCipherParameters in AESMode.ecb HOT 2
- 5.0.2 cannot decrypt data encoded with 5.0.1 HOT 36
- 5.0.1, 5.0.2 and 5.03 cannot decrypt data HOT 1
- AES 128 CFB decryption
- I can't decrypt the text in 5.0.3 😕 HOT 11
- Output double in size, AES in CTR
- After RSA public key encryption, I am unable to decrypt it. It shows an error message: 'Error: Expected a value of type 'ASN1Integer', but got one of type 'ASN1Sequence'.
- Getting Private Key Issue HOT 1
- Invalid argument(s): Initialization vector must be the same length as block size
- You can add an onProgress
- Invalid argument(s): Invalid or corrupted pad block HOT 6
- Different encrypted output
- Encryption between platforms
- Web release Invalid or corrupted pad block HOT 3
- '(x >= 0) && (x <= _MASK_32)': is not true
- Exception has occurred. ArgumentError (Invalid argument(s): Invalid or corrupted pad block) HOT 11
- About Privacy Manifest HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from encrypt.