Xamarin.ios NSData.FromStream()-文件大小加倍,文件已损坏
我正在尝试使用以下代码动态加密文件:Xamarin.ios NSData.FromStream()-文件大小加倍,文件已损坏,xamarin.ios,Xamarin.ios,我正在尝试使用以下代码动态加密文件: NSError oError = null; using ( FileStream oStream = File.Open ( sSourcePathAndFile, FileMode.Open ) ) { NSData oData = NSData.FromStream ( oStream ); // Save and encrypt. oData.Save ( sDestPathAndFile, NSDataWritingOptions.Fi
NSError oError = null;
using ( FileStream oStream = File.Open ( sSourcePathAndFile, FileMode.Open ) )
{
NSData oData = NSData.FromStream ( oStream );
// Save and encrypt.
oData.Save ( sDestPathAndFile, NSDataWritingOptions.FileProtectionAlways, out oError );
}
没有错误,但所有保存的文件大小大约都增加了一倍,并且已损坏。即使我设置了写入选项“FileProtectionNone”,这根本不会改变文件,我也会得到相同的结果
知道发生了什么事吗?像这样的东西会有帮助吗?还没有在monotouch上尝试过这一点,但mtouch堆栈中包含了一个AIK所需的名称空间
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
// Function to Generate a 64 bits Key.
static string GenerateKey()
{
// Create an instance of Symetric Algorithm. Key and IV is generated automatically.
DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
// Use the Automatically generated key for Encryption.
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
static void EncryptFile(string sInputFilename,
string sOutputFilename,
string sKey)
{
FileStream fsInput = new FileStream(sInputFilename,
FileMode.Open,
FileAccess.Read);
FileStream fsEncrypted = new FileStream(sOutputFilename,
FileMode.Create,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted,
desencrypt,
CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}
static void DecryptFile(string sInputFilename,
string sOutputFilename,
string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
//Create a file stream to read the encrypted file back.
FileStream fsread = new FileStream(sInputFilename,
FileMode.Open,
FileAccess.Read);
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
//Create crypto stream set to read and do a
//DES decryption transform on incoming bytes.
CryptoStream cryptostreamDecr = new CryptoStream(fsread,
desdecrypt,
CryptoStreamMode.Read);
//Print the contents of the decrypted file.
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}
static void Main()
{
// Must be 64 bits, 8 bytes.
// Distribute this key to the user who will decrypt this file.
string sSecretKey;
// Get the Key for the file to Encrypt.
sSecretKey = GenerateKey();
// For additional security Pin the key.
GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );
// Encrypt the file.
EncryptFile(@"C:\MyData.txt",
@"C:\Encrypted.txt",
sSecretKey);
// Decrypt the file.
DecryptFile(@"C:\Encrypted.txt",
@"C:\Decrypted.txt",
sSecretKey);
}
也许吧,但如果iOS和iPhone3GS+内置了硬件加密,我为什么要这么做呢?呵呵,是的,我知道,但如果你急于将应用程序投入生产,作为一种解决办法:)我测试了你的代码,是的,我的文件也被破坏了,我想我们应该提交一个错误。我曾经尝试在这个奇怪的Novell网站上提交一个错误,但失败了。如果你有经验,请提交一个错误!我设法放了一只虫子!我得到了一些404,被重定向到最奇怪的页面,但最后我做到了!是 啊让我们看看会发生什么。