javascript 如何在javascript中加密/解密URL参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19819407/
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
How to encrypt/decrypt URL parameters in javascript?
提问by Raghav
Before anyone jumps in and says, "Oh!! that's a bad idea", I know it is.
在任何人跳进来说“哦!!那是个坏主意”之前,我知道它是。
I want to keep both the key and value in the query string to be not easily visible to the end user.
I have something like this google.com/?category=textile&user=user1
I need to make it unintelligible like this: google.com/?kasjdhfkashasdfsf32423
我想让查询字符串中的键和值都不容易被最终用户看到。我有这样的事情google.com/?category=textile&user=user1
我需要让它像这样难以理解:google.com/?kasjdhfkashasdfsf32423
Is there any way to achieve this in javascript. I have already seen this
有什么办法可以在javascript中实现这一点。我已经看过这个
I have already seen thisand this.
but I don't think encoding will solve the problem. Also, this code is entirely in client side. I know that it is not secure but I just need this is a naive, weak defense. Please help.
但我认为编码不会解决问题。此外,此代码完全在客户端。我知道它不安全,但我只需要这是一种幼稚、薄弱的防御。请帮忙。
Edit
编辑
I apologize if my question was not clear earlier.
如果我的问题之前不清楚,我深表歉意。
The URL google.com/?category=textile&user=user1
is being passed on from a different application.
URLgoogle.com/?category=textile&user=user1
是从不同的应用程序传递的。
The values passed in the query string directly controls what is being displayed to the user. As is, anyone with no technical knowledge can easily change the value and view the data corresponding to a different category or user. I need to make this unintelligible so that it is not obvious. If a user is a techie and figures out the encryption used, then it is fine. I need a stop-gap solution till we have a better architecture in place
查询字符串中传递的值直接控制向用户显示的内容。照原样,任何没有技术知识的人都可以轻松更改值并查看与不同类别或用户对应的数据。我需要让这个难以理解,这样它就不明显了。如果用户是技术人员并且弄清楚所使用的加密,那么这很好。我需要一个权宜之计,直到我们有了更好的架构
采纳答案by olegarch
If you don't looking for serious strong crypto, you can use ROT13:
如果您不寻找严肃的强加密,您可以使用 ROT13:
http://en.wikipedia.org/wiki/ROT13
http://en.wikipedia.org/wiki/ROT13
This is enough for slightly obfuscate keys/values in the your URLs.
这对于稍微混淆 URL 中的键/值就足够了。
回答by Sebastien C.
You can use base64. Javascript has native functions to do that :
您可以使用 base64。Javascript 具有执行此操作的本机函数:
alert(btoa("category=textile&user=user1")); // ==> Y2F0ZWdvcnk9dGV4dGlsZSZ1c2VyPXVzZXIx
and to reverse it :
并扭转它:
alert(atob("Y2F0ZWdvcnk9dGV4dGlsZSZ1c2VyPXVzZXIx")); // ==> category=textile&user=user1
Be careful to read the doc if you have unicode strings, it's a little different : https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa
如果您有 unicode 字符串,请小心阅读文档,它有点不同:https: //developer.mozilla.org/en-US/docs/Web/API/Window.btoa