【问题标题】:NSData.FromStream() - filesize doubles and files are corruptedNSData.FromStream() - 文件大小翻倍并且文件已损坏
【发布时间】:2011-02-03 10:43:19
【问题描述】:

我正在尝试使用以下代码即时加密文件:

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”,它根本不应该更改文件,我得到相同的结果。

知道发生了什么吗?

【问题讨论】:

    标签: xamarin.ios


    【解决方案1】:

    这样的事情不会有帮助吗?尚未在 monotouch 上尝试过此操作,但 mtouch 堆栈中包含 AFAIK 所需的命名空间

    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 和 iPhone 3GS+ 内置了硬件加密,我为什么要这样做?
    • 呵呵呵呵是的,我知道,但如果您急于将应用程序投入生产,作为一种解决方法:) 我测试了您的代码,是的,我的文件也损坏了,我想我们应该提交一个错误
    • 我曾尝试在这个奇怪的 Novell 网站上提交错误,但失败了。如果您有经验,请提交错误!
    • 我设法放置了一个错误!我得到了几个 404,被重定向到最奇怪的页面,但最后我做到了!是的!让我们看看会发生什么。
    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 2013-08-15
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2023-04-08
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多