首页 > 解决方案 > 如何在 Reactjs 中加密和解密对象数据

问题描述

我有一个具有三个属性的对象to name message我想加密我想发送name并且message只有一个属性值。如何从接收方name返回message

// 我有这样的对象

const user={
to:1235,
name:"fox"
message:"Hi"
}

//我想这样发送

const user={
    to:1235,
    name:"fox&&message:"hii",
    }

//我怎样才能得到名字和消息

标签: javascriptreactjsjavascript-objects

解决方案


您可以通过使用字符串操作轻松解密。首先,您将获得namePost 参数中包含的数据。假设我们在服务器端有这样的数据。

let encoded_data = "alice&message:hello world"

然后我们将这个字符串拆分为&

let split_data = encoded_data.split('&')
let name = split_data[0] //this contains value alice

现在将拆分 split_data[1] 其中包含message:hello world

let message = split_data[1].split(':')[1]

推荐阅读