在PCL库上的Xamarin上实现加密后,Android可以正常进行加密和解密,但是IOS并没有在服务器上解密,因为我知道我已更改并再次检查了为该设备自动生成的私钥和公钥也可以手动设置它们,但仍然是相同的错误。
用于加密/解密的代码
public static string EncryptFile(string ChannelId, string inputFilePath)
{
try
{
string resultFilePath = "";
var fileInfo = new System.IO.FileInfo(inputFilePath);
resultFilePath = Path.GetDirectoryName(inputFilePath) + @"/" + Guid.NewGuid().ToString().Replace("-", "") + fileInfo.Extension;
using (var symmetricCypher = new AesManaged())
{
// Generate random key and IV for symmetric encryption
var key = new byte[symmetricCypher.KeySize / 8];
var iv = new byte[symmetricCypher.BlockSize / 8];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(key);
rng.GetBytes(iv);
}
// Encrypt the symmetric key and IV
var buf = new byte[key.Length + iv.Length];
Array.Copy(key, buf, key.Length);
Array.Copy(iv, 0, buf, key.Length, iv.Length);
buf = EncryptData(buf, ChannelId);
var bufLen = BitConverter.GetBytes(buf.Length);
// Symmetrically encrypt the data and write it to the file, along with the encrypted key and iv
using (var cypherKey = symmetricCypher.CreateEncryptor(key, iv))
using (var fsIn = new FileStream(inputFilePath, FileMode.Open))
using (var fsOut = new FileStream(resultFilePath, FileMode.Create))
using (var cs = new CryptoStream(fsOut, cypherKey, CryptoStreamMode.Write))
{
fsOut.Write(bufLen, 0, bufLen.Length);
fsOut.Write(buf, 0, buf.Length);
fsIn.CopyTo(cs);
/* var xx = fsOut.ToArray()*/
;
}
File.Delete(inputFilePath);
return resultFilePath;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
public static byte[] EncryptData(byte[] data, string ChannelId)
{
try
{
var publicKey = GetChannelPublicKey(ChannelId);
var encryptEngine = new Pkcs1Encoding(new RsaEngine());
encryptEngine.Init(true, publicKey);
return encryptEngine.ProcessBlock(data, 0, data.Length);
}
catch (Exception eex)
{
Console.WriteLine(eex);
throw;
}
}
public static void DecryptFile(byte[] fileBytes, string outputFilePath, string ChannelId)
{
using (var symmetricCypher = new AesManaged())
using (var fsIn = new MemoryStream(fileBytes))
{
//symmetricCypher.Padding = PaddingMode.None;
// Determine the length of the encrypted key and IV
var buf = new byte[sizeof(int)];
fsIn.Read(buf, 0, buf.Length);
var bufLen = BitConverter.ToInt32(buf, 0);
// Read the encrypted key and IV data from the file and decrypt using the asymmetric algorithm
buf = new byte[bufLen];
fsIn.Read(buf, 0, buf.Length);
buf = DecryptData(buf, ChannelId);
var key = new byte[symmetricCypher.KeySize / 8];
var iv = new byte[symmetricCypher.BlockSize / 8];
Array.Copy(buf, key, key.Length);
Array.Copy(buf, key.Length, iv, 0, iv.Length);
// Decript the file data using the symmetric algorithm
using (var cypherKey = symmetricCypher.CreateDecryptor(key, iv))
using (var fsOut = new FileStream(outputFilePath, FileMode.Create))
using (var cs = new CryptoStream(fsOut, cypherKey, CryptoStreamMode.Write))
{
fsIn.CopyTo(cs);
}
}
}
public static byte[] DecryptData(byte[] data, string ChannelId)
{
try
{
var publicKey = GetServerPrivateKey(ChannelId);
var encryptEngine = new Pkcs1Encoding(new RsaEngine());
encryptEngine.Init(false, publicKey);
return encryptEngine.ProcessBlock(data, 0, data.Length);
}
catch (Exception eex)
{
Console.WriteLine(eex);
throw;
}
}
从iOS设备发送邮件时,服务器出现错误。
使用的密钥长度为1024。
任何想法都可能导致这种问题。
经过更多调试后,我注意到该文件的接收字节大小不同。 [字节大小为1985940,发送时为1985942,接收到此消息] ,并且该文件仅在IOS设备上
我们正在使用crossfileuploader传输文件