我有一些数据,我正在使用Elliptic Curve私钥在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算法验证数据?
解决方法
字节顺序不匹配:
> iOS是小端.创建nonceData的方式,保留此顺序.
>在Java方面,ByteBuffer默认为big endian,独立于底层操作系统/硬件.
所以你需要改变字节顺序:
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.putLong(nonce);