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

我有一些数据,我正在使用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);

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...