在iOS上签名的数据无法在Java中验证

时间:2018-05-21 11:53:41

标签: java ios objective-c java-security commoncrypto

我有一些数据,我使用Elliptic Curve私钥在​​iOS上使用SecKeyRawSign进行签名。但是,使用Signature.verify()验证Java中的数据会返回false

数据是一个随机的64位整数,分成像这样的字节

uint64_t nonce = (some 64 bit integer)
NSData *nonceData = [NSData dataWithBytes: &nonce length: sizeof(nonce)];

根据这些数据,我创建了一个SHA256摘要

int digestLength = CC_SHA256_DIGEST_LENGTH;
uint8_t *digest = malloc(digestLength);
CC_SHA256(nonceData.bytes, (CC_LONG)nonceData.length, digest);
NSData *digestData = [NSData dataWithBytes:digest length:digestLength];

然后使用私钥对其进行签名

size_t signedBufferSize = kMaxCipherBufferSize;
uint8_t *signedBuffer = malloc(kMaxCipherBufferSize);

OSStatus status = SecKeyRawSign(privateKeyRef,
                                kSecPaddingPKCS1SHA256,
                                (const uint8_t *)digestData.bytes,
                                digestData.length,
                                &signedBuffer[0],
                                &signedBufferSize);

NSData *signedData = nil;
if (status == errSecSuccess) {
    signedData = [NSData dataWithBytes:signedBuffer length:signedBufferSize];
}

一切似乎都很好。

然后,在Java服务器中,我试图验证签名数据

PublicKey publicKey = (a public key sent from iOS, X509 encoded)

Long nonce = (64 bit integer sent from iOS)
String signedNonce = (base64 encoded signed data)

ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(nonce);
byte[] nonceBytes = buffer.array();
byte[] signedNonceBytes = Base64.getDecoder().decode(signedNonce.getBytes());

Signature signer = Signature.getInstance( "SHA256withECDSA" );
signer.initVerify( publicKey );
signer.update( nonceBytes );
Boolean isVerified = signer.verify( signedNonceBytes );

此时,signer.verify()返回false

我还尝试签署普通数据,而不是SHA256摘要,但这也不起作用。

我错过了什么?我是否正确签署了数据?我使用正确的填充物吗?是否还有其他事情可以使用SHA256withECDSA算法验证数据?

4 个答案:

答案 0 :(得分:2)

字节顺序不匹配:

  • iOS是 little endian 。您创建nonceData的方式将保留此订单。
  • 在Java方面,ByteBuffer默认为 big endian ,独立于底层操作系统/硬件。

所以你需要改变字节顺序:

ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(nonce);

答案 1 :(得分:0)

我是一个java人,所以我不能对iOS方面说些什么,但是可以使用评论的假设快速检查java方面:

// Generate a new random EC keypair for testing
KeyPair keys = KeyPairGenerator.getInstance("EC").generateKeyPair();
PrivateKey privateKey = keys.getPrivate();
PublicKey publicKey = keys.getPublic();

// Generate a Random nonce to test with
byte[] nonceBytes = new byte[8]; // (some 64 bit integer)
new Random(System.nanoTime()).nextBytes(nonceBytes);

// sign     
Signature sign = Signature.getInstance("SHA256withECDSA");
sign.initSign(privateKey);
sign.update(nonceBytes);
byte[] signature = sign.sign();

//verify
Signature verify = Signature.getInstance("SHA256withECDSA");
verify.initVerify(publicKey);
verify.update(nonceBytes);
Boolean isVerified = verify.verify(signature);

// print results
System.out.println("nonce used  ::" + Base64.getEncoder().encodeToString(nonceBytes));
System.out.println("Signed nonce ::" + Base64.getEncoder().encodeToString(signature));
System.out.println("nonce used ::" + isVerified);

正如您所期望的那样,上面的代码将始终返回验证签名。检查您的假设是否准确,并验证所使用的密钥在两个方面是否正确。

答案 2 :(得分:0)

我可以建议你使用一些可用于iOS和JAVA端的加密库(f.e.:https://github.com/VirgilSecurity/virgil-crypto)。这将确保算法和块类型(等)在两种情况下都相同,您不再需要担心它。我相信你会在GitHub上找到很多加密库。

答案 3 :(得分:0)

在getBytes()中,您可以使用java.nio.charset.StandardCharsets指定编码技术。并对解码器做同样的事情。

https://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html