我有一个需要实现的要求cross platform encryption and decryption
。
所以,我做了一些谷歌搜索,找到了这个链接
因此,我计划使用SharpAESCrypt
并使用1.3.1
它的最新版本。
现在,我的问题是:我能够通过转换为流来加密字符串,但是无法使用相同的密码将其解密。
我的示例代码:
加密
protected void btnEncrypt_Click(object sender, EventArgs e)
{
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(txtInput.Text.Trim());
Byte[] newByteArray;
using (MemoryStream plainText = new MemoryStream(byteArray))
{
using (MemoryStream encryptedData = new MemoryStream())
{
SharpAESCrypt.SharpAESCrypt.Encrypt("ABCD@123", plainText, encryptedData);
newByteArray = encryptedData.ToArray();
}
}
string FinalText = Convert.ToBase64String(newByteArray);
}
catch (Exception ex)
{
}
}
解密方式
protected void btnDecrypt_Click(object sender, EventArgs e)
{
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(txtOutput.Text.Trim());
Byte[] newByteArray;
string output = "";
using (MemoryStream plainText = new MemoryStream(byteArray))
{
using (MemoryStream encryptedData = new MemoryStream())
{
SharpAESCrypt.SharpAESCrypt.Decrypt("ABCD@123", plainText, encryptedData);
newByteArray = encryptedData.ToArray();
}
}
//string FinalText = Convert.ToBase64String(newByteArray);
string FinalText = System.Text.Encoding.UTF8.GetString(byteArray);
}
catch (Exception ex)
{
}
}
我收到类似的错误Invalid header marker
。有人可以让我知道我在做的错误吗?
注意:需要一些有关最佳跨平台(iOS,Android和C#)加密和解密库的建议。
在Decrypt方法中,您需要使用Convert.FromBase64String而不是Encoding.UTF8.GetBytes重新构造加密的字节数组。使用UTF8重构解密的字符串的最后一部分是正确的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句