第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# 和 javascript 之間的 ECDH

C# 和 javascript 之間的 ECDH

C#
元芳怎么了 2021-12-25 18:39:46
我正在構(gòu)建一個 Web API,我需要實現(xiàn) ECDH 來執(zhí)行端到端加密。在服務(wù)器端我有一個 C# 應(yīng)用程序,在客戶端我有一個 Javascript 應(yīng)用程序。我能夠交換密鑰、生成私鑰并加密消息,但我在解密時遇到了問題。我認為問題在于公鑰的交換。在 javascript 中,鍵以字節(jié)“4”開頭,.NET 鍵以 8 個字節(jié)開頭,用于標識鍵的類型和大小,我需要更改這些字節(jié)以導(dǎo)入每個鍵(我在這里找到的信息)。也許這會導(dǎo)致一些不一致。在客戶端,我使用 Web Cryptography API 來處理 ECDH。我正在實施如下。生成密鑰await window.crypto.subtle.generateKey(        {            name: "ECDH",            namedCurve: "P-256",        },        false,        ["deriveKey", "deriveBits"]    );像這樣導(dǎo)出公鑰:await window.crypto.subtle.exportKey(        "raw",        publicKey    );導(dǎo)入外部公鑰await window.crypto.subtle.importKey(        "raw",        {            name: "ECDH",            namedCurve: "P-256",        },        false,        ["deriveKey", "deriveBits"]    )最后推導(dǎo)出密鑰await window.crypto.subtle.deriveKey(        {            name: "ECDH",            namedCurve: "P-256",            public: publicKey,        },        privateKey,        {            name: "AES-CBC",            length: 256,        },        false,        ["encrypt", "decrypt"]    )在服務(wù)器端,我正在實施以下相同的步驟。生成公鑰private static ECDiffieHellmanCng ecdh = new ECDiffieHellmanCng(256);public static void GeneratePublicKey(){    ecdh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;    ecdh.HashAlgorithm = CngAlgorithm.Sha256;    publicKey = ecdh.PublicKey.ToByteArray();}導(dǎo)出公鑰。請注意,我更改了第一個字節(jié)public static byte[] GetPublicKey()    {        var auxKey = publicKey.Skip(7).ToArray();        auxKey[0] = 4;        return auxKey;    }導(dǎo)入公鑰并導(dǎo)出私鑰。請注意,我更改了第一個字節(jié)public static void GerarChavePrivada(byte[] bobPublicKey){    byte[] aux = new byte[bobPublicKey.Length + 7];    aux[0] = 0x45;    aux[1] = 0x43;    aux[2] = 0x4B;    aux[3] = 0x31;    aux[4] = 0x20;    aux[5] = 0x00;    aux[6] = 0x00;    aux[7] = 0x00;    for (int i = 1; i < bobPublicKey.Length; i++)    {        aux[7 + i] = bobPublicKey[i];    }    var importedKey = CngKey.Import(aux, CngKeyBlobFormat.EccPublicBlob);    privateKey = ecdh.DeriveKeyMaterial(importedKey);}
查看完整描述

3 回答

?
qq_笑_17

TA貢獻1818條經(jīng)驗 獲得超7個贊

我遇到過同樣的問題。經(jīng)過更多調(diào)試后,我意識到 C# 使用 DeriveKeyMaterial 生成的密鑰然后使用 SHA-256 進行哈希處理。


我的解決方案是在 javascript 上導(dǎo)出派生密鑰,對其進行哈希處理,然后將其作為新密鑰導(dǎo)入。


cryptoApi().deriveKey(

  {

      name: "ECDH",

      namedCurve: "P-256", //can be "P-256", "P-384", or "P-521"

      public: ServerKey, //an ECDH public key from generateKey or importKey

  },

  ECkey.privateKey, //your ECDH private key from generateKey or importKey

  { //the key type you want to create based on the derived bits

      name: "AES-CBC", //can be any AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH", or "HMAC")

      //the generateKey parameters for that type of algorithm

      length: 256, //can be  128, 192, or 256

  },

  true, //whether the derived key is extractable (i.e. can be used in exportKey)

  ["encrypt", "decrypt"] //limited to the options in that algorithm's importKey

)

.then(function(AESKeyData){

  //returns the exported key data

  console.log(AESKeyData);


  cryptoApi().exportKey('raw',

    AESKeyData

  ).then(function (exportedAESKeyData) {

      cryptoApi().digest('SHA-256', exportedAESKeyData).then(function (HashedAESKeyValue) {

          console.log(HashedAESKeyValue);


          cryptoApi().importKey(

            'raw',

            HashedAESKeyValue,

            { //the key type you want to create based on the derived bits

                name: "AES-CBC", //can be any AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH", or "HMAC")

                //the generateKey parameters for that type of algorithm

                length: 256, //can be  128, 192, or 256

            },

            false,

            ["encrypt", "decrypt"]

        ).then(function (TrueAESKey) {


            cryptoApi().decrypt(

               {

                   name: 'AES-CBC',

                   length: 256,

                   iv: base64ToArrayBuffer(IV)

               },

               TrueAESKey,

               base64ToArrayBuffer(EncryptedData)

               ).then(function (decrypted) {

                   console.log(buf2hex(decrypted));

               });

        })

      });

  });



})


查看完整回答
反對 回復(fù) 2021-12-25
?
天涯盡頭無女友

TA貢獻1831條經(jīng)驗 獲得超9個贊

你見過PKI.js嗎?在那里您可以找到 CMS Enveloped/Encrypted Data 的所有可能的密鑰加密模式的完整實現(xiàn)。還有現(xiàn)場示例這里是所有示例的源代碼。請注意存在的wiki頁面關(guān)于PKI.js.與CMS EnvelopedData工作


查看完整回答
反對 回復(fù) 2021-12-25
?
FFIVE

TA貢獻1797條經(jīng)驗 獲得超6個贊

嘗試這樣做


要點中的完整示例


class Protector {

  ab2str(buffer) {

    return new TextDecoder().decode(buffer);

  }


  str2ab(text) {

    return new TextEncoder().encode(text);

  }


  generateIv() {

    return crypto.getRandomValues(new Uint8Array(16));

  }


  /**

   * @see https://github.com/mdn/dom-examples/blob/master/web-crypto/derive-bits/ecdh.js

   */

  async generateKey() {

    this.key = await window.crypto.subtle.generateKey(

      { name: 'ECDH', namedCurve: 'P-256' },

      false,

      ['deriveBits']

    );

  }


  async encrypt(plaintext) {

    const counter = this.generateIv();

    const buffer = await crypto.subtle.decrypt({

      name: 'aes-ctr',

      counter: counter,

      length: 128

    }, this.importedKey, this.str2ab(plaintext));

    return { buffer, counter };

  }


  async decrypt(data) {

    const buffer = await crypto.subtle.decrypt({

      name: 'aes-ctr',

      counter: data.counter,

      length: 128

    }, this.importedKey, data.buffer);

    return this.ab2str(buffer);

  }


  getPublicKey() {

    return {publicKey: this.key.publicKey};

  }


  async setRemotePublicKey(key) {

    this.clientKey = key;

    

    this.sharedSecret = await window.crypto.subtle.deriveBits(

      { name: 'ECDH', namedCurve: 'P-256', public: this.clientKey.publicKey },

      this.key.privateKey,

      256

    );


    this.importedKey = await crypto.subtle.importKey(

      'raw',

      this.sharedSecret,

      'aes-ctr',

      false,

      ['encrypt', 'decrypt']

    );

  }

}

如何使用:


(async () => {

  // Generate Keys

  const pro1 = new Protector();

  await pro1.generateKey();

  const pub1 = pro1.getPublicKey();


  const pro2 = new Protector();

  await pro2.generateKey();

  const pub2 = pro2.getPublicKey();


  // Exchange Keys

  await pro1.setRemotePublicKey(pub2);

  await pro2.setRemotePublicKey(pub1);


  // Let`s Encrypt

  const crypted = await pro1.encrypt('Hello World');

  const descrypted = await pro2.decrypt(crypted);

})();


查看完整回答
反對 回復(fù) 2021-12-25
  • 3 回答
  • 0 關(guān)注
  • 380 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號