在iOS上签名的数据无法在Java中验证
I have some data that I'm signing on iOS with SecKeyRawSign
using Elliptic Curve private key. However, verifying that data in Java using Signature.verify()
returns false
我有一些数据,我正在使用Elliptic Curve私钥在SecKeyRawSign上签名。但是,使用Signature.verify()验证Java中的数据是否返回false
The data is a random 64 bit integer, split into bytes like so
数据是一个随机的64位整数,像这样分成字节
uint64_t nonce = (some 64 bit integer)
NSData *nonceData = [NSData dataWithBytes: &nonce length: sizeof(nonce)];
From that data I'm creating a SHA256 digest
根据该数据,我正在创建一个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];
and then signing it with private key
然后使用私钥对其进行签名
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];
}
Everything appears to work fine.
一切似乎都很好。
Then, in Java server, I'm trying to verify that signed data
然后,在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 );
At this point, signer.verify()
returns false
此时,signer.verify()返回false
I also tried to sign plain data, instead of SHA256 digest, but that doesn't work either.
我也尝试签署普通数据,而不是SHA256摘要,但这也不起作用。
What am I missing? Am I signing the data correctly? Am I using correct padding? Is there something else to be done with data to be able to verify it with SHA256withECDSA
algorithm?
我错过了什么?我正确签署数据吗?我使用正确的填充物吗?是否还有其他事情可以使用SHA256withECDSA算法验证数据?
4 个解决方案
#1
1
The byte ordering does not match:
字节顺序不匹配:
- iOS is little endian. The way you create
nonceData
, this order is retained. - On the Java side,
ByteBuffer
defaults to big endian, independent of the underlying operating system / hardware.
iOS是小端。创建nonceData的方式,保留此顺序。
在Java方面,ByteBuffer默认为big endian,独立于底层操作系统/硬件。
So you need to change the byte order:
所以你需要改变字节顺序:
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(nonce);
更多相关文章
- 求一段与读取数据库数据,写入一个数组的javascript 代码!谢谢!!
- 如何通过Java将Digital Persona的模板保存到数据库中
- 命运被转折改变--掌握java高性能分布式服务和海量大数据技术体系
- java 自动拆箱与装箱(基本数据类型与引用类型)
- 云星数据---Apache Flink实战系列(精品版)】:Flink流处理API详解
- 使用SWT/JFace与WindowBuilder绑定数据的参考资料
- 01500105_MLDN-魔乐科技-李兴华【Java核心技术】_JDBC连接Oracle
- 数据结构-java与c实现带头结点的单链表