ios php rsa,RSA 加密 iOS

在iOS端使用RSA加密的记录

一、需求:

SDK开发,使用RSA加密和后台进行数据交互,后台是PHP

要求:

1、post请求,客户端放公钥,私钥放后台

2、发送数据:客户端的json数据—>RSA加密数据—>base64编码数据—>php后台

3、接收数据:php后台base64编码数据—>客户端base64解码数据—>RSA解密数据—>json数据

4、使用的密钥是1024位,要和后台统一,解密长度128,加密长度117

5、rsa_public_key.pem文件后台给的

二、探究过程

三、遇到的问题

1、'openssl/asn1.h' file not found‘

解决方法:

Header Search Paths 添加这种格式的路径

"$(SRCROOT)/XSFH_game/Lib"

a863443198ff

image.png

2、base编码发给后台,后台接收少+的问题

我这边发给后台加密后的base64编码,后台接收后,base64编码中的+变成了空格,导致后台解码错误,这是后台的问题

解决方法:

PHP学习 base64_encode +号变空格

3、后台数据接收到问题

可能是php接收数据的问题,我放在HTTPBody里的传给它的是一字符串,后台说没法解析,要那种类似于json的格式,最后给它传了个如下格式的代码stringWithFormat:@"baseKey=%@",encryptedString]; ,baseKey可以随便写,就类似于json的key值

NSString *encryptedString = [[XSFHRSATool shareInstance] encryptStr:paramStr WithRSAKeyType:KeyTypePublic];

NSString *keyAndValue = [NSString stringWithFormat:@"baseKey=%@",encryptedString];

request.HTTPBody = [keyAndValue dataUsingEncoding:NSUTF8StringEncoding];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS和Java都支持RSA加密,但是具体实现可能会有所不同。下面是一个简单的示例: 在iOS中使用RSA加密: ```objective-c // 加载公钥文件 NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"der"]; NSData *publicKeyData = [[NSData alloc] initWithContentsOfFile:publicKeyPath]; SecCertificateRef certificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)publicKeyData); // 获取公钥 SecKeyRef publicKey = NULL; SecTrustRef trust; SecPolicyRef policy; policy = SecPolicyCreateBasicX509(); OSStatus status = SecTrustCreateWithCertificates(certificate, policy, &trust); if (status == errSecSuccess) { SecTrustResultType resultType; status = SecTrustEvaluate(trust, &resultType); publicKey = SecTrustCopyPublicKey(trust); } // 使用公钥进行加密 NSString *plainText = @"Hello, world!"; NSData *plainData = [plainText dataUsingEncoding:NSUTF8StringEncoding]; size_t cipherBufferSize = SecKeyGetBlockSize(publicKey); uint8_t *cipherBuffer = malloc(cipherBufferSize); memset(cipherBuffer, 0, cipherBufferSize); OSStatus status = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, [plainData bytes], [plainData length], cipherBuffer, &cipherBufferSize); NSData *cipherData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize]; NSLog(@"Cipher text: %@", [cipherData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]); ``` 在Java中使用RSA加密: ```java // 加载公钥文件 String publicKeyPath = "/path/to/public_key.der"; byte[] publicKeyBytes = Files.readAllBytes(Paths.get(publicKeyPath)); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(keySpec); // 使用公钥进行加密 String plainText = "Hello, world!"; byte[] plainBytes = plainText.getBytes(StandardCharsets.UTF_8); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherBytes = cipher.doFinal(plainBytes); System.out.println(Base64.getEncoder().encodeToString(cipherBytes)); ``` 需要注意的是,在iOS中使用SecKeyEncrypt函数进行加密时,需要指定填充方式为kSecPaddingPKCS1;在Java中使用Cipher类进行加密时,需要指定填充方式为PKCS1Padding。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值