确保加密的字符串不包含/

发布于 2025-01-14 06:58:14 字数 1057 浏览 0 评论 0原文

public static string Encryptor(string plainText, string EncryptionKey)
    {
        byte[] clearBytes = Encoding.Unicode.GetBytes(plainText);

        Aes encryptor = Aes.Create();
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);

        MemoryStream ms = new MemoryStream();
        using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();
        }
        plainText = Convert.ToBase64String(ms.ToArray());

        return plainText;
    }

这是我用来确保文件名安全的代码,但问题是有时诸如 / 之类的字符对于文件创建来说是不可接受的,如果我想打开它,则 System.IO.FileStream/ 后面的部分检测为显然不存在的文件夹。

我对 Aes 还很陌生,那么我该怎么做才能确保 / 不会出现在加密代码中呢?

public static string Encryptor(string plainText, string EncryptionKey)
    {
        byte[] clearBytes = Encoding.Unicode.GetBytes(plainText);

        Aes encryptor = Aes.Create();
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);

        MemoryStream ms = new MemoryStream();
        using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();
        }
        plainText = Convert.ToBase64String(ms.ToArray());

        return plainText;
    }

this is the code I use to make filenames safe but the problem is that sometimes characters such as / which is not acceptable for file creating and if I want to open it, System.IO.FileStream detects the part behind / as a folder which clearly doesn't exist.

I'm fairly new to Aes So what can I do to make sure / will not appear in the ciphered code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文