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

React中对象数据的加密解密及属性合并与拆分实现方案

Got it, let's walk through exactly how to handle this in React—from merging your properties, encrypting the data, to decrypting and splitting it back on the receiver's end. I'll cover both a simple approach and a more robust option for sensitive data!

1. Sender Side: Merge Properties + Encrypt

First, we'll combine the name and message fields into your desired string format, then encrypt the result before sending.

Step 1: Merge the Properties

Create a helper function to format name and message into the string structure you want:

const mergeUserData = (user) => {
  // Follow your specified format: "name&&message:messageContent"
  return `${user.name}&&message:${user.message}`;
};

Step 2: Encrypt the Merged Data

For simple scenarios, you can use Base64 encoding (note: this is not true encryption, just obfuscation). For sensitive data, use a proper encryption library like crypto-js for AES encryption.

Option A: Simple Base64 Encoding

// Encrypt (encode) function
const encryptData = (data) => {
  return btoa(unescape(encodeURIComponent(data)));
};

// Put it all together
const originalUser = { to: 1235, name: "fox", message: "Hi" };
const mergedString = mergeUserData(originalUser);
const encryptedString = encryptData(mergedString);

// Final object to send
const userToSend = {
  to: originalUser.to,
  data: encryptedString
};
// Output: { to: 1235, data: "Zm94JiZtZXNzYWdlOmhp" }

Option B: Secure AES Encryption (For Sensitive Data)

First install the library:

npm install crypto-js

Then implement encryption:

import CryptoJS from 'crypto-js';

// Shared secret key (keep this secure and consistent between sender/receiver)
const SECRET_KEY = 'your-strong-unique-secret-key';

const encryptData = (data) => {
  return CryptoJS.AES.encrypt(data, SECRET_KEY).toString();
};

// Usage is the same as above—merge first, then encrypt
const mergedString = mergeUserData(originalUser);
const encryptedString = encryptData(mergedString);
const userToSend = { to: originalUser.to, data: encryptedString };
2. Receiver Side: Decrypt + Split Back to Original Properties

On receiving the data, we'll first decrypt it, then split the string back into name and message fields.

Step 1: Decrypt the Data

Match the decryption method to the encryption method used:

Option A: Base64 Decoding

const decryptData = (encryptedData) => {
  return decodeURIComponent(escape(atob(encryptedData)));
};

Option B: AES Decryption

import CryptoJS from 'crypto-js';

const SECRET_KEY = 'your-strong-unique-secret-key'; // Same as sender

const decryptData = (encryptedData) => {
  const decryptedBytes = CryptoJS.AES.decrypt(encryptedData, SECRET_KEY);
  return decryptedBytes.toString(CryptoJS.enc.Utf8);
};

Step 2: Split the Decrypted String

Create a helper function to parse the merged string back into individual properties:

const splitUserData = (decryptedString) => {
  // Split the string at "&&message:" to separate name and message
  const [name, message] = decryptedString.split('&&message:');
  return { name, message };
};

// Put it all together to restore the original object
const receivedUser = { to: 1235, data: "Zm94JiZtZXNzYWdlOmhp" }; // Or AES encrypted string
const decryptedString = decryptData(receivedUser.data);
const { name, message } = splitUserData(decryptedString);

const restoredUser = {
  to: receivedUser.to,
  name,
  message
};
// Output: { to: 1235, name: "fox", message: "Hi" }
Pro Tip: More Robust Merging (Avoids Edge Cases)

If name or message might contain the string &&message: (unlikely but possible), use JSON serialization instead—it's more reliable:

// Sender: Merge using JSON
const mergeUserData = (user) => {
  return JSON.stringify({ name: user.name, message: user.message });
};

// Receiver: Split using JSON parse
const splitUserData = (decryptedString) => {
  return JSON.parse(decryptedString);
};

This way, even special characters won't break the splitting logic.

内容的提问来源于stack exchange,提问作者Puneet Biswas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 05:18:19