我使用以下代码来签署数据:
static public byte[] sign(byte[] data, PrivateKey privateKey, int saltLength) throws Exception {
Signature instance = Signature.getInstance("SHA256withRSA/PSS", provider);
MGF1ParameterSpec mgf1ParameterSpec = new MGF1ParameterSpec("SHA-256");
PSSParameterSpec pssParameterSpec = new PSSParameterSpec("SHA-256", "MGF1",mgf1ParameterSpec , saltLength, 1);
instance.setParameter(pssParameterSpec);
instance.initSign(privateKey);
instance.update(data);
return instance.sign();
}
(检查标志的代码非常相似)
在桌面上,代码运行良好,可以用其他平台检查签名(用 python、node 等测试)。
在 Android 上,“saltLength”未被读取,并且被强制为 32 位,因此如果使用另一个“saltLength”,我无法从其他平台上的设备签署/验证签名。 (我通过在桌面上强制使用 saltLength 值来验证它)。
我真的不知道如何调试它,同样的 Java 代码在桌面或 Android 上启动时有不同的行为。知道如何在 Android 上强制使用“saltLength”吗?
最佳答案
好的,在桌面端和 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();
}
关于java - BouncyCaSTLe,桌面/安卓签名的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38963560/