RSA Sign on iOS (Swift) and Verify in Java(RSA登录IOS(SWIFT)并在Java中验证)
本文介绍了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中验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:RSA登录IOS(SWIFT)并在Java中验证
基础教程推荐
猜你喜欢
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
