5
2014/2/23 Cocoa #27 QuickLook file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 1/5

Cocoa勉強会#28-OpenSSLで暗号化したファイルを復号する

Embed Size (px)

DESCRIPTION

Cocoa勉強会#28 2008/4/12 OpenSSLで暗号化したファイルを復号する 新居雅行

Citation preview

Page 1: Cocoa勉強会#28-OpenSSLで暗号化したファイルを復号する

2014/2/23 Cocoa #27 QuickLook

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 1/5

Page 2: Cocoa勉強会#28-OpenSSLで暗号化したファイルを復号する

2014/2/23 Cocoa #27 QuickLook

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 2/5

$ echo 'MacBook Air' | openssl bf -e -k apple2 -out test.enc

$ hexdump -C test.enc

00000000 53 61 6c 74 65 64 5f 5f 09 40 94 44 e3 d9 23 71 |[email protected]??#q|

00000010 9d e9 86 7f 3b 62 e1 80 09 5e 2f a4 b4 8d 7e 7e |.?..;b?..^/??.~~|

00000020

$ echo 'MacBook Air' | openssl bf -k apple2 -out test2.enc

$ hexdump -C test2.enc

00000000 53 61 6c 74 65 64 5f 5f b2 04 99 d5 48 5e 28 b7 |Salted__?..?H^(?|

00000010 2e 42 9c 15 83 25 6a 39 e5 1b f7 a6 2c 85 c9 a5 |.B...%j9?.??,.ɥ|

00000020

$ openssl bf -d -k apple2 -in test.enc

MacBook Air

$ openssl bf -P -k apple2

salt=17731E3E93896C77

key=3C27DE1D1DD84C0FA67B6A49E423DF4A

iv =B7FBF5536765B277

$ echo 'MacBook Air' | openssl bf -e -K 3C27DE1D1DD84C0FA67B6A49E423DF4A -iv B7FBF5536765B277 -out test3.enc

$ hexdump -C test3.enc

00000000 25 d0 54 e1 59 fa 67 11 13 16 32 7a c8 87 b6 8b |%?T?Y?g...2z?.?.|

00000010

$ echo 'MacBook Air' | openssl bf -e -K 3C27DE1D1DD84C0FA67B6A49E423DF4A -iv B7FBF5536765B277 -out test4.enc

$ hexdump -C test4.enc

00000000 25 d0 54 e1 59 fa 67 11 13 16 32 7a c8 87 b6 8b |%?T?Y?g...2z?.?.|

00000010

$ openssl bf -in test3.enc -d -K 3C27DE1D1DD84C0FA67B6A49E423DF4A -iv B7FBF5536765B277

MacBook Air

Page 3: Cocoa勉強会#28-OpenSSLで暗号化したファイルを復号する

2014/2/23 Cocoa #27 QuickLook

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 3/5

Page 4: Cocoa勉強会#28-OpenSSLで暗号化したファイルを復号する

2014/2/23 Cocoa #27 QuickLook

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 4/5

#import "AppController.h"

@implementation AppController

- (void)awakeFromNib{ NSString *rPath = [[NSBundle mainBundle] resourcePath]; NSString *encFile = [rPath stringByAppendingPathComponent:@"data.enc"];/*$ openssl bf -P -k apple2salt=17731E3E93896C77key=3C27DE1D1DD84C0FA67B6A49E423DF4Aiv =B7FBF5536765B277*/ NSData* encData = [NSData dataWithContentsOfFile: encFile]; NSString* originalString = [[NSString alloc]initWithData: encData encoding: NSASCIIStringEncoding]; [originalText insertText: originalString]; int plainlen, tmplen; unsigned char iv[8] = {0xB7, 0xFB, 0xF5, 0x53, 0x67, 0x65, 0xB2, 0x77}; unsigned char key[16] = {0x3C, 0x27, 0xDE, 0x1D, 0x1D, 0xD8, 0x4C, 0x0F, 0xA6, 0x7B, 0x6A, 0x49, 0xE4, 0x23, 0xDF, 0x4A}; unsigned char* inData; unsigned char* outData;

inData = (unsigned char*)[encData bytes]; int inDataLen = [encData length]; outData = malloc(inDataLen + 8);

EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init( &ctx ); EVP_DecryptInit( &ctx, EVP_bf_cbc(), key ,iv ); int resultUpdate = EVP_DecryptUpdate( &ctx, outData, &plainlen, inData, inDataLen); int resultFinal = EVP_DecryptFinal( &ctx, outData + plainlen, &tmplen); EVP_CIPHER_CTX_cleanup(&ctx); if ( ( resultFinal != 1 ) || ( resultUpdate != 1 ) ) { [decriptedText insertText: @"Decription was not succeed." ]; } else { outData[ plainlen + tmplen ] = 0; NSString* decriptedString = [NSString stringWithUTF8String: (char*)outData]; [decriptedText insertText: decriptedString ]; } free(outData);}

Page 5: Cocoa勉強会#28-OpenSSLで暗号化したファイルを復号する

2014/2/23 Cocoa #27 QuickLook

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 5/5

@end