简体   繁体   English

web.crypto加密C#解密

[英]web.crypto encryption c# decryption

I'm trying to do: 我正在尝试做:

  1. encrypt text using web.crypto, 使用web.crypto加密文本,
  2. decrypt text using AesCryptoServiceProvider I didn't get any exceptions in my code, but the decryption doesn't match the plain text that i've encrypted 使用AesCryptoServiceProvider解密文本我的代码没有任何异常,但是解密与我加密的纯文本不匹配

the plaintext bytes after decyption are the same, but the encoding to string doesn't work 解密后的纯文本字节相同,但是对字符串的编码不起作用

I'm using random iv, the key and the plaintext are constant. 我正在使用随机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);
}

After that I'm sending the iVec, ciphertext using json 之后,我使用json发送iVec,密文

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

I'm using the following utility functions : 我正在使用以下实用程序功能:

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

server side accepts the keys, and suppose to decrypt: 服务器端接受密钥,并假设解密:

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

I've got this utility function as well: 我也有这个实用程序功能:

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

I'm getting the paramters from anthor function: 我从anthor函数获取参数:

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

I've used wrong Ecoding, instand of 我使用了错误的Ecoding,

Convert.ToBase64String(plaintextBytes); Convert.ToBase64String(plaintextBytes);

I should've used 我应该用

Encoding.ASCII.GetString(plaintextBytes); Encoding.ASCII.GetString(plaintextBytes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM