objective-CでAES暗号化を行う その2
この前作ったのを拡張してみた。"objective-Cで継承ってどうやるんだろう?"という疑問とともに少し練習した次第です。
====
前のやつ
objective-CでAES暗号化を行う - めがねをかけて生きていこう
ファイル操作関係も入れてしまった方が楽じゃないの!ってことです。
cc_crypto.hを継承したcc_file_crypto.h
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
#import "cc_crypto.h"
@interface cc_file_crypto : cc_crypto
{
}
- (int)CCCryptoInPath:(NSString *)inPath OutPath:(NSString*)outPath;
@end
cc_file_crypto.m
#import "cc_file_crypto.h"
@implementation cc_file_crypto
- (int)CCCryptoInPath:(NSString *)inPath OutPath:(NSString*)outPath {
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:inPath];
if (!fileHandle) {
NSLog(@"ファイルがありません.");
return 1;
}
// ファイルの末尾まで読み込み、暗号化を行う
NSData *data = [fileHandle readDataToEndOfFile];
NSData* CryptedData = [super CCCrypto:data];
/** 復号されたデータをファイルとして書き込む **/
NSFileManager *fileManager = [NSFileManager defaultManager];
// ファイルが存在しないか?
if (![fileManager fileExistsAtPath:outPath]) { // yes
// 空のファイルを作成する
BOOL result = [fileManager createFileAtPath:outPath contents:[NSData data] attributes:nil];
if (!result) {
NSLog(@"ファイルの作成に失敗");
return 1;
}
}
// ファイルハンドルを作成する
NSFileHandle *writefileHandle = [NSFileHandle fileHandleForWritingAtPath:outPath];
// ファイルハンドルの作成に失敗したか?
if (!writefileHandle) { // no
NSLog(@"ファイルハンドルの作成に失敗");
return 1;
}
// ファイルに書き込む
[writefileHandle writeData:CryptedData];
return 0;
}
@end
こいつは暗号化するファイルのパスと暗号化後のファイルのパスを渡せば良きに計らってくれる感じです。
テスト用のmain
#import "cc_file_crypto.h"
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
/*
* ファイルの暗号化のテスト
*
*
*/
int main(void) {
unsigned char key[16] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
unsigned char iv[16] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
id cc, cc2;
NSString *filePath = @"/Users/tokihiro/image/yuuyake03.jpg";
NSString *encfilePath = @"/Users/tokihiro/image/E1";
NSString *decfilePath = @"/Users/tokihiro/image/hoge.jpg";
cc = [[cc_file_crypto alloc] initWithOperation:kCCEncrypt key:key iv:iv];
cc2 = [[cc_file_crypto alloc] initWithOperation:kCCDecrypt key:key iv:iv];
int result = [cc CCCryptoInPath:filePath OutPath:encfilePath];
if(result == 1){
NSLog(@"encrypt error");
exit(EXIT_FAILURE);
}
result = [cc2 CCCryptoInPath:encfilePath OutPath:decfilePath];
if(result == 1){
NSLog(@"decrypt error");
exit(EXIT_FAILURE);
}
}
以下実行結果
bash-3.2$ cc cc_file_crypto.m cc_crypto.m main3.m -framework Foundation cc cc_file_crypto.m cc_crypto.m main3.m -framework Foundation bash-3.2$ ./a.out ./a.out 2013-11-19 22:29:14.915 a.out[62471:507] CCCryptorCreate = 0 2013-11-19 22:29:14.917 a.out[62471:507] CCCryptorUpdate = 0, resultLength = 246592 2013-11-19 22:29:14.917 a.out[62471:507] CCCryptorFinal = 0, resultLength = 16 success 2013-11-19 22:29:14.919 a.out[62471:507] CCCryptorCreate = 0 2013-11-19 22:29:14.920 a.out[62471:507] CCCryptorUpdate = 0, resultLength = 246592 2013-11-19 22:29:14.920 a.out[62471:507] CCCryptorFinal = 0, resultLength = 8 success
うまくできたー。