反转编码

时间:2016-10-13 16:10:20

标签: node.js function cookies buffer

我设法将base64json编码的字符串转换为它的json格式,但我无法弄清楚如何反转函数

var jsonEncodedString = 'eyJ1c2VybmFtZSI6InNhbSIsImtleSI6IkhNZUs5WE1sZW9DbTlhWmJrTWZSVEZsNThRVTNWbFhlYURlaktRVnN6UzJvWG45YVRVMlJ5VGVySHVqdVhRcHNqRzA5NjZXelRQT0hwVm95bTMyL05nY0toN085elhhV1VmZFlNV1NEUUNra1BMWHU0eUJGNWFLdFE3Umd4eTFNN0pXakE4a3d5a1F rNTFPdE8yRGVWMklFYnBrbDZPV00rSGRKUmFBNk1IYW1IU0srYVdUOHQ3SGM3QkJudXBIWFhqWkNFa3pDY290Tk5COUwzMjNsR3VMSktoL2VvTU1zL1IyZmtzaTJONm5sUk95SFNYWjVpNzhyN2ZvY2l1OHZKajQ4cnhBOG9UNFZ3OERnTVJoZERXczc0M1dhUHJJUWh3MU95WjBQQVBDTzNxZUpicW1XZzFZaFl KZmlQTzd1VW1mN2hlMmdlbWFIU09CVlkxWloxUT09In0=; HttpOnly; SameSite=Strict';

var parsedString = JSON.parse((Buffer.from(jsonEncodedString, 'base64')).toString());

console.log(parsedString);

// Outputs

// {
//   username: 'sam',
//   key: 'HMeK9XMleoCm9aZbkMfRTFl58QU3VlXeaDejKQVszS2oXn9aTU2RyTerHujuXQpsjG0966WzTPOHpVoym32/NgcKh7O9zXaWUfdYMWSDQCkkPLXu4yBF5aKtQ7Rgxy1M7JWjA8kwykQk51OtO2DeV2IEbpkl6OWM+HdJRaA6MHamHSK+aWT8t7Hc7BBnupHXXjZCEkzCcotNNB9L323lGuLJKh/eoMMs/R2fksi2N6nlROyHSXZ5i78r7fociu8vJj48rxA8oT4Vw8DgMRhdDWs743WaPrIQhw1OyZ0PAPCO3qeJbqmWg1YhYJfiPO7uUmf7he2gemaHSOBVY1ZZ1Q=='
// }

我想要一个能够获取parsedString并输出jsonEncodedString

的函数

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

看起来它只是像逻辑一样的代数:

解决方案:

console.log(Buffer.from(JSON.stringify(parsedString)).toString('base64'));

提供原始