2017-09-06 3 views
5

私はAkavacheを使用してデータをキャッシュし、データのサーバーへの不要な要求の数を減らすXamarin.iOSアプリケーションを使用しています。 BlobCache.LocalMachineを使用してすべてが期待通りにうまくいっています。Xamarin.iOS/Akavache - カスタムEncryptionProviderを使用した暗号化キャッシュ

ここでは、データが暗号化されていることを確認したいと思います。これを調べてオンラインでドキュメントを読むと、デフォルトの実装では実際にiOSのキャッシュは暗号化されず、実際にはIEncryptionProviderのカスタム実装を提供する必要があることがわかりました。

私はこの記事に続い:このスレッドからhttp://kent-boogaart.com/blog/password-protected-encryption-provider-for-akavache

を追加しまし入力:https://github.com/akavache/Akavache/issues/190

そして、この実装になってしまった:呼び出す前 Locator.CurrentMutable.RegisterConstant(new MyEncryptionProvider(), typeof(IEncryptionProvider));

public interface IPasswordProtectedEncryptionProvider : IEncryptionProvider 
    { 
     void SetPassword(string password); 
    } 

    public sealed class MyEncryptionProvider : IPasswordProtectedEncryptionProvider 
    { 
     static readonly byte[] salt = Encoding.ASCII.GetBytes("dVBZMQWyFRcJOIas"); 
     readonly IScheduler scheduler; 
     readonly SymmetricAlgorithm symmetricAlgorithm; 
     ICryptoTransform decryptTransform; 
     ICryptoTransform encryptTransform; 

     public MyEncryptionProvider() 
     { 
      scheduler = BlobCache.TaskpoolScheduler ?? throw new ArgumentNullException(nameof(scheduler), "Scheduler instance is null"); 
      symmetricAlgorithm = new RijndaelManaged(); 
      var securePassword = "kadhaskdhsakhaskjdhaskjdhaskdjashdkjahkfghkjhew"; 
      SetPassword(securePassword); 
     } 

     public void SetPassword(string password) 
     { 
      if(password == null) 
       throw new ArgumentNullException(nameof(password), "password can't be null"); 

      var derived = new Rfc2898DeriveBytes(password, salt); 
      var bytesForKey = symmetricAlgorithm.KeySize/8; 
      var bytesForIV = symmetricAlgorithm.BlockSize/8; 
      symmetricAlgorithm.Key = derived.GetBytes(bytesForKey); 
      symmetricAlgorithm.IV = derived.GetBytes(bytesForIV); 
      decryptTransform = symmetricAlgorithm.CreateDecryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV); 
      encryptTransform = symmetricAlgorithm.CreateEncryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV); 
     } 

     public IObservable<byte[]> DecryptBlock(byte[] block) 
     { 
      if (block == null) 
      { 
       throw new ArgumentNullException(nameof(block), "block can't be null"); 
      } 

      if (decryptTransform == null) 
      { 
       return Observable.Throw<byte[]>(new InvalidOperationException("You must call SetPassword first.")); 
      } 

      return Observable.Start(() => InMemoryTransform(block, decryptTransform), scheduler); 
     } 

     public IObservable<byte[]> EncryptBlock(byte[] block) 
     { 
      if (block == null) 
      { 
       throw new ArgumentNullException(nameof(block), "block can't be null"); 
      } 

      if (encryptTransform == null) 
      { 
       return Observable.Throw<byte[]>(new InvalidOperationException("You must call SetPassword first.")); 
      } 

      return Observable.Start(() => InMemoryTransform(block, encryptTransform), scheduler); 
     } 

     static byte[] InMemoryTransform(byte[] bytesToTransform, ICryptoTransform transform) 
     { 
      using (var memoryStream = new MemoryStream()) 
      { 
       using (var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write)) 
       { 
        cryptoStream.Write(bytesToTransform, 0, bytesToTransform.Length); 
       } 

       return memoryStream.ToArray(); 
      } 
     } 
    } 

を、私はこのように、この実装を登録BlobCache.Secure

その安全なキャッシュに.GetOrFetchObjectを使用すると、EncryptBlockDecryptBlockが呼び出されます。ここまでは順調ですね。同じオブジェクトが正常にこのように保存されているBlobCache.LocalMachine同じデータ構造を使用してキャッシュされている場合は

{Newtonsoft.Json.JsonReaderException: Read past end of current container context. Path ''. 
    at Newtonsoft.Json.Bson.BsonReader.ReadNormal() [0x0013c] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.Bson.BsonReader.Read() [0x00033] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.JsonReader.ReadAndAssert() [0x00000] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x000b6] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x0006d] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00053] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Newtonsoft.Json.JsonSerializer.Deserialize[T] (Newtonsoft.Json.JsonReader reader) [0x00000] in <c19705166c7c4a608e182e859c4de6d2>:0 
    at Akavache.Sqlite3.SQLitePersistentBlobCache.DeserializeObject[T] (System.Byte[] data) [0x00074] in <67aced6c5c1a4c15b03e120d7300429d>:0 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.12.0.20/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151 
    at System.Reactive.PlatformServices.ExceptionServicesImpl.Rethrow (System.Exception exception) [0x00006] in <427ee2007f4d40bb9d3f1fcd73e9b750>:0 
    at System.Reactive.ExceptionHelpers.ThrowIfNotNull (System.Exception exception) [0x0000d] in <ab76b1b1678341d69f8fc2c1f68d0bf5>:0 
    at System.Reactive.Subjects.AsyncSubject`1[T].GetResult() [0x00039] 

を::

私はに実行し、問題は、それがNewtonsoftからこのエラーをデシリアライズしようとすると失敗するということです

私はここで何が欠けていますか?非常に欲求不満、本当に誰かが私が見ていないものを見て欲しい。ご協力いただきありがとうございます。この記事では例の助けを借りて

+1

誰かがここに到着した場合のサンプルで他の問題を参照するだけですhttps://stackoverflow.com/questions/46104014/xamarin-ios-akavache-working-example –

+0

この記事の例の助けを借りて: https://stackoverflow.com/questions/46104014/xamarin-ios-akavache-wo rking-example 私はこの問題を解決することができ、実際のサンプルはここのサンプルソリューションに表示されています:https://github.com/dmitrysamuylov/xamarin-ios-akavache-secure-example –

答えて

関連する問題