How to decrypt with CryptoJs?
aprakasa opened this issue · comments
The go file:
package main
import (
"fmt"
openssl "github.com/Luzifer/go-openssl/v4"
)
func main() {
plaintext := "Hello World!"
passphrase := "z4yH36a6zerhfE5427ZV"
o := openssl.New()
enc, err := o.EncryptBytes(passphrase, []byte(plaintext), openssl.PBKDF2SHA256)
if err != nil {
fmt.Printf("An error occurred: %s\n", err)
}
fmt.Printf("Encrypted text: %s\n", string(enc)) // U2FsdGVkX1928WeGHGmlUbUOk3+MdJ5gt7ydozSxaP0=
}
The js file:
const CryptoJS = require("crypto-js");
const message = "U2FsdGVkX1928WeGHGmlUbUOk3+MdJ5gt7ydozSxaP0="
const key = "z4yH36a6zerhfE5427ZV"
var decryptedData = CryptoJS.AES.decrypt(message, key).toString(CryptoJS.enc.Utf8))
console.log(decryptedData) //
https://svelte.dev/repl/ff7fe2b993e847a4b19947761fe257b9?version=3.37.0
I'm not sure about the implementation you're using but the original crypto-js did only support MD5 key-derivation for AES encryption while your Go-code is using the openssl.PBKDF2SHA256
key-derivation…
I am sorry it was copy paste from the example.
Not sure why I can't decrypt the encrypted data although use the same key-derivation.
package main
import (
"fmt"
openssl "github.com/Luzifer/go-openssl/v4"
)
func main() {
c := "U2FsdGVkX18usncIYQU7zpoOtzU88HRePCuBmhJvNJ4Pz7eM5kLKE9DSl+ZQOUzP"
plaintext := "Hello World!"
passphrase := "z4yH36a6zerhfE5427ZV"
o := openssl.New()
enc, _ := o.EncryptBytes(passphrase, []byte(plaintext), openssl.PBKDF2MD5)
dec, _ := o.DecryptBytes(passphrase, []byte(string(enc)), openssl.BytesToKeyMD5)
cjs, _ := o.DecryptBytes(passphrase, []byte(string(c)), openssl.BytesToKeyMD5)
fmt.Printf("Encrypted text: %s\n", string(enc)) // U2FsdGVkX18q83+Cwf8qmuTEQyP3VKJcfCgsUE/OtOU=
fmt.Printf("Decrypted text: %s\n", string(dec)) //
fmt.Printf("CryptoJS: %s\n", string(cjs)) // Encrypted by crypto-js
}
Well, the decrypting of the crypto-js part works fine and decrypting data encrypting with openssl.PBKDF2MD5
can only be decrypted with openssl.PBKDF2MD5
, not with openssl.BytesToKeyMD5
. You need to use the same key-derivation function for decryption you used for encryption.
So modified your example:
package main
import (
"fmt"
openssl "github.com/Luzifer/go-openssl/v4"
)
func main() {
c := "U2FsdGVkX18usncIYQU7zpoOtzU88HRePCuBmhJvNJ4Pz7eM5kLKE9DSl+ZQOUzP"
plaintext := "Hello World!"
passphrase := "z4yH36a6zerhfE5427ZV"
o := openssl.New()
enc, _ := o.EncryptBytes(passphrase, []byte(plaintext), openssl.BytesToKeyMD5) // <<< See this changed
dec, _ := o.DecryptBytes(passphrase, []byte(string(enc)), openssl.BytesToKeyMD5)
cjs, _ := o.DecryptBytes(passphrase, []byte(string(c)), openssl.BytesToKeyMD5)
fmt.Printf("Encrypted text: %s\n", string(enc)) // U2FsdGVkX18q83+Cwf8qmuTEQyP3VKJcfCgsUE/OtOU=
fmt.Printf("Decrypted text: %s\n", string(dec)) //
fmt.Printf("CryptoJS: %s\n", string(cjs)) // Encrypted by crypto-js
}
Thanks for your awesome work