不使用密钥的简单 JavaScript 加密和解密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16605769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:18:48  来源:igfitidea点击:

Simple JavaScript encryption & decryption without using key

javascriptencryption

提问by blasteralfred Ψ

I would like to know if it is possible to encrypt and decrypt a text, using pure JavaScript. I don't want to use a key. It may be an entry lever solution. But I simply want to encode a text "my-name-1" into some text format and want to retrieve the text from it. Is this possible, without using any js libraries?

我想知道是否可以使用纯 JavaScript 加密和解密文本。我不想使用 key。它可能是一种入门杠杆解决方案。但我只是想将文本“my-name-1”编码为某种文本格式,并想从中检索文本。在不使用任何 js 库的情况下,这可能吗?

回答by Sirko

Without a key (or some secret for that matter), you wont get any kind of encryption.

如果没有密钥(或与此相关的一些秘密),您将无法获得任何类型的加密。

What you mean is something like a different encoding. So maybe Base64 is something for you.

你的意思是类似于不同的编码。所以也许 Base64 适合你。

var baseString = 'my-name-1';

var encodedString = window.btoa( baseString ); // returns "bXktbmFtZS0x"

var decodedString = window.atob( encodedString );  // returns "my-name-1"

This is supported in all major browsers. IE support just in IE10+.

在所有主要浏览器中都受支持。仅在 IE10+ 中支持 IE。

References:

参考: