Created
September 14, 2021 04:09
-
-
Save mudssrali/952ff6c7424aa7425e6d733fbfea1859 to your computer and use it in GitHub Desktop.
a simple AES service to encrypt and decrypt plaintext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const cors = require('cors'); | |
const crypto = require('crypto-js'); | |
const PORT = process.env.PORT ?? 8080; | |
const HOST = process.env.HOST ?? '0.0.0.0'; | |
const SECRET_KEY = process.env.SECRET_KEY ?? "write something secret here"; | |
const app = express(); | |
app.use(cors()); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: true })); | |
app.post("/api/aes/encrypt", function (req, res) { | |
const plainText = req.body.rawText; | |
if (!plainText) { | |
res.json({ error: "Plain text is not available" }); | |
} else { | |
const encrypted = crypto.AES.encrypt(plainText, SECRET_KEY).toString(); | |
res.json({ encryptedText: encrypted }); | |
} | |
}); | |
app.post("/api/aes/decrypt", function (req, res) { | |
const encryptedText = req.body.encryptedText; | |
if (!encryptedText) { | |
res.json({ error: "Encrypted text is not available" }); | |
} else { | |
const bytes = crypto.AES.decrypt(encryptedText, SECRET_KEY); | |
const originalText = bytes.toString(crypto.enc.Utf8); | |
res.json({ originalText}); | |
} | |
}); | |
app.listen(PORT, HOST, () => { | |
console.info(`Server is listening on ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment