无法使用SubTecryPto Web Crypto API解密使用PyCryptodome加密的RSA-OAEP消息

无法使用SubTecryPto Web Crypto API解密使用PyCryptodome加密的RSA-OAEP消息,rsa,webcrypto-api,pycryptodome,subtlecrypto,Rsa,Webcrypto Api,Pycryptodome,Subtlecrypto,在服务器端,我使用RSA-OAEP对消息进行加密(使用SHA-256)。 我试图在客户端使用Web Crypto API对消息进行解密,但它给了我一个DomeException错误,没有进一步的细节。 在SubTecryPto上,我可以毫无问题地导入PyCryptodome中生成的私钥,但是当我尝试解密消息时,它会给我错误 我还尝试导入在客户端PyCryptodome上生成的公钥,以使用SubTecryPto加密相同的消息。在这种情况下,我可以使用与以前相同的流,毫无问题地对其进行解密 这两个库

在服务器端,我使用RSA-OAEP对消息进行加密(使用SHA-256)。
我试图在客户端使用Web Crypto API对消息进行解密,但它给了我一个DomeException错误,没有进一步的细节。
在SubTecryPto上,我可以毫无问题地导入PyCryptodome中生成的私钥,但是当我尝试解密消息时,它会给我错误

我还尝试导入在客户端PyCryptodome上生成的公钥,以使用SubTecryPto加密相同的消息。在这种情况下,我可以使用与以前相同的流,毫无问题地对其进行解密

这两个库之间的RSA-OAEP算法是否不兼容? 我注意到PyCryptodome在其各自的文档中引用了SublecryPto

编辑

  • 服务器端代码(pycryptodome==3.9.8):

  • 客户端代码

    private decryptRSAString (encryptedText: string, privateKey: string) : Observable<ArrayBuffer> {
    
      return Observable.create ((observer: any) => {
    
        let keyBuffer: ArrayBuffer = this.str2ab(window.atob(privateKey));
        let encryptedTextBuffer: ArrayBuffer = this.str2ab(window.atob(encryptedText));
        let algorithmParams: RsaHashedImportParams = {
          name: "RSA-OAEP",
          hash: "SHA-256"
        };
        window.crypto.subtle.importKey(
          'pkcs8',
          keyBuffer,
          algorithmParams,
          true,
          ["decrypt"]
        ).then (
          (cryptoKey: CryptoKey) => {
            window.crypto.subtle.decrypt(
              {
                name: "RSA-OAEP"
              },
              cryptoKey,
              encryptedTextBuffer
            ).then (
              (decryptedMessage: ArrayBuffer) => {
                observer.next (decryptedMessage);
                observer.complete();
              },
              (error: any) => {
                observer.error (error)
              }
            )
          },
          (error: any) => {
            observer.error (error)
          }
        );
      });
    }
    
    私有解密字符串(encryptedText:string,privateKey:string):可观察{
    return Observable.create((observator:any)=>{
    let keyBuffer:ArrayBuffer=this.str2ab(window.atob(privateKey));
    让encryptedTextBuffer:ArrayBuffer=this.str2ab(window.atob(encryptedText));
    let algorithmParams:RsaHashedImportParams={
    名称:“RSA-OAEP”,
    散列:“SHA-256”
    };
    window.crypto.division.importKey(
    “pkcs8”,
    键缓冲区,
    算法参数,
    是的,
    [“解密”]
    ).那么(
    (加密密钥:加密密钥)=>{
    window.crypto.division.decrypt(
    {
    名称:“RSA-OAEP”
    },
    加密密钥,
    加密文本缓冲区
    ).那么(
    (解密消息:ArrayBuffer)=>{
    observer.next(解密消息);
    observer.complete();
    },
    (错误:any)=>{
    observer.error(错误)
    }
    )
    },
    (错误:any)=>{
    observer.error(错误)
    }
    );
    });
    }
    
PyCryptodome不将SHA-256作为OAEP的默认摘要,但SHA-1、。因此,必须在WebCrypto侧使用SHA-1:

当然,您也可以在PyCryptodome端应用SHA-256,然后在WebCryptop端不需要任何更改


通过双方的一致摘要,我可以使用您的WebCryptop代码成功解密密文,我以前使用您的PyCryptodome代码(使用我自己的密钥)生成了该代码。

欢迎使用Stackoverflow。看到你正在挣扎的代码可能会很有帮助,所有其他答案都是某种“争论”。用客户端代码编辑。该代码用于解密使用同一库加密的消息,但正如我前面解释的,当我尝试解密使用PyCryptodome加密的消息时,它给出了DomeException。已解决,非常感谢!
private decryptRSAString (encryptedText: string, privateKey: string) : Observable<ArrayBuffer> {

  return Observable.create ((observer: any) => {

    let keyBuffer: ArrayBuffer = this.str2ab(window.atob(privateKey));
    let encryptedTextBuffer: ArrayBuffer = this.str2ab(window.atob(encryptedText));
    let algorithmParams: RsaHashedImportParams = {
      name: "RSA-OAEP",
      hash: "SHA-256"
    };
    window.crypto.subtle.importKey(
      'pkcs8',
      keyBuffer,
      algorithmParams,
      true,
      ["decrypt"]
    ).then (
      (cryptoKey: CryptoKey) => {
        window.crypto.subtle.decrypt(
          {
            name: "RSA-OAEP"
          },
          cryptoKey,
          encryptedTextBuffer
        ).then (
          (decryptedMessage: ArrayBuffer) => {
            observer.next (decryptedMessage);
            observer.complete();
          },
          (error: any) => {
            observer.error (error)
          }
        )
      },
      (error: any) => {
        observer.error (error)
      }
    );
  });
}
let algorithmParams: RsaHashedImportParams = {
    name: "RSA-OAEP",
    hash: "SHA-1"
};
from Crypto.Hash import SHA256
...
rsa_encryption_cipher = PKCS1_OAEP.new(key, SHA256) # default: Crypto.Hash.SHA1