2017-05-07 1 views
0

내가 할 노력하고있어 :web.crypto 암호화 C#을 암호 해독

  1. 암호화 사용하여 텍스트 web.crypto을, 내 코드에서 예외를하지 않았다 AesCryptoServiceProvider I을 사용하여
  2. 해독 텍스트 만 암호 해독 내가 decyption 후

일반 텍스트 바이트를 암호화 동일 한 일반 텍스트와 일치하지 않지만, 문자열 인코딩이 작동하지 않습니다

무작위로 iv를 사용하고 있는데, 키와 평문은 상수입니다. 그 후

function cryptoSys(plaintext,KeyString){ 
     var iVec=window.crypto.getRandomValues(new Uint8Array(16));       
     var encryptSuccessFunc=(encrypt)=> { AfterEncrypt(BytearrayToString(iVec),arraybufferTostring(encrypt));} 
     var ImportKeySuccessFunc= (keyObj)=>{     
            window.crypto.subtle.encrypt(    
            {name:"AES-CBC", iv:iVec}, 
            keyObj, 
            StringToByteArray(plaintext) 
            ).then(encryptSuccessFunc);   

     window.crypto.subtle.importKey(
      "raw", 
      StringToByteArray(KeyString), 
      {name:"AES-CBC", length:128}, 
      true, 
      ["encrypt","decrypt"] 
      ).then(ImportKeySuccessFunc); 
} 

나는 다음과 같은 유틸리티 기능을 사용하고 JSON을

function AfterEncrypt(iVec,ciphertext) 
    {    
     var plaintext="String to Encrypt"; 
     if(iVec==null) 
      return; 
     var send2server= {"ciphertext":ciphertext, 
      "iVec":iVec, 
      "plaintext":plaintext};    
     var objectDataString = JSON.stringify(send2server);    
     sendJSONtoserver(objectDataString,"getDelayedBid");     
    } 

를 사용하여 iVec, 암호문을 보내고있다 :

function StringToByteArray(strString) {    

     var byteArray = new Uint8Array(strString.length); 
     for (var i=0; i<strString.length; i++) { 
      byteArray[i] = strString.charCodeAt(i); 
     } 
     return byteArray; 
    } 
    function BytearrayToString(arrayBuffer) {    
     var strString = "";       
     for (var i=0; i<arrayBuffer.byteLength; i++) {     
      strString += String.fromCharCode(arrayBuffer[i]); 
     } 
     return strString; 
    } 
    function arraybufferTostring(buf) { 
     return String.fromCharCode.apply(null, new Uint8Array(buf)); 
    } 

서버 측이 열쇠를 받아, 그리고 가정 해독하기 :

public string DecryptCipher(Encoding u16, string cipherText, string key,string iVec) 
    { 
     byte[] ciphertextBytes = clearZeros(u16.GetBytes(cipherText)); 
     AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); 
     aes.BlockSize = 128; 
     aes.KeySize = 128; //minimun key length 
     aes.Key = clearZeros(u16.GetBytes(key)); 
     aes.IV = clearZeros(u16.GetBytes(iVec)); 
     aes.Padding = PaddingMode.PKCS7; 
     aes.Mode = CipherMode.CBC; 
     ICryptoTransform cryptoTrans = aes.CreateDecryptor(aes.Key, aes.IV); 
     byte[] plaintextBytes = cryptoTrans.TransformFinalBlock(ciphertextBytes, 0, ciphertextBytes.Length); 
     cryptoTrans.Dispose(); 
     return Convert.ToBase64String(plaintextBytes); 
    } 

나뿐만 아니라이 유틸리티 기능을 가지고 :

private byte [] clearZeros(byte [] bytearray) 
    { 
     byte[] ans = new byte[bytearray.Length/2]; 
     for (int i = 0; i < bytearray.Length/2; i++) 
      ans[i] = bytearray[2 * i]; 
     return ans; 
    } 

내가 anthor 기능에서 paramters 받고 있어요 : 내가 잘못 Ecoding을 사용했습니다

public ActionResult getDelayedBid([FromBody] DelayedSubmission delaySub) 
    { 
     if (delaySub.ciphertext == null) 
      return Json("Failure", JsonRequestBehavior.AllowGet); 
     Encoding u16LE = Encoding.Unicode;   
     String decrypetedMessageLE = new Encryption().DecryptCipher(u16LE, delaySub.ciphertext, getKeyFromDB(), delaySub.iVec);     
     if (decrypetedMessageLE.Equals(delaySub.plaintext)) 
      { 
       return Json("Success", JsonRequestBehavior.AllowGet); 
      } 
     return Json("Failure", JsonRequestBehavior.AllowGet); 
} 

답변

0

,

변환의 instand .ToBase64String (plaintextBytes);

나는

Encoding.ASCII.GetString (plaintextBytes)를 사용했습니다한다