2016-10-17 4 views
0

C#에서 파일을 암호화하고 암호를 해독하는 코드가 있습니다. 이제 암호화 작업을 프론트 엔드로 옮겨서 .xlsx 파일을 암호화하는 자바 스크립트 코드를 작성해야합니다.C# 암호화 메커니즘 코드를 자바 스크립트로 변환

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); 
: 나는 또한 아래 UTF16 키를 변환 시도

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); 

:

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(); 

     } 
    } 

내가 아래로 자바 스크립트에서 암호화를 만들려고 : 다음은 암호화 및 암호 해독에 대한 C# 코드는

하지만 행운은 없습니다. 내가 잘못하고있는 곳에서 누군가 도와 주시겠습니까? 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>

관련 문제