2017-07-19 101 views
0

我是Angular 2中的一個新手。我試圖實現一個登錄表單,它在某些加密步驟後發送emailid和密碼到服務器。Angular 2:window.crypto.subtle.importKey在'localhost'中工作,但不在'ip'上工作

我已通過使用AES-CTR從實現AES-ECB,

https://github.com/diafygi/webcrypto-examples

我已經使用了 'importKey' 和 '加密' 方法如下,

public deriveAKey(input, encryptKey, cryptoIV) { 

    var ref: TopDivComponent = this; 
    console.log('Testing before importKey...'); 

    window.crypto.subtle.importKey(
     "raw", 
     ref.stringToArrayBuffer(encryptKey), 
     { 
      name: "AES-CTR", 
     }, 
     true, 
     ["encrypt"] 
    ).then(function (key) { 
     console.log('Inside then...'); 
     var newvar = ref.stringToArrayBuffer(cryptoIV); 
     var encrypt = window.crypto.subtle.encrypt(
      { 
       name: "AES-CTR", 
       counter: newvar, 
       length: 128, 
      }, 
      key, 
      ref.stringToArrayBuffer(input) 
     ).then(function (encrypted) { 
      var temp = ref.arrayBufferToString(encrypted); 
      console.log('Encrypted First: ' + encrypted); 
      console.log('Temp: ' + temp); 
      console.log('Key: ' + key); 
      let fin_encrypted = btoa(temp); 
      // console.log('Encrypted Snc/d: ' + fin_encrypted); 
      ref.response(fin_encrypted); 
      // console.log('From deriveKey: ' + fin_encrypted); 
     }); 
    }); 
} 

我使用本地服務器來獲取響應。使用本地主機時一切正常。請求和響應從服務器正確發送和獲取。但是,通過IP連接時,它顯示錯誤「NotSupportedError:只允許安全的來源」

當我用鉻金絲雀,它表示importKey方法不被識別。所以當我用Chrome對其進行安慰時,控件並沒有超越importKey方法。可能是什麼問題?

回答

1

Chrome限制WebCryptographyApi的使用來確保原點。它的意思是'https'。 localhost是啓用開發的特殊地址。因此,要在真實環境中使用WebCrypto,您需要設置SSL/TLS服務器

相關問題