确保加密的字符串不包含/
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论