2013-11-27 37 views
2

我需要複製下面的C#方法來加密Javascript中的一些文本。目前我使用Crypto JS,但JS的輸出不等於C#輸出。Javascript與TripleDES實現不同的對比C#

const string EncryptKey = "hello"; 

private static String getHexStringFromArray(byte[] arr) { 
    StringBuilder sBuilder = new StringBuilder(); 

    for (int i = 0; i < arr.Length; i++) { 
     sBuilder.Append(arr[i].ToString("x2")); 
    } 

    return sBuilder.ToString(); 
}  

public void Encrypt(string toEncrypt, bool useHashing) { 
    byte[] keyArray; 
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); 

    string key = EncryptKey; 

    if (useHashing) { 
     MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); 
     keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); 

     hashmd5.Clear(); 
    } else 
     keyArray = UTF8Encoding.UTF8.GetBytes(key); 

    Console.WriteLine("hexadecimal key: " + getHexStringFromArray(keyArray)); 

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); 
    tdes.Key = keyArray; 
    tdes.Mode = CipherMode.ECB; 
    tdes.Padding = PaddingMode.PKCS7; 

    ICryptoTransform cTransform = tdes.CreateEncryptor(); 
    byte[] resultArray = 
     cTransform.TransformFinalBlock(toEncryptArray, 0, 
      toEncryptArray.Length); 

    tdes.Clear(); 

    Console.WriteLine("hexadecimal encrypted: " + getHexStringFromArray(resultArray)); 

    //Return the encrypted data into unreadable string format 
    string test = Convert.ToBase64String(resultArray, 0, resultArray.Length); 

    Console.WriteLine("Output: " + test); 
} 

Encrypt("password", true)的輸出是:

hexadecimal key: 5d41402abc4b2a76b9719d911017c592 
hexadecimal encrypted: 069c44845e907b346b9d82d1d553f391 
Output: BpxEhF6QezRrnYLR1VPzkQ== 

現在,Javascript實現(請忽略全局變量):

window.text = "password"; 
window.key = "hello"; 
var useHashing = true; 

if (useHashing){ 
    key = CryptoJS.MD5(key).toString(); 
} 

window.options = { 
    mode: CryptoJS.mode.ECB, 
    padding: CryptoJS.pad.Pkcs7 
}; 

window.textWordArray = CryptoJS.enc.Utf8.parse(text); 
window.keyHex = CryptoJS.enc.Hex.parse(key); 

console.log('hexadecimal key: ' + keyHex); 

window.encrypted = CryptoJS.TripleDES.encrypt(textWordArray, keyHex, options); 

var base64String = encrypted.toString(); 

console.log('base64: ' + base64String); 

window.decrypted = CryptoJS.TripleDES.decrypt({ 
    ciphertext: CryptoJS.enc.Base64.parse(base64String) 
}, keyHex, options); 

console.log('decrypted: ' + decrypted.toString(CryptoJS.enc.Utf8)); 

會產生這樣的結果:

hexadecimal key: 5d41402abc4b2a76b9719d911017c592 
base64: BK5f0AhEuUl9pYEy2Mliyw== 

這與C#實現不同。

Here你可以找到Javascript代碼。

任何幫助?

+0

都是程序加密* *最後塊,即,使PKCS7填充被兩個相同呢? –

+0

@DavidRTribble我真的不知道。我認爲PKCS7是一個標準以及tripleDES,所以兩個實施應該是相同的... –

回答

7

TripleDES需要24字節密鑰(k1 + k2 + k3)。你的密鑰只有16個字節。而.NET自動完成k3 = k1。但Javascript不,k3 = 0.請修改密鑰:

if (useHashing){ 
    key = CryptoJS.MD5(key).toString(); 
    var k1 = key.substring(0, 16); 
    key = key + k1; 
} 
0

這裏是Decrypter使用3DES-ECB的Forge js。 由於找不到僞造的js解決方案,因此其他人可以使用它。

var md = forge.md.md5.create(); 
md.update(window.key); 

var key = md.digest().getBytes(); 
//3DES-ECB requires 24byte of data and key returned from md5 is 16byte 
var k1 = key.substring(0, 8); 
var key1 = key + key1; 

var input = forge.util.createBuffer(forge.util.decode64(window.text)); 
var decTer = forge.cipher.createDecipher('3DES-ECB', key1); 
decTer.start(); 
decTer.update(input); 

return decTer.output.getBytes(); 
相關問題