У меня есть конечная точка API (API Gateway) и простой сценарий шифрования / дешифрования AES в Lambda. Шифрование работает нормально, но не расшифровка, и я не могу понять, почему нет.
Мои вызовы API идентичны, и я изменяю только параметр «действие 1» для шифрования и параметр «действие 0» для расшифровки.
Что-то не так в коде?
const crypto = require('crypto')
function encrypt(text, password){
const cipher = crypto.createCipher('aes256', password)
var encrypted = cipher.update(text, 'utf8', 'hex')
encrypted += cipher.final('hex')
console.log("Encrypted key: "+encrypted)
return encrypted
}
function decrypt(text, password){
const decipher = crypto.createDecipher('aes256', password)
var decrypted = decipher.update(text, 'hex', 'utf8')
decrypted += decipher.final('utf8')
console.log("Decrypted key: "+decrypted)
return decrypted
}
exports.handler = async (event) => {
var password = "bootstrap"
var authValue = event["headers"]["Authorization"]
if(authValue == "1234567890"){
const action = event['queryStringParameters']['action']
var text1 = event['queryStringParameters']['text1']
var text2 = event['queryStringParameters']['text2']
console.log(password+text1)
if(action == 1){
var res1 = encrypt(text1, password)
var res2 = encrypt(text2, password)
}
else{
var res1 = decrypt(text1, password)
var res2 = decrypt(text2, password)
}
}
else{
var res = "Authentication failed! Access key missing"
}
const response = {
statusCode: 200,
body: JSON.stringify({ res1: res1, res2 : res2 })
};
return response;
};