加密/解密在 Base-64 字符串中给出无效字符
我一直在研究 RijndaelManaged 加密/解密算法。
在加密 GUID 时,我们会在加密算法的中间得到“%”符号。 当我解密相同的加密 GUI 时,我收到“Base-64 字符串中的无效字符”错误。 这是由于加密令牌中存在 % 符号而发生的。
这是加密和解密的代码,
using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
public class ABC_RMCryptography
{
public string Decrypt(string strValueIn)
{
try
{
byte[] arrB = System.Convert.FromBase64String(strValueIn);
string strRC;
byte[] key = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionKey"]);
byte[] IV = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionVector"]);
MemoryStream memStream = new MemoryStream(arrB);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(memStream, RMCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read);
StreamReader SReader = new StreamReader(CryptStream);
strRC = SReader.ReadToEnd();
SReader.Close();
return strRC;
}
catch (Exception ex)
{
throw ex;
}
}
public string Encrypt(string strValue)
{
MemoryStream memStream = new MemoryStream();
try
{
byte[] key = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionKey"]);
byte[] IV = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionVector"]);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(memStream, RMCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
StreamWriter SWriter = new StreamWriter(CryptStream);
SWriter.Write(strValue);
SWriter.Close();
CryptStream.Close();
byte[] b = memStream.ToArray();
return Convert.ToBase64String(b);
}
catch (Exception ex)
{
throw ex;
}
}
private string GenerateRMKey()
{
RijndaelManaged objRM = new RijndaelManaged();
objRM.GenerateKey();
return Convert.ToBase64String(objRM.Key);
}
private string GenerateRMIV()
{
RijndaelManaged objRM = new RijndaelManaged();
objRM.GenerateKey();
return Convert.ToBase64String(objRM.IV);
}
}
请让我知道避免此异常的解决方案。 如何避免%符号?
加密/解密在 Base-64 字符串中给出无效字符
I have been working with encryption/decryption RijndaelManaged alogorithm.
While encrypting a GUID we get "%" symbol in the middle of the encrypted algorithm. When I decrypt the same encrypted GUI i get a " Invalid character in a Base-64 string" error. This occurs due to the presence of % symbol in the encrypted token.
Here is the Code for encryption and decryption,
using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
public class ABC_RMCryptography
{
public string Decrypt(string strValueIn)
{
try
{
byte[] arrB = System.Convert.FromBase64String(strValueIn);
string strRC;
byte[] key = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionKey"]);
byte[] IV = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionVector"]);
MemoryStream memStream = new MemoryStream(arrB);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(memStream, RMCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read);
StreamReader SReader = new StreamReader(CryptStream);
strRC = SReader.ReadToEnd();
SReader.Close();
return strRC;
}
catch (Exception ex)
{
throw ex;
}
}
public string Encrypt(string strValue)
{
MemoryStream memStream = new MemoryStream();
try
{
byte[] key = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionKey"]);
byte[] IV = System.Convert.FromBase64String(ConfigurationManager.AppSettings["TokenEncryptionVector"]);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(memStream, RMCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
StreamWriter SWriter = new StreamWriter(CryptStream);
SWriter.Write(strValue);
SWriter.Close();
CryptStream.Close();
byte[] b = memStream.ToArray();
return Convert.ToBase64String(b);
}
catch (Exception ex)
{
throw ex;
}
}
private string GenerateRMKey()
{
RijndaelManaged objRM = new RijndaelManaged();
objRM.GenerateKey();
return Convert.ToBase64String(objRM.Key);
}
private string GenerateRMIV()
{
RijndaelManaged objRM = new RijndaelManaged();
objRM.GenerateKey();
return Convert.ToBase64String(objRM.IV);
}
}
Please let me know the solution to avoid this exception. How do I avoid % symbol?
Encryption/decryption gives Invalid character in a Base-64 string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
%到底来自哪里? 它不应该是
Encrypt
方法返回值的一部分。我猜测是您有一些东西可以对您的加密文本执行 URL 编码。 如果您能告诉我们更多有关 % 的位置以及如何获取该字符串的信息,那将会有很大帮助。
(顺便说一句,您应该对所有流等使用
using
语句,而不是显式关闭它们;而且您的 try/catch 有点毫无意义。不过,这些都是次要问题。)Where exactly is the % coming from? It shouldn't be part of the return value from the
Encrypt
method.My guess is that you've got something performing URL-encoding on your encrypted text. If you could tell us more about where the % is and how you got that string, that would help a lot.
(As an aside, you should use
using
statements for all the streams etc, rather than explicitly closing them; also your try/catch is somewhat pointless. Those are side issues though.)