You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CryptoJS将数字编码为字符并输出Base64格式?

How to modify CryptoJS AES code to output Base64 encoded result for the number '7543275'?

I want to encode the number '7543275' into characters and output it in Base64 format. My current code for AES encryption/decryption using CryptoJS is as follows:

const CryptoJS = require('crypto-js');
function msg() {
 return '7543275'; // I want to encrypt this number to character
}
const msgLocal = msg();
// Encrypt
const ciphertext = CryptoJS.AES.encrypt(msgLocal, 'password');
// Decrypt
const bytes = CryptoJS.AES.decrypt(ciphertext.toString(), 'password');
const plaintext = bytes.toString(CryptoJS.enc.Utf8);
console.log(plaintext);

How can I modify the code to achieve Base64 encoding output?


Hey there! Let's break this down into two clear scenarios based on what you're actually trying to accomplish — since there's a key difference between simple Base64 encoding and AES encryption + Base64 formatting:

Scenario 1: Just Base64 encode the number string (no encryption)

If you only need to convert the number '7543275' into a Base64-formatted string (no security/encryption required), you can skip AES entirely and use CryptoJS's built-in encoding utilities directly:

const CryptoJS = require('crypto-js');

function msg() {
  return '7543275'; // Your number as a string
}
const msgLocal = msg();

// Encode the string to Base64
const base64Encoded = CryptoJS.enc.Utf8.parse(msgLocal).toString(CryptoJS.enc.Base64);
console.log('Base64 Encoded Result:', base64Encoded); // Outputs: NzU0MzI3NQ==

// To decode it back to the original string
const decodedBytes = CryptoJS.enc.Base64.parse(base64Encoded);
const decodedText = decodedBytes.toString(CryptoJS.enc.Utf8);
console.log('Decoded Result:', decodedText); // Outputs: 7543275

Scenario 2: AES encrypt the number, then output the ciphertext as Base64

If you want to secure the number with AES encryption first, then represent that encrypted data as a Base64 string (which aligns with your original code), you're already almost there!

CryptoJS's AES.encrypt() returns a CipherParams object, and calling .toString() on it by default gives you a Base64-encoded string of the encrypted data (including necessary salt/IV info for decryption). You can adjust your code to explicitly log this Base64 ciphertext:

const CryptoJS = require('crypto-js');

function msg() {
  return '7543275';
}
const msgLocal = msg();
const password = 'password';

// Encrypt the string and get Base64 ciphertext
const ciphertext = CryptoJS.AES.encrypt(msgLocal, password);
const base64Ciphertext = ciphertext.toString(); // This is already Base64!
console.log('Encrypted Base64 Result:', base64Ciphertext);

// Decryption remains the same as your original code
const bytes = CryptoJS.AES.decrypt(base64Ciphertext, password);
const plaintext = bytes.toString(CryptoJS.enc.Utf8);
console.log('Decrypted Plaintext:', plaintext); // Outputs: 7543275

Quick note:

  • Base64 is just an encoding scheme (converts binary data to ASCII-safe characters) — it doesn't secure your data. Use AES encryption only if you need to prevent unauthorized access to the number.
  • The ciphertext.toString() output includes metadata (like the salt) that CryptoJS needs for decryption, so avoid extracting just the raw ciphertext unless you're handling salt/IV manually.

内容的提问来源于stack exchange,提问作者Saeed Heidarizarei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:28:20