Nenhuma Descrição

XRSA.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #import "XRSA.h"
  2. @implementation XRSA
  3. - (XRSA *)initWithData:(NSData *)keyData {
  4. self = [super init];
  5. if (self) {
  6. if (keyData == nil) {
  7. return nil;
  8. }
  9. certificate = SecCertificateCreateWithData(kCFAllocatorDefault, ( __bridge CFDataRef) keyData);
  10. if (certificate == nil) {
  11. NSLog(@"Can not read certificate from data");
  12. return nil;
  13. }
  14. policy = SecPolicyCreateBasicX509();
  15. OSStatus returnCode = SecTrustCreateWithCertificates(certificate, policy, &trust);
  16. if (returnCode != 0) {
  17. NSLog(@"SecTrustCreateWithCertificates fail. Error Code: %d", (int)returnCode);
  18. return nil;
  19. }
  20. SecTrustResultType trustResultType;
  21. returnCode = SecTrustEvaluate(trust, &trustResultType);
  22. if (returnCode != 0) {
  23. return nil;
  24. }
  25. publicKey = SecTrustCopyPublicKey(trust);
  26. if (publicKey == nil) {
  27. NSLog(@"SecTrustCopyPublicKey fail");
  28. return nil;
  29. }
  30. maxPlainLen = SecKeyGetBlockSize(publicKey) - 12;
  31. }
  32. return self;
  33. }
  34. - (XRSA *)initWithPublicKey:(NSString *)publicKeyPath {
  35. if (publicKeyPath == nil) {
  36. NSLog(@"Can not find %@", publicKeyPath);
  37. return nil;
  38. }
  39. NSData *publicKeyFileContent = [NSData dataWithContentsOfFile:publicKeyPath];
  40. return [self initWithData:publicKeyFileContent];
  41. }
  42. - (NSData *) encryptWithData:(NSData *)content {
  43. size_t plainLen = [content length];
  44. if (plainLen > maxPlainLen) {
  45. NSLog(@"content(%ld) is too long, must < %ld", plainLen, maxPlainLen);
  46. return nil;
  47. }
  48. void *plain = malloc(plainLen);
  49. [content getBytes:plain
  50. length:plainLen];
  51. size_t cipherLen = 128; // currently RSA key length is set to 128 bytes
  52. void *cipher = malloc(cipherLen);
  53. OSStatus returnCode = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plain,
  54. plainLen, cipher, &cipherLen);
  55. NSData *result = nil;
  56. if (returnCode != 0) {
  57. NSLog(@"SecKeyEncrypt fail. Error Code: %d", (int)returnCode);
  58. }
  59. else {
  60. result = [NSData dataWithBytes:cipher
  61. length:cipherLen];
  62. }
  63. free(plain);
  64. free(cipher);
  65. return result;
  66. }
  67. - (NSData *) encryptWithString:(NSString *)content {
  68. return [self encryptWithData:[content dataUsingEncoding:NSUTF8StringEncoding]];
  69. }
  70. - (NSString *) encryptToString:(NSString *)content {
  71. NSData *data = [self encryptWithString:content];
  72. return [self base64forData:data];
  73. }
  74. // convert NSData to NSString
  75. - (NSString*)base64forData:(NSData*)theData {
  76. const uint8_t* input = (const uint8_t*)[theData bytes];
  77. NSInteger length = [theData length];
  78. static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  79. NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
  80. uint8_t* output = (uint8_t*)data.mutableBytes;
  81. NSInteger i;
  82. for (i=0; i < length; i += 3) {
  83. NSInteger value = 0;
  84. NSInteger j;
  85. for (j = i; j < (i + 3); j++) {
  86. value <<= 8;
  87. if (j < length) {
  88. value |= (0xFF & input[j]);
  89. }
  90. }
  91. NSInteger theIndex = (i / 3) * 4;
  92. output[theIndex + 0] = table[(value >> 18) & 0x3F];
  93. output[theIndex + 1] = table[(value >> 12) & 0x3F];
  94. output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
  95. output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
  96. }
  97. return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
  98. }
  99. - (void)dealloc{
  100. CFRelease(certificate);
  101. CFRelease(trust);
  102. CFRelease(policy);
  103. CFRelease(publicKey);
  104. }
  105. @end