2016-12-08 6 views
0

Infernoを使用してECIESで暗号化されたテキストファイルに署名しようとしています。暗号化と鍵交換の部分はうまくいきますが、ECDSAに関するいくつかの質問が残っています。InfernoのECIES:ECDSAの署名

1-受信者がデータの整合性を検証するために使用するように、ECDSA署名を送信ファイルに保存する必要があります。

2ハッシュされたデータに対して署名を実行する必要があるのは何度も読んでいますが、ECIESで送信されるデータは暗号化されています。

3-ここで、次のコード例で署名を行う必要がありますか?解読時に署名が受け入れられますが、解読されたテキストの代わりに空文字列が返されるため、現在の試行は機能しません。

internal static void EncryptText(string text, Keyring k, string file, bool forSender) 
    { 
     // never mind the Keyring class, it has no methods and only exposes 
     // a few properties to store the session keys conveniently 

     SharedEphemeralBundle ephemeralBundle; 
     if (forSender) ephemeralBundle = k.SenderDHM.GetSharedEphemeralDhmSecret(); 
     else ephemeralBundle = k.ReceiverDHM.GetSharedEphemeralDhmSecret(); 

     var ephemeralPublic = ephemeralBundle.EphemeralDhmPublicKeyBlob; 
     var ephemeralSymmetric = ephemeralBundle.SharedSecret; 
     var textBytes = Utils.SafeUTF8.GetBytes(text); 

     byte[] signature;    
     using (var ecdsa = new ECDsaCng(k.SenderDSA) { HashAlgorithm = CngAlgorithm.Sha384 }) 
      signature = ecdsa.SignData(textBytes); 

     using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write)) 
     { 
      fs.Write(signature, 0, signature.Length); 
      fs.Write(ephemeralPublic, 0, ephemeralPublic.Length); 

      EtM_EncryptTransform etm = new EtM_EncryptTransform(ephemeralSymmetric); 
      using (CryptoStream cs = new CryptoStream(fs, etm, CryptoStreamMode.Write))     
       cs.Write(textBytes, 0, textBytes.Length);         
     }    
    } 

    internal static string DecryptText(string file, Keyring k) 
    { 
     string decrypted = null;    
     var ephemeralPublic = new byte[104]; 
     var signature = new byte[96];    

     using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) 
     { 
      fs.Read(signature, 0, 96); 
      fs.Read(ephemeralPublic, 0, 104); 

      var ephemeralSymmetric = k.SenderDHM.GetSharedDhmSecret(ephemeralPublic.ToPublicKeyFromBlob()); 
      EtM_DecryptTransform etm = new EtM_DecryptTransform(ephemeralSymmetric); 
      using (CryptoStream cs = new CryptoStream(fs, etm, CryptoStreamMode.Read)) 
      { 
       byte[] decrypt = new byte[fs.Length - 200]; 
       cs.Read(decrypt, 0, decrypt.Length); 
       using (var ecdsa = new ECDsaCng(k.SenderDSA)) 
       { 
        // signature is accepted but returns an empty string!? 
        if (ecdsa.VerifyData(decrypt, signature)) 
         decrypted = Utils.SafeUTF8.GetString(decrypt);       
       }       
      }                      
     } 
     return decrypted; 
    } 

答えて

0

気にしないでオーケー、精密検査の後、それは私のシナリオのように見えますが、私は幸いにも一人でHMACに行くことができます:

private static bool Authenticate(string file, byte[] key, bool masterKey = false) 
    { 
     int position = 104; 
     if (masterKey) position = 48; 
     using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read)) 
     using (var etm = new EtM_DecryptTransform(key, authenticateOnly: true)) 
     { 
      fs.Position = position; 
      using (var cs = new CryptoStream(fs, etm, CryptoStreamMode.Read)) 
       cs.CopyTo(Stream.Null); 

      if (!etm.IsComplete) throw new Exception("Some blocks were not authenticated"); 
     } 
     return true; 
    }   

    internal static void EncryptText(string text, Keyring k, string file, bool forSender) 
    { 
     SharedEphemeralBundle ephemeralBundle; 
     if (forSender) ephemeralBundle = k.SenderDHM.GetSharedEphemeralDhmSecret(); 
     else ephemeralBundle = k.ReceiverDHM.GetSharedEphemeralDhmSecret(); 

     var ephemeralPublic = ephemeralBundle.EphemeralDhmPublicKeyBlob; 
     var ephemeralSymmetric = ephemeralBundle.SharedSecret; 
     var textBytes = text.ToBytes();      

     using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write)) 
     {     
      fs.Write(ephemeralPublic, 0, ephemeralPublic.Length); 
      using (var etm = new EtM_EncryptTransform(ephemeralSymmetric)) 
      using (var cs = new CryptoStream(fs, etm, CryptoStreamMode.Write)) 
       cs.Write(textBytes, 0, textBytes.Length);                      
     }    
    } 

    internal static string DecryptText(string file, Keyring k) 
    { 
     string decrypted = null;    
     var ephemeralPublic = new byte[104]; 

     using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read)) 
     {     
      fs.Read(ephemeralPublic, 0, 104); 
      var ephemeralSymmetric = k.SenderDHM.GetSharedDhmSecret(ephemeralPublic.ToPublicKeyFromBlob()); 
      if (Authenticate(file, ephemeralSymmetric)) 
      { 
       using (var etm = new EtM_DecryptTransform(ephemeralSymmetric)) 
       using (var cs = new CryptoStream(fs, etm, CryptoStreamMode.Read)) 
       { 
        var decrypt = new byte[fs.Length - 104]; 
        cs.Read(decrypt, 0, decrypt.Length); 
        decrypted = decrypt.FromBytes(); 
       } 
      }                           
     } 
     return decrypted; 
    }