BouncyCastle,桌面/ Android上签名的不同行为

时间:2016-08-15 21:46:53

标签: java android cryptography digital-signature bouncycastle

我使用以下代码来签署数据:

if (client.IsConnected)
{
    var player = GamesClass.Players.GetCurrentPlayer(client);
    Console.WriteLine(player.PlayerId);
    Console.WriteLine(player.DisplayName);
}

(检查标志的代码非常相似)

在桌面上,代码工作正常,可以使用其他平台检查签名(使用python,node,...测试)。

在Android上,“saltLength”未被读取,并被强制为32位,因此如果使用另一个“saltLength”,我无法在其他平台上签署/验证设备的签名。 (我通过在桌面上强制执行saltLength值来验证它。)

我真的不知道如何调试它,同样的Java代码在桌面或Android上启动时有不同的行为。知道如何在Android上强制使用“saltLength”吗?

1 个答案:

答案 0 :(得分:1)

好的,经过台式机和Android端的多次测试...... 我仍然无法找到问题的根源(我猜测java.security.Signature$SignatureImpl的不同实现但不确定......)

然而,我找到了解决遇到同样问题的人的工作。 我创建了以下类:

public class SHA256withRSACustomPadding extends PSSSignatureSpi {
    public SHA256withRSACustomPadding(int padding) {
        super(new RSABlindedEngine(), new PSSParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), padding, 1));
    }

    public void initSign(PrivateKey privateKey) throws InvalidKeyException {
        engineInitSign(privateKey);
    }

    public void update(byte[] data) throws SignatureException {
        engineUpdate(data,0,data.length);
    }

    public byte[] sign() throws SignatureException {
        return engineSign();
    }

    public void initVerify(PublicKey publicKey) throws InvalidKeyException {
        engineInitVerify(publicKey);
    }

    public boolean verify(byte[] data) throws SignatureException {
        return engineVerify(data);
    }
}

并使用以下代码更新了我之前的代码:

static public byte[] sign(byte[] data, PrivateKey privateKey, int saltLength) throws Exception {
    SHA256withRSACustomPadding instance = new SHA256withRSACustomPadding(padding);
    instance.initSign(privateKey);
    instance.update(data);
    return instance.sign();
}