3

https://github.com/openresty/lua-resty-string

I am not able to decrypt back what was encrypted using Crypto-JS in browser Javascript / NodeJS:

// Encrypt
var ciphertext = CryptoJS.AES.encrypt('testingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtesting', '&&nH8P3bxk+?C4gR');

// Decrypt
var bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), '&&nH8P3bxk+?C4gR');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);

console.log(plaintext);

I can decrypt it in Java using:

Cipher.getInstance("AES/CBC/PKCS5Padding")

But, I get

nil

when I try to do it with resty.aes. Here is the code:

local aes = require "resty.aes"
local cipher = aes.cipher(256)
local aes_256_cbc_md5 = aes:new('&&nH8P3bxk+?C4gR', nil, cipher)

local cipherText = 'U2FsdGVkX1859eIyt4M7VHNBl9BGMdsemPYAADKmqs9sltwKINfzVMci0Vw1NLr73Iti67zQ0+JoqVcL59Gcp+4R5NY6wg2n3r0wqLcQRc7PkIGpgup1UJp4DzhXSIGHz08Eu/nEbt3jAh3S4GVUoVFbXLluf/BvedTGdsqcN2EPL9S/WQOc5QDyl9OQjpBl+QS56nWL0DO6iR/6CIoEuQ+zC/7KTpBw2jQf8sxuDNptZzwKLlDi2sWSaeCkvPj+m8zheAlnZzVc+L5JeLdcx7WkIRQImNs9P5bkhXmiK2nZnw4yco3QHbzRkRBJiB3HgdYDauHsuKmR21zv9VLjAcGTrZjiUbtrBfuTRawKOiAFm599Inbq+Ugu9n4RelQ2CTdxwDfe3ZE3kscP3dyAmg=='
ngx.say(aes_256_cbc_md5:decrypt(cipherText))

What do I need to change with the server side decryption?

halfer
  • 19,824
  • 17
  • 99
  • 186
Prakhar Mishra
  • 1,586
  • 4
  • 28
  • 52

1 Answers1

2

First, you need to decode base64-encoded encrypted data to bytes.

Second, your encrypted data is salted and stored as described here:

Files have an 8-byte signature, followed by an 8(?)-byte salt. Following the salt is the encrypted data.

Files begin with an 8-byte signature: the ASCII characters "Salted__".

So, you should extract both salt and actual encrypted data from “OpenSSL salted format”-ted string (Salted__{salt}{data}):

-- aes_demo.lua
local aes = require "resty.aes"


local encrypted = ngx.decode_base64('U2FsdGVkX1859eIyt4M7VHNBl9BGMdsemPYAADKmqs9sltwKINfzVMci0Vw1NLr73Iti67zQ0+JoqVcL59Gcp+4R5NY6wg2n3r0wqLcQRc7PkIGpgup1UJp4DzhXSIGHz08Eu/nEbt3jAh3S4GVUoVFbXLluf/BvedTGdsqcN2EPL9S/WQOc5QDyl9OQjpBl+QS56nWL0DO6iR/6CIoEuQ+zC/7KTpBw2jQf8sxuDNptZzwKLlDi2sWSaeCkvPj+m8zheAlnZzVc+L5JeLdcx7WkIRQImNs9P5bkhXmiK2nZnw4yco3QHbzRkRBJiB3HgdYDauHsuKmR21zv9VLjAcGTrZjiUbtrBfuTRawKOiAFm599Inbq+Ugu9n4RelQ2CTdxwDfe3ZE3kscP3dyAmg==')

local salt = encrypted:sub(9, 16)   -- skip first 8 bytes, get salt value (8 bytes)
local data = encrypted:sub(17)      -- rest of data is actual encrypted data
local cipher = aes.cipher(256)
local aes_256_cbc_md5 = aes:new('&&nH8P3bxk+?C4gR', salt, cipher)

ngx.say(aes_256_cbc_md5:decrypt(data))

$ resty aes_demo.lua

testingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtestingtesting
Community
  • 1
  • 1
un.def
  • 1,200
  • 6
  • 10