brix / crypto-js

JavaScript library of crypto standards.
Other
15.88k stars 2.39k forks source link

Encrypt text to binary and then decrypt that binary #437

Closed ghost closed 1 year ago

ghost commented 2 years ago

Just simple code (live: https://stackblitz.com/edit/typescript-vxan7n?file=index.ts)

import * as crypto from 'crypto-js';

const key = 'secret key';
const message = 'Hello, World! Привет! Azərbaycan! öğü 你好世界!';

const originalWords = crypto.enc.Utf16.parse(message);
const cipherParams = crypto.AES.encrypt(originalWords, key);
const encryptedWords = cipherParams.ciphertext;
const decryptedWords = crypto.AES.decrypt(cipherParams, key);

console.log({ originalWords, encryptedWords, decryptedWords });

The question is I want to store encrypted as binary. Then I will read that binary and decrypt it. How to do so? I cannot understand what should I put into binary file the cipherParams or encryptedWords. Could you show an example or something. Like cipherParams looks a bit sensetive data, it has KEY and algotiyhm information about ciphertext. And also why decrypt() takes cipherParams and not ciphertext ?:(

daizhenhong commented 2 years ago

have you solved this issue? i am having the same problem.

ghost commented 2 years ago

I come up with this https://stackblitz.com/edit/typescript-vxan7n?file=index.ts

Basically extracting salt iv and ciphertext from cipherParams and putting them in object then stringifing the object and converting that string to base64 (binary). Same process in decrypting but in reverse

I don't know, maybe there is better solution

ghost commented 2 years ago

@evanvosberg Hello, is this a good approach?