2015-10-30 64 views
1

我有用AesCryptoServiceProvider加密的磁盤上的文件。 我需要將代碼轉換爲PCL(它沒有類AesCryptoServiceProvider)。所以我想用Bouncy Castle(https://www.nuget.org/packages/Portable.BouncyCastle/)來讀取和保存文件而不改變加密。並防止需要轉換所有文件。將AesCryptoServiceProvider的加密轉換爲BouncyCastle

Key是32個字節長。 IV長16字節。

下面是原來的代碼:

private static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) 
{ 
    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) 
    { 
     aesAlg.Key = Key; 
     aesAlg.IV = IV; 

     ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 
     using (MemoryStream outputStream = new MemoryStream()) 
     { 
      using (CryptoStream csEncrypt = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write)) 
      { 
       using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
       { 
        swEncrypt.Write(plainText); 
       } 
      } 

      return outputStream.ToArray(); 
     } 
    } 
} 

AesCryptoServiceProvider默認模式是CBCPKCS7類型的填充,然後我試圖與類CbcBlockCipherPkcs7Padding。這是我最後一次嘗試:

private static byte[] Encrypt(byte[] input, byte[] key, byte[] IV) 
{ 
    var keyParameter = new KeyParameter(key); 
    var parameters = new ParametersWithIV(keyParameter, IV); 

    var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding()); 

    cipher.Init(true, parameters); 

    var rsl = new byte[cipher.GetOutputSize(input.Length)]; 
    var outOff = cipher.ProcessBytes(input, 0, input.Length, rsl, 0); 

    cipher.DoFinal(rsl, outOff); 
    return rsl; 
} 

原來的結果爲文本「測試」是8jykPv2OSILKjJwvgLRr1w==,但隨着新的代碼是BaYeiuWSnRiHXBpwt19Dag==

那我想念什麼?

感謝您的幫助!

更新#1: 這裏是鍵和值IV:

private static byte[] KEY = {98, 193, 95, 78, 211, 151, 118, 57, 179, 5, 85, 181, 133, 20, 94, 101, 184, 175, 94, 164, 150, 119, 75, 207, 189, 178, 21, 213, 13, 217, 174, 44}; 
private static byte[] IV = {1, 199, 179, 189, 160, 220, 229, 238, 179, 14, 255, 147, 187, 49, 179, 134 }; 

更新#2: 對於充氣城堡,將我plainTextinput這一行:

var input = Encoding.Unicode.GetBytes(plainText); 

差異來自那裏...

+1

什麼是關鍵和IV? –

+0

@JamesKPolk:增值! – Kipotlov

+0

您能借此機會通過安全加密取代加密嗎?由於它使用了一個常量IV(完全忽略了IV的要點)並且缺少一個MAC,所以現有的比較弱。 – CodesInChaos

回答

0

調用使用BouncyCastle的加密()方法之前,我將我串Encoding.Unicode.GetBytes(plainText);代替Encoding.UTF8.GetBytes(plainText);

用此修復程序,無論方法給出相同的結果!

0

問題是編碼的Streamwriter。寫入CryptoStream的時候它帶來的相同的結果:

[TestMethod] 
    public void windows() 
    { 

     byte[] KEY = { 98, 193, 95, 78, 211, 151, 118, 57, 179, 5, 85, 181, 133, 20, 94, 101, 184, 175, 94, 164, 150, 119, 75, 207, 189, 178, 21, 213, 13, 217, 174, 44 }; 
     byte[] IV = { 1, 199, 179, 189, 160, 220, 229, 238, 179, 14, 255, 147, 187, 49, 179, 134 }; 

     byte[] input = System.Text.Encoding.UTF8.GetBytes("test"); 

     string win = Convert.ToBase64String(Encrypt_win(input, KEY, IV)); 

     string bc = Convert.ToBase64String(Encrypt_bc(input, KEY, IV)); 



     Assert.AreEqual(win, "8jykPv2OSILKjJwvgLRr1w=="); 
     Assert.AreEqual(bc, "8jykPv2OSILKjJwvgLRr1w=="); 

    } 


    //private static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) 
    private static byte[] Encrypt_win(byte[] input, byte[] Key, byte[] IV) 
    { 
     using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) 
     { 
      aesAlg.Key = Key; 
      aesAlg.IV = IV; 


      ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 
      using (MemoryStream outputStream = new MemoryStream()) 
      { 
       using (CryptoStream csEncrypt = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write)) 
       { 
        csEncrypt.Write(input, 0, input.Length); 

        /* 
        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
        { 
         //swEncrypt.Write(plainText); 
         swEncrypt.Write(input); 
        } 
        */ 
       } 

       return outputStream.ToArray(); 
      } 
     } 
    } 



    private static byte[] Encrypt_bc(byte[] input, byte[] key, byte[] IV) 
    { 
     var keyParameter = new KeyParameter(key); 
     var parameters = new ParametersWithIV(keyParameter, IV); 

     var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding()); 

     cipher.Init(true, parameters); 

     var rsl = new byte[cipher.GetOutputSize(input.Length)]; 
     var outOff = cipher.ProcessBytes(input, 0, input.Length, rsl, 0); 

     cipher.DoFinal(rsl, outOff); 
     return rsl; 
    } 
+0

感謝您的回答。問題不在於StreamWriter。但你的回答幫助我發現問題!我用'Encoding.Unicode.GetBytes(plainText);'而不是'Encoding.UTF8.GetBytes(plainText);'來轉換我的字符串;'' – Kipotlov