├── .gitignore ├── src ├── env │ └── developer.env.ts ├── encrypt.symmetric.ts ├── decrypt.symmetric.ts └── index.ts ├── tsconfig.json ├── package.json ├── README.md └── express-crypto.postman_collection.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /src/env/developer.env.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | 'ENCRYPTION_KEY': '87debcd956427aba61e42b14c6ae05a2', 3 | 'IV_LENGTH': 16 4 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node16/tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "dist" 5 | }, 6 | "include": [ 7 | "src" 8 | ], 9 | "exclude": [ 10 | "node_modules" 11 | ] 12 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "deno", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "ts-node src/index.ts", 8 | "start:dev": "tsnd --respawn src/index.ts" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "cors": "^2.8.5", 15 | "express": "^4.18.2" 16 | }, 17 | "devDependencies": { 18 | "@tsconfig/node16": "^1.0.4", 19 | "@types/cors": "^2.8.13", 20 | "@types/express": "^4.17.17", 21 | "@types/node": "^20.2.3", 22 | "ts-node": "^10.9.1", 23 | "ts-node-dev": "^2.0.0", 24 | "typescript": "^5.0.4" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/encrypt.symmetric.ts: -------------------------------------------------------------------------------- 1 | import crypto from "node:crypto"; 2 | import { Buffer } from "node:buffer"; 3 | 4 | import CONFIG_ENV from './env/developer.env'; 5 | 6 | const encryptSymmetric = async (plainText: string) => { 7 | try { 8 | const cipher = crypto.createCipheriv('aes-256-cbc', 9 | Buffer.from(CONFIG_ENV.ENCRYPTION_KEY), 10 | Buffer.from(CONFIG_ENV.ENCRYPTION_KEY.substring(0,CONFIG_ENV.IV_LENGTH))); 11 | let encrypted = cipher.update(plainText); 12 | encrypted = Buffer.concat([encrypted, cipher.final()]); 13 | return { encryptedData: encrypted.toString('base64') }; 14 | } catch (error) { 15 | console.log(error); 16 | } 17 | }; 18 | 19 | export default encryptSymmetric; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Symmetric-key algorithm 3 | https://en.wikipedia.org/wiki/Symmetric-key_algorithm 4 | 5 | asymmetric cryptography 6 | https://en.wikipedia.org/wiki/Public-key_cryptography 7 | 8 | command : local: npm start or npm start:dev 9 | 10 | health : http://localhost:5001/health 11 | 12 | encrypt: 13 | 14 | post : http://localhost:5001/encrypt 15 | 16 | { 17 | "plainText": "sample application" 18 | } 19 | 20 | { 21 | "encryptedData": "aeaded7bf5d1c5090c6563cf9ec372a1d1b53bb83d74183e0363706d40407ed6" 22 | } 23 | 24 | decrypt: 25 | 26 | post : http://localhost:5001/decrypt 27 | 28 | { 29 | "encryptedData": "aeaded7bf5d1c5090c6563cf9ec372a1d1b53bb83d74183e0363706d40407ed6" 30 | } 31 | 32 | { 33 | "plainText": "sample application" 34 | } 35 | 36 | ``` 37 | -------------------------------------------------------------------------------- /src/decrypt.symmetric.ts: -------------------------------------------------------------------------------- 1 | import crypto from "node:crypto"; 2 | import { Buffer } from "node:buffer"; 3 | 4 | import CONFIG_ENV from './env/developer.env'; 5 | 6 | const decryptSymmetric = async (encryptedInfo: any) => { 7 | try { 8 | let encryptedText = Buffer.from(encryptedInfo.encryptedData, 'base64'); 9 | const decipher = crypto.createDecipheriv('aes-256-cbc', 10 | Buffer.from(CONFIG_ENV.ENCRYPTION_KEY), 11 | Buffer.from(CONFIG_ENV.ENCRYPTION_KEY.substring(0,CONFIG_ENV.IV_LENGTH))); 12 | const decrypted = decipher.update(encryptedText); 13 | const decryptedText = Buffer.concat([decrypted, decipher.final()]); 14 | return { decryptedData: decryptedText.toString() } 15 | } catch (error) { 16 | console.log(error) 17 | } 18 | }; 19 | 20 | export default decryptSymmetric; -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import http from 'http'; 2 | import cors from "cors"; 3 | import express, { Application, Request, Response } from "express"; 4 | 5 | import decryptSymmetric from "./decrypt.symmetric"; 6 | import encryptSymmetric from './encrypt.symmetric'; 7 | 8 | const app: Application = express(); 9 | 10 | app.use(cors()); 11 | app.use(express.json()); 12 | app.use(express.urlencoded({ extended: true })); 13 | 14 | app.get('/health', (req: Request, res: Response) => { 15 | return res.status(200).json({ 'status': 'heathy' }) 16 | }) 17 | 18 | app.post("/encrypt", async (req: Request, res: Response) => { 19 | const plainText = req.body.plainText; 20 | res.json(await encryptSymmetric(plainText)); 21 | }); 22 | 23 | app.post("/decrypt", async (req: Request, res: Response) => { 24 | const { initVector, encryptedData } = req.body; 25 | res.json(await decryptSymmetric({ initVector, encryptedData })); 26 | }); 27 | 28 | app.use("*", (req: Request, res: Response) => { 29 | return res.status(200).json({ 30 | 'statusCode': 404, 31 | 'statusName': 'NOT_FOUND', 32 | 'statusMessage': 'The requested resource could not be found.' 33 | }) 34 | }); 35 | 36 | export const server = http.createServer(app); 37 | server.listen(5001, () => { console.log(`App listening on port 5001`); }); 38 | -------------------------------------------------------------------------------- /express-crypto.postman_collection.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "_postman_id": "486071ed-c5e0-48a7-ba88-ddaaf150c44f", 4 | "name": "express-crypto", 5 | "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" 6 | }, 7 | "item": [ 8 | { 9 | "name": "http://localhost:5001/encrypt", 10 | "request": { 11 | "method": "POST", 12 | "header": [], 13 | "body": { 14 | "mode": "raw", 15 | "raw": "{\r\n \"plainText\": \"sample application\" \r\n}", 16 | "options": { 17 | "raw": { 18 | "language": "json" 19 | } 20 | } 21 | }, 22 | "url": { 23 | "raw": "http://localhost:5001/encrypt", 24 | "protocol": "http", 25 | "host": [ 26 | "localhost" 27 | ], 28 | "port": "5001", 29 | "path": [ 30 | "encrypt" 31 | ] 32 | } 33 | }, 34 | "response": [] 35 | }, 36 | { 37 | "name": "http://localhost:5001/decrypt", 38 | "request": { 39 | "method": "POST", 40 | "header": [], 41 | "body": { 42 | "mode": "raw", 43 | "raw": "{\r\n \"initVector\": \"b714c118f8453aa8f0dc419fa300582c\",\r\n \"encryptedData\": \"aeaded7bf5d1c5090c6563cf9ec372a1d1b53bb83d74183e0363706d40407ed6\"\r\n}", 44 | "options": { 45 | "raw": { 46 | "language": "json" 47 | } 48 | } 49 | }, 50 | "url": { 51 | "raw": "http://localhost:5001/decrypt", 52 | "protocol": "http", 53 | "host": [ 54 | "localhost" 55 | ], 56 | "port": "5001", 57 | "path": [ 58 | "decrypt" 59 | ] 60 | } 61 | }, 62 | "response": [] 63 | } 64 | ] 65 | } --------------------------------------------------------------------------------