2016-10-17 48 views
0

我有用於在C#中加密和解密文件的代碼。現在我需要將加密任務移到前端,我需要編寫JavaScript代碼來加密.xlsx文件。下面是加密和解密的C#代碼:將C#加密機制代碼轉換爲javascript

private void EncryptFile(string inputFile, string outputFile) 
    { 

     try 
     { 
      string password = @"myKey123"; // Your Key Here 
      UnicodeEncoding UE = new UnicodeEncoding(); 
      byte[] key = UE.GetBytes(password); 

      string cryptFile = outputFile; 
      FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create); 

      RijndaelManaged RMCrypto = new RijndaelManaged(); 

      CryptoStream cs = new CryptoStream(fsCrypt, 
       RMCrypto.CreateEncryptor(key, key), 
       CryptoStreamMode.Write); 

      FileStream fsIn = new FileStream(inputFile, FileMode.Open); 

      int data; 
      while ((data = fsIn.ReadByte()) != -1) 
       cs.WriteByte((byte)data); 


      fsIn.Close(); 
      cs.Close(); 
      fsCrypt.Close(); 
     } 
     catch(Exception ex) 
     { 
      // MessageBox.Show("Encryption failed!", "Error"); 
     } 
    } 

    private void DecryptFile(string inputFile, string outputFile) 
    { 

     { 
      string password = @"myKey123"; // Your Key Here 

      UnicodeEncoding UE = new UnicodeEncoding(); 
      byte[] key = UE.GetBytes(password); 

      FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); 

      RijndaelManaged RMCrypto = new RijndaelManaged(); 

      CryptoStream cs = new CryptoStream(fsCrypt, 
       RMCrypto.CreateDecryptor(key, key), 
       CryptoStreamMode.Read); 

      FileStream fsOut = new FileStream(outputFile, FileMode.Create); 

      int data; 
      while ((data = cs.ReadByte()) != -1) 
       fsOut.WriteByte((byte)data); 

      fsOut.Close(); 
      cs.Close(); 
      fsCrypt.Close(); 

     } 
    } 

我試圖在JavaScript中創建一個加密如下:

var reader = new FileReader(); 
    reader.onload = function (e) { 

     var encrypted = CryptoJS.AES.encrypt(e.target.result, 'myKey123'); 

     var data = new FormData(); 


     var encryptedFile = new File([encrypted], file.name, { type: "text/plain", lastModified: new Date() }); 

     data.append('file', encryptedFile); 

     $.ajax({ 
      url: 'http://localhost:57691/api/WithKey/UploadFile', 
      data: data, 
      cache: false, 
      contentType: false, 
      processData: false, 
      type: 'POST', 
      success: function (data) { 
       debugger; 
      } 
     }); 
    }; 

    reader.readAsDataURL(file); 

我也試圖轉換鍵如下UTF16:

function wordsToBytes (words) { 
     for (var bytes = [], b = 0; b < words.length * 32; b += 8) 
      bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); 
     return bytes; 
    } 
    var temp = CryptoJS.enc.Utf16.parse('myKey123'); 
    var key = wordsToBytes(temp.words); 

但沒有運氣。你可以請別人幫我,我做錯了。在C#中用JavaScript加密文件的正確方法是什麼?

+0

你爲什麼想做這個,而不是使用HTTPS的?你的C#代碼泄漏,你不會處置你需要處理的所有東西。 –

+0

我想知道爲什麼你創建了一個新賬戶來問一個類似於[this one]的問題(http://stackoverflow.com/q/40055398/1816580)。無論如何,爲什麼你需要移植這個C#代碼?它是可怕的。你爲什麼不簡單地使用具有多種語言實現的[RNCryptor](https://github.com/RNCryptor)? –

回答

1

這是將產生與C#代碼相同的密文的JavaScript代碼。現在仍然存在的問題是您需要以某種方式傳輸。

var keyWords = CryptoJS.enc.Utf16LE.parse("myKey123"); 
 
var encryptedWords = CryptoJS.AES.encrypt("some string", keyWords, { iv: keyWords }).ciphertext; 
 
console.log("Hex: " + encryptedWords.toString()); 
 
console.log("Base64: " + encryptedWords.toString(CryptoJS.enc.Base64));
<script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/aes.js"></script> 
 
<script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/components/enc-utf16-min.js"></script>