RSA登录IOS(SWIFT)并在Java中验证 [英] RSA Sign on iOS (Swift) and Verify in Java

查看:32
本文介绍了RSA登录IOS(SWIFT)并在Java中验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS上签名数据并在Java中验证数据时遇到问题。

到目前为止我尝试的内容:

iOS(SWIFT):

 let text = "Hello World!"
 let publicKey = heimdall.publicKeyComponents()!
 let hashedText = text.sha512()

 let modulus = publicKey.modulus.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
 let exponent = publicKey.exponent.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
 let signature = heimdall.sign(hashedText.dataUsingEncoding(NSUTF8StringEncoding)!)!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
 let verSig = NSData(base64EncodedString: signature, options: NSDataBase64DecodingOptions(rawValue: 0))

 let message: NSDictionary = ["text": text, "signature": signature, "modulus": modulus, "exponent": exponent ]

我正在使用SWIFTHeimdall处理RSA-Key,报文以JSON格式通过http发送。

在Java端:

final byte[] signature = Base64.decodeBase64( message.getSignature() );
final byte[] modulus = Base64.decodeBase64( message.getModulus() ) ;
final byte[] exponent = Base64.decodeBase64( message.getExponent() );      
final String messageText = message.getText();      
final Signature sig = Signature.getInstance( "SHA512withRSA" );
final KeyFactory keyMaker = KeyFactory.getInstance( "RSA" );
final RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( new BigInteger( modulus ), 
                                                          new BigInteger( exponent ) );      
final RSAPublicKey pubKey = (RSAPublicKey)keyMaker.generatePublic( pubKeySpec );

sig.initVerify( pubKey );
sig.update( messageText.getBytes() );
final boolean result = sig.verify( signature );

但结果始终为假:-/ 据我所知,数据传输是正确的。 也许我在摆弄编码。

推荐答案

我解决了问题,删除了消息的初始设置(在iOS上)。那么一切都很好。Heimdall.sign正在对数据进行哈希处理,然后再进行签名。

这篇关于RSA登录IOS(SWIFT)并在Java中验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆