Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript C#和Cryptojs的TripleDESCryptoServiceProvider给出了不同的结果_Javascript_C#_Cryptojs_Tripledes - Fatal编程技术网

Javascript C#和Cryptojs的TripleDESCryptoServiceProvider给出了不同的结果

Javascript C#和Cryptojs的TripleDESCryptoServiceProvider给出了不同的结果,javascript,c#,cryptojs,tripledes,Javascript,C#,Cryptojs,Tripledes,为什么使用c#和JavaScript cryptojs对三元组进行加密时会得到不同的结果?请看下面我的代码 c# 使用cryptojs的JavaScript txtEncrypter = () => { const msg = 'jvofs:JCV XXXXX:201911141547:12345678'; const key = 'jjvofs'; const keyOffset = 10; const keybyte: any = this.wordAr

为什么使用c#和JavaScript cryptojs对三元组进行加密时会得到不同的结果?请看下面我的代码

c#

使用cryptojs的JavaScript

txtEncrypter = () => {
    const msg = 'jvofs:JCV XXXXX:201911141547:12345678';
    const key = 'jjvofs';
    const keyOffset = 10;
    const keybyte: any = this.wordArrayToByteArray(crypto.SHA512(key), 100);

    // For message
    const dataToEncrypt = crypto.enc.Utf8.parse(msg);
    const dte = this.wordArrayToByteArray(dataToEncrypt, 100);
    const dataToEncryptx = this._arrayBufferToBase64(dte);
    const dataToEncryptxx = crypto.enc.Utf8.parse(dataToEncryptx);

    // For key
    let newKeyx = keybyte.slice(keyOffset, 34);
    const newKeyxB4Splice = newKeyx;
    const newKeyxB4Splicex = this._arrayBufferToBase64(newKeyx);
    newKeyx = crypto.enc.Utf8.parse(newKeyx);
    
    const options = {
      mode: crypto.mode.ECB,
      padding: crypto.pad.Pkcs7
    };
    
    const encrypted = crypto.TripleDES.encrypt(dataToEncrypt, newKeyx, options);
    const base64String = encrypted.toString();
    console.log(base64String);
  }



wordArrayToByteArray(wordArray, length) {
    if (wordArray.hasOwnProperty('sigBytes') && wordArray.hasOwnProperty('words')) {
      length = wordArray.sigBytes;
      wordArray = wordArray.words;
    }

    const result = [];
    let bytes: any;
    let i = 0;
    while (length > 0) {
      bytes = this.wordToByteArray(wordArray[i], Math.min(4, length));
      length -= bytes.length;
      result.push(bytes);
      i++;
    }
    return [].concat.apply([], result);
  }

  wordToByteArray(word: any, length: any) {
    const ba = [], xFF = 0xFF;
    if (length > 0) {
      // tslint:disable-next-line:no-bitwise
      ba.push(word >>> 24);
    }
    if (length > 1) {
      // tslint:disable-next-line:no-bitwise
      ba.push((word >>> 16) & xFF);
    }
    if (length > 2) {
      // tslint:disable-next-line:no-bitwise
      ba.push((word >>> 8) & xFF);
    }
    if (length > 3) {
      // tslint:disable-next-line:no-bitwise
      ba.push(word & xFF);
    }
    return ba;
  }

  byteArrayToWordArray(ba) {
    const wa = [];
    let i = 0;
    for (i = 0; i < ba.length; i++) {
      // tslint:disable-next-line:no-bitwise
      wa[(i / 4) | 0] |= ba[i] << (24 - 8 * i);
    }

    return crypto.lib.WordArray.create(wa);
  }

  toUTF8Array(str) {
    const utf8 = [];
    for (let i = 0; i < str.length; i++) {
        let charcode = str.charCodeAt(i);
        if (charcode < 0x80) { utf8.push(charcode); } else if (charcode < 0x800) {
            // tslint:disable-next-line:no-bitwise
            utf8.push(0xc0 | (charcode >> 6), 
                      // tslint:disable-next-line: no-bitwise
                      0x80 | (charcode & 0x3f));
        } else if (charcode < 0xd800 || charcode >= 0xe000) {
            // tslint:disable-next-line: no-bitwise
            utf8.push(0xe0 | (charcode >> 12), 
                      // tslint:disable-next-line: no-bitwise
                      0x80 | ((charcode>>6) & 0x3f), 
                      // tslint:disable-next-line: no-bitwise
                      0x80 | (charcode & 0x3f));
        } else {
            i++;
            // UTF-16 encodes 0x10000-0x10FFFF by
            // subtracting 0x10000 and splitting the
            // 20 bits of 0x0-0xFFFFF into two halves
            // tslint:disable-next-line:no-bitwise
            charcode = 0x10000 + (((charcode & 0x3ff)<<10)
                      // tslint:disable-next-line:no-bitwise
                      | (str.charCodeAt(i) & 0x3ff));
            // tslint:disable-next-line:no-bitwise
            utf8.push(0xf0 | (charcode >>18), 
                      // tslint:disable-next-line:no-bitwise
                      0x80 | ((charcode>>12) & 0x3f), 
                      // tslint:disable-next-line: no-bitwise
                      0x80 | ((charcode>>6) & 0x3f), 
                      // tslint:disable-next-line: no-bitwise
                      0x80 | (charcode & 0x3f));
        }
    }
    return utf8;
}

 _arrayBufferToBase64( buffer ) {
  let binary = '';
  const bytes = new Uint8Array( buffer );
  const len = bytes.byteLength;
  for (let i = 0; i < len; i++) {
      binary += String.fromCharCode( bytes[ i ] );
  }
  return window.btoa( binary );
}
txtEncrypter=()=>{
const msg='jvofs:JCV XXXXX:201911141547:12345678';
常量键='jjvofs';
常数keypoffset=10;
const keybyte:any=this.wordArrayToByteArray(crypto.SHA512(key),100);
//留言
const dataToEncrypt=crypto.enc.Utf8.parse(msg);
const dte=this.wordArrayToByteArray(dataToEncrypt,100);
const dataToEncryptx=此。_arrayBufferToBase64(dte);
const dataToEncryptxx=crypto.enc.Utf8.parse(dataToEncryptx);
//钥匙
设newKeyx=keybyte.slice(keyOffset,34);
常数newKeyxB4Splice=newKeyx;
const NEWKEYXB4STIPLEX=此。_arrayBufferToBase64(newKeyx);
newKeyx=crypto.enc.Utf8.parse(newKeyx);
常量选项={
模式:crypto.mode.ECB,
填充:crypto.pad.Pkcs7
};
const encrypted=crypto.TripleDES.encrypt(dataToEncrypt、newKeyx、options);
const base64String=加密的.toString();
console.log(base64String);
}
wordArray字节数组(wordArray,长度){
if(wordArray.hasOwnProperty('sigBytes')&&wordArray.hasOwnProperty('words')){
长度=wordArray.sigBytes;
wordArray=wordArray.words;
}
常量结果=[];
让字节:任意;
设i=0;
而(长度>0){
bytes=this.wordToByteArray(wordArray[i],Math.min(4,长度));
长度-=字节数。长度;
结果推送(字节);
i++;
}
返回[]。concat.apply([],结果);
}
wordToByteArray(字:任意,长度:任意){
常数ba=[],xFF=0xFF;
如果(长度>0){
//tslint:禁用下一行:无位
ba.push(word>>>24);
}
如果(长度>1){
//tslint:禁用下一行:无位
推送((word>>>16)和xFF);
}
如果(长度>2){
//tslint:禁用下一行:无位
ba.push((word>>>8)和xFF);
}
如果(长度>3){
//tslint:禁用下一行:无位
推送学士(word和xFF);
}
返回ba;
}
byteArrayToWordArray(ba){
常数wa=[];
设i=0;
对于(i=0;i6),
//tslint:禁用下一行:无位
0x80 |(字符码&0x3f));
}否则如果(字符码<0xd800 | |字符码>=0xe000){
//tslint:禁用下一行:无位
utf8.按下(0xe0 |(字符码>>12),
//tslint:禁用下一行:无位
0x80 |((字符码>>6)和0x3f),
//tslint:禁用下一行:无位
0x80 |(字符码&0x3f));
}否则{
i++;
//UTF-16通过以下方式对0x10000-0x10FFFF进行编码:
//减去0x10000并拆分
//0x0-0xFFFFF的20位分成两半
//tslint:禁用下一行:无位
charcode=0x10000+((charcode&0x3ff)18),
//tslint:禁用下一行:无位
0x80 |((字符码>>12)和0x3f),
//tslint:禁用下一行:无位
0x80 |((字符码>>6)和0x3f),
//tslint:禁用下一行:无位
0x80 |(字符码&0x3f));
}
}
返回utf8;
}
_arrayBufferToBase64(缓冲区){
让二进制=“”;
const bytes=新的Uint8Array(缓冲区);
const len=bytes.bytellength;
for(设i=0;i
解析消息和键的数据时,c#和JavaScript都是相同的:

信息:

[C#]ANZVZNM6SKNWIFHYWFHYOJIWMTKxMTTE0MTU0NZOxMj0Nty3OA==

[cryptojs]ANZVZNM6SKNWIFHYWFHYOJIWMTKxMTTE0MTU0NZOxMJ0NTY3OA==

关键:

[C#]SMBKKMMDRCBDREV7S4HLAWE16NVYM+9gW

[cryptojs]SMBKKKMDRCBDRev7S4hlawe16NVYM+9gW

但一旦“crypto.TripleDES.encrypt(..)”运行,我就会得到不同的c#和javascript结果:

[C#]1PJVBOB81IAOQSKZ+cM080yDU37XBoCwMhbYULwva/Nql5vbEMiPQ==

[cryptojs]ncCcCYNy3jVsB/95SaC2N1rH5Q+hX04WvScMvwmtkPkrnL7Ki1bmPg==


从散列的字节数组中将键确定为子数组(包括索引10到索引34),该子数组必须转换回具有等效内容的
字数组
,即行:

newKeyx = crypto.enc.Utf8.parse(newKeyx);
必须替换为:

newKeyx = byteArrayToWordArray(newKeyx);  
通过此更改,NodeJS代码返回与C#代码相同的结果


转换
WordArray byte array
(以及这些转换所需的所有函数)实际上并不是必需的,因为作为替代方案,密钥也可以仅使用以下函数派生:



顺便说一下,-模式是不安全的,应该使用性能更高的AES来代替。

在c#中,KeyOffSet!=键偏移。然后从IX10开始复制,但是newKeyx只有24字节长,所以哈希值只有14字节?你确定?在javascript中,它至少是34。检查keybyte的长度。然后javascript可能会用salt或rounds填充前10个字节,你也必须在c#中这样做。@Charles,对不起。那只是个打字错误。我试图使代码更简单,以便更清晰。我现在做了改变。请看我的编辑。谢谢。试着逐行比较byte[]数组,看看它们什么时候不同。是keybyte js==keybyte c#,等等。我无法测试您的javascript,因为有函数missing@Charles我会把一切都发出去。我比较了每一行,它们完全相同。@Charles请查看我的编辑,其中包括所有方法。非常感谢您,先生!!!!!!!!!!!!!!!非常感谢。非常感谢。非常感谢。
newKeyx = byteArrayToWordArray(newKeyx);  
...
const key = 'jjvofs';
const keyOffset = 10;
const keyLength = 24;
const keyHash = crypto.enc.Hex.stringify(crypto.SHA512(key));
const newKey = crypto.enc.Hex.parse(keyHash.slice(keyOffset * 2, (keyOffset + keyLength) * 2));
...