需要将以下签名验证 python 代码转换为 Android?

need to convert following Signnature verification python code to Android?

我创建了 python3 生成 RSA 密钥对的应用程序。

from Crypto.PublicKey import RSA

print("--Private Key Generate--")

key = RSA.generate(2048)
private_key = key.export_key()
file_out = open("key/private.pem", "wb")
file_out.write(private_key)
file_out.close()

print("--Public Key Generate--")

public_key = key.publickey().export_key()
file_out_1 = open("key/receiver.pem", "wb")
file_out_1.write(public_key)
file_out_1.close()

print("key Generated")

我使用 python 签署了一些数据并创建了一个签名。使用python也验证成功。

 def sign(data):
    private_key = RSA.import_key(open('key/private.pem').read())
    h = SHA256.new(data)
    signature =  base64.b64encode(pss.new(private_key).sign(h))
    print("signature generate")
    verify(data,signature)
    return signature


def verify(recive_Data ,signature):
    public_key = RSA.import_key(open('key/receiver.pem').read())
    h =  SHA256.new(recive_Data)
    verifier = pss.new(public_key)
    try:
        verifier.verify(h, base64.b64decode(signature))
        print("The signature is authentic")
    except (ValueError, TypeError):
        print ("The signature is not authentic.")

但实际上,我的验证实现在 Android(最小 SDK 23,目标 SDK 29)。所以,我需要把这个验证码转换成Android。我尝试使用以下代码,但未验证成功。需要一些专家的帮助。

public class SecurityHelper {

    private static String getKey(InputStream filename) throws IOException {
        // Read key from file
        String strKeyPEM = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(filename));
        String line;
        while ((line = br.readLine()) != null) {
            strKeyPEM += line + "\n";
        }
        br.close();
       // System.out.println(strKeyPEM);
        return strKeyPEM;
    }


    public static PublicKey getPublicKey(InputStream filename) throws IOException, GeneralSecurityException {
        String publicKeyPEM = getKey(filename);
        return getPublicKeyFromString(publicKeyPEM);
    }

    public static PublicKey getPublicKeyFromString(String key) throws IOException, GeneralSecurityException {
        String publicKeyPEM = key;
        publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----\n", "");
        publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
        System.out.println(publicKeyPEM);
        byte[] encoded = Base64.decode(publicKeyPEM ,Base64.CRLF);
      //  System.out.println(encoded);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PublicKey pubKey = kf.generatePublic(new X509EncodedKeySpec(encoded));
        System.out.println(pubKey);
        return pubKey;
    }


  public static boolean verify(PublicKey publicKey, String message, String signature) throws SignatureException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException {

        Signature sign = Signature.getInstance("SHA256withRSA");
        sign.initVerify(publicKey);
        sign.update(message.getBytes("UTF-8"));
        System.out.println(message);
        return  sign.verify(Base64.decode(signature,Base64.CRLF));
    }


}

在Python代码PSS and in the Android code Pkcs#1 v1.5, see for the difference RFC 8017. Replace in the Android code SHA256withRSA with SHA256withRSA/PSS中使用了不同的填充。

更新:

尽管根据 Android 文档,SHA256withRSA/PSS 从 API 级别 23+ 开始得到支持,但 InvalidKeyException (No provider supports the provided key) 会被抛出 API 23 级,对于 API 24+ 级,它按规定工作。

对于 API 级别 23 的一个可能的 解决方法 是对 Android Studio 使用 BouncyCastle, which then has to be included as a dependency in the Android project (details depend on the IDE, e.g. :

implementation 'org.bouncycastle:bcprov-jdk15on:1.64'

添加BC Provider前,必须先删除预装版本。要使用的模式是 SHA256withRSAandMGF1 (see section Signature Algorithms):

Security.removeProvider("BC"); 
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 
Signature sign = Signature.getInstance("SHA256withRSAandMGF1"); 
// Go ahead as for schema SHA256withRSA/PSS...

注意:SpongyCastle 是另一种可能性。此处预装的 BC Provider 不必删除。架构是 SHA256withRSA/PSS.