2016-10-12 68 views
0

目前,我有這個算法在C#/。NET:as3crypto加密不匹配,C#/。NET

private static byte[] key = { }; 
    private static readonly byte[] IV = { 20, 52, 88, 120, 76, 89, 205, 239 }; 
    private static readonly string sEncryptionKey = "abcdefgh"; 

    public static string Encrypt(string stringToEncrypt) 
    { 
     try 
     { 
      Debug.WriteLine("stringToEncrypt: " + stringToEncrypt); 
      key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8)); 
      Debug.WriteLine("key: " + Convert.ToBase64String(key)); 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
      //var des = new AesCryptoServiceProvider(); 
      des.Padding = PaddingMode.Zeros; 
      byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt); 
      Debug.WriteLine("inputByteArray: " + Convert.ToBase64String(inputByteArray)); 
      MemoryStream ms = new MemoryStream(); 
      CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write); 
      Debug.WriteLine("IV: " + Convert.ToBase64String(IV)); 
      cs.Write(inputByteArray, 0, inputByteArray.Length); 
      cs.FlushFinalBlock(); 
      Debug.WriteLine("Convert.ToBase64String(ms.ToArray()): " + Convert.ToBase64String(ms.ToArray())); 
      return Convert.ToBase64String(ms.ToArray()); 
     } 
     catch (Exception e) 
     { 
      return e.Message; 
     } 
    } 

的問題是,我試圖讓這個算法的結果相匹配的結果我的ActionScript使用as3crypto DES:

protected function encrypt(input:String):String 
    { 
     var logData:Object = new Object(); 

     var decrKey:String = new String("abcdefgh"); 
     // byte[] IV = { 20, 52, 88, 120, 76, 89, 205, 239 }; 
     var iv:ByteArray = new ByteArray(); 
     iv.writeByte(20); 

     iv.writeByte(52); 

     iv.writeByte(88); 

     iv.writeByte(120); 

     iv.writeByte(76); 

     iv.writeByte(89); 

     iv.writeByte(205); 

     iv.writeByte(239); 

     iv.position = 0; 
     trace("iv: " + iv);   

     //var decrIV:String = iv.readUTF(); 
     var decrIV:String = new String(); 
     while (iv.bytesAvailable > 0) { 
      //read to letter or end of bytes 
      decrIV += iv.readUTFBytes(1); 
     } 
     var inputBA:ByteArray=Hex.toArray(Hex.fromString(input)); 
     var key:ByteArray = Hex.toArray(Hex.fromString(decrKey)); 
     var pad:IPad = new NullPad(); 
     var aes:ICipher = Crypto.getCipher("des-cbc", key, pad); 
     pad.setBlockSize(aes.getBlockSize()); 
     var ivmode:IVMode = des as IVMode; 
     ivmode.IV = Hex.toArray(Hex.fromString(decrIV)); 
     des.encrypt(inputBA); 

     return Base64.encodeByteArray(inputBA); 
    } 

有沒有人有任何建議,爲什麼他們不同?我錯過了什麼? TIA。

UPDATE:

這是我現在使用ActionScript代碼,但作爲我的評論表明它仍然比C#的結果不同:

protected function encrypt(input:String):String 
    { 
     var logData:Object = new Object(); 

     var decrKey:String = new String("tuber$20"); 
     // byte[] IV = { 20, 52, 88, 120, 76, 89, 205, 239 }; 
     var iv:ByteArray = new ByteArray(); 
     iv.writeByte(20); 

     iv.writeByte(52); 

     iv.writeByte(88); 

     iv.writeByte(120); 

     iv.writeByte(76); 

     iv.writeByte(89); 

     iv.writeByte(205); 

     iv.writeByte(239); 

     iv.position = 0; 
     trace("iv: " + iv);   

     //var decrIV:String = iv.readUTF(); 
     var decrIV:String = new String(); 
     while (iv.bytesAvailable > 0) { 
      //read to letter or end of bytes 
      decrIV += iv.readUTFBytes(1); 
     } 
     //var inputBA:ByteArray = Hex.toArray(Hex.fromString(input)); 
     var inputBA:ByteArray = new ByteArray(); 
     inputBA.writeUTFBytes(input); 

     //var key:ByteArray = Hex.toArray(Hex.fromString(decrKey)); 
     var key:ByteArray = new ByteArray(); 
     inputBA.writeUTFBytes(decrKey); 

     var pad:IPad = new NullPad(); 
     var aes:ICipher = Crypto.getCipher("des-cbc", key, pad); 
     pad.setBlockSize(aes.getBlockSize()); 
     var ivmode:IVMode = aes as IVMode; 
     //ivmode.IV = Hex.toArray(Hex.fromString(decrIV)); 
     ivmode.IV = new ByteArray(); 
     inputBA.writeUTFBytes(decrIV); 

     aes.encrypt(inputBA); 

     return Base64.encodeByteArray(inputBA); 
    } 

更新2:

謝謝@Miguel Sanchez。如果我打破各細目,並比較它們的輸出,他們除了最後加密相同:

protected function encrypt(input:String):String 
    { 
     trace("stringToEncrypt: " + input);      
     var logData:Object = new Object(); 

     var decrKey:String = new String("tuber$20"); 
     // byte[] IV = { 20, 52, 88, 120, 76, 89, 205, 239 }; 
     var iv:ByteArray = new ByteArray(); 
     iv.writeByte(20); 

     iv.writeByte(52); 

     iv.writeByte(88); 

     iv.writeByte(120); 

     iv.writeByte(76); 

     iv.writeByte(89); 

     iv.writeByte(205); 

     iv.writeByte(239); 

     iv.position = 0; 
     trace("iv: " + Base64.encodeByteArray(iv));   

     //var decrIV:String = iv.readUTF(); 
     var decrIV:String = new String(); 
     while (iv.bytesAvailable > 0) { 
      //read to letter or end of bytes 
      decrIV += iv.readUTFBytes(1); 
     } 
     trace("decrIV: " + decrIV); 

     //var inputBA:ByteArray = Hex.toArray(Hex.fromString(input)); 
     var inputBA:ByteArray = new ByteArray(); 
     inputBA.writeUTFBytes(input); 
     trace("inputBA: " + Base64.encodeByteArray(inputBA));  

     //var key:ByteArray = Hex.toArray(Hex.fromString(decrKey)); 
     var key:ByteArray = new ByteArray(); 
     key.writeUTFBytes(decrKey); 
     trace("key: " + Base64.encodeByteArray(key)); 

     var pad:IPad = new NullPad(); 
     var aes:ICipher = Crypto.getCipher("des-cbc", key, pad); 
     pad.setBlockSize(aes.getBlockSize()); 
     var ivmode:IVMode = aes as IVMode; 
     //ivmode.IV = Hex.toArray(Hex.fromString(decrIV)); 
     ivmode.IV = new ByteArray(); 
     ivmode.IV.writeUTFBytes(decrIV); 
     trace("ivmode.IV: " + Base64.encodeByteArray(ivmode.IV)); 

     aes.encrypt(inputBA); 

     trace("Base64.encodeByteArray(inputBA): " + Base64.encodeByteArray(inputBA)); 
     return Base64.encodeByteArray(inputBA); 
    } 

下面是從動作腳本輸出:

stringToEncrypt: a1d63a1fb90b422ecce953b3302b6e521f96 
key: dHViZXIkMjA= 
inputBA: YTFkNjNhMWZiOTBiNDIyZWNjZTk1M2IzMzAyYjZlNTIxZjk2 
iv: FDRYeExZze8= 
decrIV: 4XxLYÍï 
ivmode.IV: FDRYeExZw43Drw== 
Base64.encodeByteArray(inputBA): 6AJu1PUFRHx+Ykf0r1HlZVy39kR0HrOaw+wTHmnRPPunisp4TR0cSw== 

stringToEncrypt: 10:00 
key: dHViZXIkMjA= 
inputBA: MTA6MDA= 
iv: FDRYeExZze8= 
decrIV: 4XxLYÍï 
ivmode.IV: FDRYeExZw43Drw== 
Base64.encodeByteArray(inputBA): N5VgWd0Ccu0= 

這裏是C#/輸出。 NET:

stringToEncrypt: a1d63a1fb90b422ecce953b3302b6e521f96 
key: dHViZXIkMjA= 
inputByteArray: YTFkNjNhMWZiOTBiNDIyZWNjZTk1M2IzMzAyYjZlNTIxZjk2 
IV: FDRYeExZze8= 
Convert.ToBase64String(ms.ToArray()): h2jT8xR2SOPagrQwF3leuKFdEvHpYyfCUzEJw2lxXG2HnuUyw1QXzg== 

stringToEncrypt: 10:00 
key: dHViZXIkMjA= 
inputByteArray: MTA6MDA= 
IV: FDRYeExZze8= 
Convert.ToBase64String(ms.ToArray()): WVOOknAikYs= 

更新3:

這是最後一個步驟的ActionScript代碼:

aes.encrypt(inputBA); 

    return Base64.encodeByteArray(inputBA); 

這是最後一個步驟,C#/。NET代碼:

 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write); 
     cs.Write(inputByteArray, 0, inputByteArray.Length); 
     cs.FlushFinalBlock(); 
     return Convert.ToBase64String(ms.ToArray()); 
+0

在'as3'爲什麼你使用'Hex'功能轉換'String'到'ByteArray'爲什麼如果我改變不使用'ByteArray.writeUTFBytes()' –

+0

第一個十六進制行:「var inputBA:ByteArray = ByteArray.writeUTFBytes(input);」我得到錯誤:「Line 222,Column 45 \t 1061:通過靜態類型Class的引用調用可能未定義的方法writeUTFBytes。」 – Roger

+0

'var inputBA:ByteArray = new ByteArray(); inputBA.writeUTFBytes(輸入);' –

回答

1

首先改變這一行

inputBA.writeUTFBytes(decrKey); 

key.writeUTFBytes(decrKey); 

Y ou可能需要這樣的功能才能在AS3中打印ByteArray

public static function fromArray(array:ByteArray, colons:Boolean=false):String { 
     var s:String = ""; 
     for (var i:uint=0;i<array.length;i++) { 
       s+=("0"+array[i].toString(16)).substr(-2,2); 
       if (colons) { 
         if (i<array.length-1) s+=":"; 
       } 
     } 
     return s; 
} 

來源:AS3 ByteArray to Hex representation

+0

所以,我應該使用「trace(」inputBA:「+ fromArray(inputBA));」而不是代碼如「trace(」inputBA:「+ Base64.encodeByteArray(inputBA));」? – Roger

+0

@ user89861由於某種原因你在做Base64編碼嗎?我的意思是隻是在你的代碼中追蹤或者需要它? –

+0

否則,在跟蹤中它只輸出像byte []這樣的類型,而不是字節數組的實際內容。 – Roger