В мире, где данные являются валютой, а кибератаки с каждым днем становятся все более изощренными, важность. В распоряжении организаций есть три важнейших инструмента для обеспечения надежной кибербезопасности: шифрование, аутентификация и авторизация. Шифрование — один из самых полезных уровней первого уровня для защиты ваших данных и обеспечения устойчивости к киберугрозам.
В этом блоге вы узнаете, как зашифровать ответ данных и показать расшифрованные данные на своем веб-сайте.
Прежде всего, вам необходимо настроить внутренний и внешний код. Для настройки вам понадобятся следующие зависимости.
npm install crypto-js
В бэкэнде Aes Encrypt Util class
создана для настройки AES и шифрования данных в зашифрованный текст.
const CryptoJS = require('crypto-js'); export class AesEncryptUtil { static publicKey: string; static getAesPublicKey() { if (AesEncryptUtil.publicKey) return AesEncryptUtil.publicKey; AesEncryptUtil.publicKey = 'here you right secret key'; return AesEncryptUtil.publicKey; } static aesEncrypt(data: any) { try { let pKey = AesEncryptUtil.getAesPublicKey(); const ciphertext = CryptoJS.AES.encrypt( JSON.stringify(data), pKey, ).toString(); return ciphertext; } catch (error) { throw error; } } }
Здесь показано, как зашифровать ответ данных.
import { AesEncryptUtil } from 'src/util/util.aes.encrypt'; @Injectable() export class PayService { async checkout(programPriceId: number) { try { const order = await instance.orders.create(options); let encryptData = AesEncryptUtil.aesEncrypt(order); return { statusCode: 200, isSuccess: true, data: encryptData }; } catch (error) { return { statusCode: 500, isSuccess: false, error: error }; } } }
На стороне внешнего интерфейса Aes Decrypt Util Class
создан для настройки AES и расшифровки зашифрованного текста в исходные данные.
const CryptoJS = require("crypto-js"); export class AesDecryptUtil { static publicKey; static getAesKey() { if (AesDecryptUtil.publicKey) return AesDecryptUtil.publicKey; AesDecryptUtil.publicKey = 'here you right secret key'; return AesDecryptUtil.publicKey; } static aesDecrypt(data) { try { let pKey = AesDecryptUtil.getAesKey(); const bytes = CryptoJS.AES.decrypt(data, pKey); const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); return decryptedData; } catch (error) { throw error; } } }
Здесь показано, как расшифровывать зашифрованный текст.
import { AesDecryptUtil } from "../utils/aes.util"; export async function getStaticPaths() { try { const response = await axios.get(`${config.url}/programs/getall`); let { data } = response; data = AesDecryptUtil.aesDecrypt(data); return {data: data}; } catch (err) { return { error: err}; } }
Спасибо за чтение!.
Дайте мне знать, если у вас есть какие-либо отзывы или комментарии, или если вы столкнулись с какими-либо проблемами.