티스토리 뷰
PEM 파일로 패스워드 암호화 하기
NSString *strData = [NSString stringWithFormat:@"{\"key\" : \"%@\"}",DEFAULT_PASSWORD];
ExtractEncryptKey *encrypt = [[ExtractEncryptKey alloc]init];
/* Open and parse the cert*/
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"pemfilename" ofType:@"pem"];
NSString* test = [encrypt encryptWithPublicKeyMessage:strData resourcePath:resourcePath];
NSLog(@"test = %@",test);
/**
@file ExtractEncryptKey.h
@brief EncryptKey
*/
#import <Foundation/Foundation.h>
@interface ExtractEncryptKey : NSObject
- (NSString *)encryptWithPublicKeyMessage:(NSString *) message resourcePath:(NSString*)resourcePath;
@end
/**
@file ExtractEncryptKey.m
@brief EncryptKey
*/
#import "ExtractEncryptKey.h"
@implementation ExtractEncryptKey
- (NSString *)encryptWithPublicKeyMessage:(NSString *) message resourcePath:(NSString*)resourcePath
{
NSLog(@"encrypting...");
NSData *inputData = [message dataUsingEncoding:NSUTF8StringEncoding];
const void *bytes = [inputData bytes];
NSUInteger length = [inputData length];
uint8_t *plainText = malloc(length);
memcpy(plainText, bytes, length);
NSString *txtFileContents = [NSString stringWithContentsOfFile:resourcePath encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"File: %@",txtFileContents);
//NSData *certData = [NSData dataWithContentsOfFile:resourcePath];
NSData *certData = [[NSData alloc] initWithBase64EncodedString:txtFileContents options:1];
SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)certData);
SecPolicyRef policy = SecPolicyCreateBasicX509();
SecTrustRef trust;
OSStatus status = SecTrustCreateWithCertificates(cert, policy, &trust);
/* You can ignore the SecTrustResultType, but you have to run SecTrustEvaluate
* before you can get the public key */
SecTrustResultType trustResult;
if (status == noErr) {
status = SecTrustEvaluate(trust, &trustResult);
}
/* Now grab the public key from the cert */
SecKeyRef publicKey = SecTrustCopyPublicKey(trust);
// /* Now grab the public key from the cert */
// SecKeyRef publicKey = [self getPublicKeyRef ];
/* allocate a buffer to hold the cipher text */
size_t cipherBufferSize;
uint8_t *cipherBuffer;
cipherBufferSize = SecKeyGetBlockSize(publicKey);
cipherBuffer = malloc(cipherBufferSize);
/* encrypt!! */
SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plainText, length, cipherBuffer, &cipherBufferSize);
NSData *data = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
/* Free the Security Framework Five! */
CFRelease(cert);
CFRelease(policy);
CFRelease(trust);
CFRelease(publicKey);
free(cipherBuffer);
NSLog(@"encrypted");
return [[NSString alloc]initWithData:[data base64EncodedDataWithOptions:0] encoding:NSUTF8StringEncoding]; //[data encodeBase64ForData];
}
@end
'Mobile > iOS' 카테고리의 다른 글
iOS 이중인증 할 수 없을 때 (0) | 2019.03.22 |
---|---|
swift online playground 사이트 (0) | 2017.12.01 |
IPA설치 Adhoc 설치방법 (0) | 2017.11.24 |
[SWIFT]오픈소스 스위프트 홈페이지 (0) | 2017.11.11 |
Device Screen Sizes and Orientations 아이폰 아이패드 해상도 (0) | 2017.09.21 |
- Total
- Today
- Yesterday
- youtube
- 성경듣기
- Android
- 통독
- 1일1독
- genesis
- iPhone
- AWS
- ChatGPT
- 반응형웹
- 성경통독
- 말씀
- SWIFT
- bible
- 아이폰
- 플러터
- 성경책
- 창세기
- IOS
- react
- VR
- 성경
- 유튜브
- MariaDB
- mysql
- flutter
- 오디오북
- 안드로이드
- 아이폰개발
- 성경읽기
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |