티스토리 뷰

반응형

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

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
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
글 보관함