gpt4 book ai didi

c# - 在 C# 中将字符串加密为 URL

转载 作者:太空狗 更新时间:2023-10-29 21:30:11 26 4
gpt4 key购买 nike

几个小时以来,我一直在寻找将字符串加密为 URL 的完美方法。这是我找到的方法

Best method I found and it's on stackoverflow

    using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;

public class SimplerAES
{
private static byte[] key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
private static byte[] vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 221, 112, 79, 32, 114, 156 };
private ICryptoTransform encryptor, decryptor;
private UTF8Encoding encoder;

public SimplerAES()
{
RijndaelManaged rm = new RijndaelManaged();
encryptor = rm.CreateEncryptor(key, vector);
decryptor = rm.CreateDecryptor(key, vector);
encoder = new UTF8Encoding();
}

public string Encrypt(string unencrypted)
{
return Convert.ToBase64String(Encrypt(encoder.GetBytes(unencrypted)));
}

public string Decrypt(string encrypted)
{
return encoder.GetString(Decrypt(Convert.FromBase64String(encrypted)));
}

public string EncryptToUrl(string unencrypted)
{
return HttpUtility.UrlEncode(Encrypt(unencrypted));
}

public string DecryptFromUrl(string encrypted)
{
return Decrypt(HttpUtility.UrlDecode(encrypted));
}

public byte[] Encrypt(byte[] buffer)
{
MemoryStream encryptStream = new MemoryStream();
using (CryptoStream cs = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
return encryptStream.ToArray();
}

public byte[] Decrypt(byte[] buffer)
{
MemoryStream decryptStream = new MemoryStream();
using (CryptoStream cs = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
return decryptStream.ToArray();
}
}

现在,当我查看用

生成的加密字符串时
    var a1 = _aes.EncryptToUrl("aaa");
var a2 = _aes.EncryptToUrl("aaaa");
var a3 = _aes.EncryptToUrl("aaaaa");
var a4 = _aes.EncryptToUrl("aaaaaa");
var a5 = _aes.EncryptToUrl("aaaaaaa");
var a6 = _aes.EncryptToUrl("aaaaaaaa");
var a7 = _aes.EncryptToUrl("aaaaaaaaa");
var a8 = _aes.EncryptToUrl("aaaaaaaaaa");
var a9 = _aes.EncryptToUrl("aaaaaaaaaaa");
var a10 = _aes.EncryptToUrl("aaaaaaaaaaa");
var a11 = _aes.EncryptToUrl("aaaaaaaaaaaa");
var a12 = _aes.EncryptToUrl("aaaaaaaaaaaaa");
var a13 = _aes.EncryptToUrl("aaaaaaaaaaaaaa");

所有字符串(var a1 到 a13)都以“%3d%3d”结尾

由于节省存储空间对于此应用程序非常重要,我想知道我是否可以删除“%3d%3d”并稍后在解码之前添加它以返回到原始 URL。

任何人都可以给我一些建议。

谢谢,

最佳答案

正如安德鲁所说,这是 base64 末尾的填充。但是,我不同意他的结论。

我们知道,一个格式正确的 base64 字符串的长度总是 4 个字符的倍数。因此,只要我们相信我们一次性获得了所有数据(即我们相信我们的 URL 没有被截断),我们就可以在为网址。但是,在稍后调用 Convert.FromBase64String 之前,我们需要将这些字符放回。幸运的是,我们可以做到这一点,因为我们知道根据我们得到的字符串的长度会有多少。即使原始长度不是常数,你也可以这样做,即使在你的情况下它是。

另一个小问题 - “普通”base64 字母表不适合 URL;您可能想使用替代版本,如 described in Wikipedia :

Using standard Base64 in URL requires encoding of '+', '/' and '=' characters into special percent-encoded hexadecimal sequences ('+' = '%2B', '/' = '%2F' and '=' = '%3D'), which makes the string unnecessarily longer. For this reason, a modified Base64 for URL variant exists, where no padding '=' will be used, and the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_', so that using URL encoders/decoders are no longer necessary and have no impact on the length of the encoded value, leaving the same encoded form intact for use in relational databases, web forms, and object identifiers in general.

不知道 .NET 中有什么东西可以让这件事轻松高效地完成。最简单的方法是进行正常的base64转换,将+替换为-,/替换为_,然后使用string.TrimEnd将末尾的=符号去掉,用解码的反向操作。这比手动编码版本的效率要低得多,但考虑到您将要处理的数据的大小,这可能无关紧要。

关于c# - 在 C# 中将字符串加密为 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6361176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com