简体   繁体   English

如何在javascript中加密/解密URL参数?

[英]How to encrypt/decrypt URL parameters in javascript?

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. 有没有办法在javascript中实现这一点。 I have already seen this 我已经看过了

I have already seen this and 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. URL google.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 我需要一个止损解决方案,直到我们有一个更好的架构

You can use base64. 你可以使用base64。 Javascript has native functions to do that : 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

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中的键/值略微混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM