ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

ios下使用rsa算法与php进行加解密通讯(3/3)

来源:网络整理     时间:2016-04-21     关键词:rsa算法,php

本篇文章主要介绍了"ios下使用rsa算法与php进行加解密通讯",主要涉及到rsa算法,php方面的内容,对于PHPjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 转载地址:https://blog.yorkgu.me/2011/10/27/rsa-in-ios-using-publick-key-generated-by...

// 我们在前面使用openssl生成的public_key.der文件的base64值,用你自己的替换掉这里#define RSA_KEY_BASE64 @"MIIC5DCCAk2gAwIBAgIJALUk4hrYth9oMA0GCSqGSIb3DQEBBQUAMIGKMQswCQYDVQQGEwJ\
DTjERMA8GA1UECAwIU2hhbmdoYWkxETAPBgNVBAcMCFNoYW5naGFpMQ4wDAYDVQQKDAVCYWl5aTEOMAwGA1UECwwFQmFpeWk\
xEDAOBgNVBAMMB1lvcmsuR3UxIzAhBgkqhkiG9w0BCQEWFGd5cTUzMTk5MjBAZ21haWwuY29tMB4XDTExMTAyNjAyNDUzMlo\
XDTExMTEyNTAyNDUzM1owgYoxCzAJBgNVBAYTAkNOMREwDwYDVQQIDAhTaGFuZ2hhaTERMA8GA1UEBwwIU2hhbmdoYWkxDjA\
MBgNVBAoMBUJhaXlpMQ4wDAYDVQQLDAVCYWl5aTEQMA4GA1UEAwwHWW9yay5HdTEjMCEGCSqGSIb3DQEJARYUZ3lxNTMxOTk\
yMEBnbWFpbC5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAK3cKya7oOi8jVMkRGVuNn/SiSS1y5knKLh6t98JukB\
DJZqo30LVPXXL9nHcYXBTulJgzutCOGQxw8ODfAKvXYxmX7QvLwlJRFEzrqzi3eAM2FYtZZeKbgV6PximOwCG6DqaFqd8X0W\
ezP1B2eWKz4kLIuSUKOmt0h3RpIPkatPBAgMBAAGjUDBOMB0GA1UdDgQWBBSIiLi2mehEgi/MwRZOld1mLlhl7TAfBgNVHSM\
EGDAWgBSIiLi2mehEgi/MwRZOld1mLlhl7TAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAB0GUsssoVEDs9vQxk0\
DzNr8pB0idfI+Farl46OZnW5ZwPu3dvSmhQ+yRdh7Ba54JCyvRy0JcWB+fZgO4QorNRbVVbBSuPg6wLzPuasy9TpmaaYaLLK\
Iena6Z60aFWRwhazd6+hIsKTMTExaWjndblEbhAsjdpg6QMsKurs9+izr"static SecKeyRef _public_key=nil;
+ (SecKeyRef) getPublicKey{ // 从公钥证书文件中获取到公钥的SecKeyRef指针if(_public_key == nil){
    NSData *certificateData = [Base64 decode:RSA_KEY_BASE64];
    SecCertificateRef myCertificate =  SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certificateData);
    SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
    SecTrustRef myTrust;
    OSStatus status = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust);
    SecTrustResultType trustResult;
    if (status == noErr) {
      status = SecTrustEvaluate(myTrust, &trustResult);
    }
    _public_key = SecTrustCopyPublicKey(myTrust);
    CFRelease(myCertificate);
    CFRelease(myPolicy);
    CFRelease(myTrust);
  }
  return _public_key;
}

+ (NSData*) rsaEncryptString:(NSString*) string{
  SecKeyRef key = [self getPublicKey];
  size_t cipherBufferSize = SecKeyGetBlockSize(key);
  uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
  NSData *stringBytes = [string dataUsingEncoding:NSUTF8StringEncoding];
  size_t blockSize = cipherBufferSize - 11;
  size_t blockCount = (size_t)ceil([stringBytes length] / (double)blockSize);
  NSMutableData *encryptedData = [[[NSMutableData alloc] init] autorelease];
  for (int i=0; iint bufferSize = MIN(blockSize,[stringBytes length] - i * blockSize);
    NSData *buffer = [stringBytes subdataWithRange:NSMakeRange(i * blockSize, bufferSize)];
    OSStatus status = SecKeyEncrypt(key, kSecPaddingPKCS1, (const uint8_t *)[buffer bytes],
                                    [buffer length], cipherBuffer, &cipherBufferSize);
    if (status == noErr){
      NSData *encryptedBytes = [[NSData alloc] initWithBytes:(constvoid *)cipherBuffer length:cipherBufferSize];
      [encryptedData appendData:encryptedBytes];
      [encryptedBytes release];
    }else{
      if (cipherBuffer) free(cipherBuffer);
      return nil;
    }
  }
  if (cipherBuffer) free(cipherBuffer);
//  NSLog(@"Encrypted text (%d bytes): %@", [encryptedData length], [encryptedData description]);//  NSLog(@"Encrypted text base64: %@", [Base64 encode:encryptedData]);return encryptedData;
}
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介绍了ios下使用rsa算法与php进行加解密通讯,包括了rsa算法,php方面的内容,希望对PHPjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

    本文网址链接:http://www.codes51.com/article/detail_777157_3.html

    相关图片

    相关文章