C#和javascript之间的加密结果差异

时间:2019-06-21 15:11:33

标签: javascript c# encryption cryptojs

我想加密数据以请求api参数。我将C#代码转换为javascript,但得到了差异结果:

C#代码:

private byte[] GetPasswordInBytes(string password)
{
   SHA1Managed mSha1 = new SHA1Managed();
   var hashBytes = mSha1.ComputeHash(Encoding.UTF8.GetBytes(password));
   return hashByte;
}   

Javascipt代码:

getPasswordInBytes(password) {
  let bytes = CryptoJS.enc.Utf8.parse(password);
  let hashPass = CryptoJS.SHA1(bytes);
  return this.getBytes(hashPass);
}

getBytes(hash) {
  var byteArray = [];
  hash.words.forEach(function(i) {
    var k;
    for (var j = 3; j >= 0; j--) {
      k = (i >> (j * 8)) & 0xFF;
      k = k < 128 ? k : -(256 - k);
      byteArray.push(k);
    }
  });

  return byteArray;
}

输入:password =“ 123456”

输出:

C#:

[124, 74, 141, 9, 202, 55, 98, 175, 97, 229, 149, 32, 148, 61, 194, 100, 148, 248, 148, 27]

Javascript:

[124, 74, -115, 9, -54, 55, 98, -81, 97, -27, -107, 32, -108, 61, -62, 100, -108, -8, -108, 27]

我只是通过上面的代码实现了新功能:

C#代码

  private byte[] GetPasswordInBytes(string password)
    {
      SHA1Managed mSha1 = new SHA1Managed();
      var hashBytes = mSha1.ComputeHash(Encoding.UTF8.GetBytes(password));           

      var keyBytes = new byte[16];
      Array.Copy(hashBytes, keyBytes, 16);//Copy first 128-bit.

      return keyBytes;
    }

    private string Encrypt(string plainText, string password)
    {
      byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
      byte[] keyBytes = GetPasswordInBytes(password);
      RijndaelManaged rijndaelManaged = new RijndaelManaged
      {
        Mode = CipherMode.CBC,
        Padding = PaddingMode.PKCS7,
        KeySize = 128,
        BlockSize = 128,
        Key = keyBytes,
        IV = keyBytes
      };

      byte[] cryptedBytes = rijndaelManaged.CreateEncryptor()
          .TransformFinalBlock(plainBytes, 0, plainBytes.Length);

      return Convert.ToBase64String(cryptedBytes);
    }

JavaScript代码

getPasswordInBytes(password) {
    let bytes = CryptoJS.enc.Utf8.parse(password);
    let hashPass =  CryptoJS.SHA1(bytes);    
    let byteArray = this.getBytes(hashPass);

    return byteArray.slice(0, 16);//Copy first 128-bit.
  } 



 getBytes(hash)
  {    
    var byteArray = [];
    hash.words.forEach(function (i) {
      var k;
      for (var j = 3; j >= 0; j--) {
        k = (i >> (j * 8)) & 0xFF;        
        byteArray.push(k);
      }
    });

    return byteArray;
  }    



  encrypt(clearText, password)
  {
    let key = this.getPasswordInBytes(password).toString();
    let iv = key;    
    let data = CryptoJS.enc.Latin1.parse(clearText);
    var encrypted = CryptoJS.AES.encrypt(
      data,
      key,
      { 
        keySize: 128,
        iv: iv,       
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      });     

    let result =  CryptoJS.enc.Base64.stringify( encrypted.ciphertext);   

    return result;
  }

调用方法: 加密('cleart text','123456')

输出:

C#: m8BOnfX+n9Fc90dZdV2vQQ==     
Javascript: 6qYSO7ZkTcesmZKvkFJz+g==  

在javascript中,每次运行的结果均已更改

lV2NO3NpsERUQmXMOOzcvQ==    
H1Sla9/wrQXvKuTELPATDQ==    
WsMHHJLskuJxmEJe7PnAAg==  

更新

 getPasswordInBytes(password) {
    let bytes = CryptoJS.enc.Utf8.parse(password);   
    let hashPass =  CryptoJS.SHA1(bytes);    
    let byteArray = this.getBytes(hashPass);
    return byteArray;
  }          

  encrypt(clearText, password)
  {
    let keyBytes = this.getPasswordInBytes(password);     
    let key = CryptoJS.lib.WordArray.create(keyBytes, 16);//128bit
    let iv = CryptoJS.lib.WordArray.create(keyBytes, 16);    
    let data = CryptoJS.enc.Utf8.parse(clearText);      
    var encrypted = CryptoJS.AES.encrypt(
      data,
      key,
      {        
        keySize:128,
        blockSize: 128,   
        iv: iv,        
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      });                    

    return encrypted.toString();            
  }

  getBytes(hash)
  {    
    var byteArray = [];
    hash.words.forEach(function (i) {
      var k;
      for (var j = 3; j >= 0; j--) {
        k = (i >> (j * 8)) & 0xFF;        
        byteArray.push(k);
      }
    });

    return byteArray;
  }

调用方法:crypto('cleart text','123456')

输出:仍然存在差异

C#: m8BOnfX+n9Fc90dZdV2vQQ==
Javascript: gVSE5CdyGxJJRbWaJiuyhg==

1 个答案:

答案 0 :(得分:1)

如果您从代码中删除行k = k < 128 ? k : -(256 - k);,结果将是相同的。