2014/2/23

Cocoa

#27 QuickLook

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

1/5
2014/2/23

Cocoa

#27 QuickLook

$ ech o 'MacBook Air' | open s s l bf -e -k apple2 -ou t tes t.en c
$ h exdu m p -C tes t.en c
00000000 53 61 6c 74 65 64 5f 5f 09 40 94 44 e3 d9 23 71 |Salted__.@.D??#q|
00000010 9d e9 86 7f 3b 62 e1 80 09 5e 2f a4 b4 8d 7e 7e |.?..;b?..^/??.~~|
00000020
$ ech o 'MacBook Air' | open s s l bf -k apple2 -ou t tes t2 .en c
$ h exdu m p -C tes t2 .en c
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
$ open s s l bf -d -k apple2 -in tes t.en c
MacBook Air

$ open s s l bf -P -k apple2
salt=17731E3E93896C77
key=3C27DE1D1DD84C0FA67B6A49E423DF4A
iv =B7FBF5536765B277
$ ech o 'MacBook Air' | open s s l bf -e -K 3 C2 7 DE1 D1 DD8 4 C0 FA6 7 B6 A4 9 E4 2 3 DF4 A -iv B7 FBF5 5 3 6 7 6 5 B2 7 7 -ou t tes t3 .en c
$ h exdu m p -C tes t3 .en c
00000000 25 d0 54 e1 59 fa 67 11 13 16 32 7a c8 87 b6 8b |%?T?Y?g...2z?.?.|
00000010
$ ech o 'MacBook Air' | open s s l bf -e -K 3 C2 7 DE1 D1 DD8 4 C0 FA6 7 B6 A4 9 E4 2 3 DF4 A -iv B7 FBF5 5 3 6 7 6 5 B2 7 7 -ou t tes t4 .en c
$ h exdu m p -C tes t4 .en c
00000000 25 d0 54 e1 59 fa 67 11 13 16 32 7a c8 87 b6 8b |%?T?Y?g...2z?.?.|
00000010
$ open s s l bf -in tes t3 .en c -d -K 3 C2 7 DE1 D1 DD8 4 C0 FA6 7 B6 A4 9 E4 2 3 DF4 A -iv B7 FBF5 5 3 6 7 6 5 B2 7 7
MacBook Air

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

2/5
2014/2/23

Cocoa

#27 QuickLook

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

3/5
2014/2/23

Cocoa

#27 QuickLook

#import "AppController.h"
@implementation AppController
- (void)awakeFromNib
{
NSString *rPath = [[NSBundle mainBundle] resourcePath];
NSString *encFile = [rPath stringByAppendingPathComponent:@"data.enc"];
/*
$ openssl bf -P -k apple2
salt=17731E3E93896C77
key=3C27DE1D1DD84C0FA67B6A49E423DF4A
iv =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);
}

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

4/5
2014/2/23

Cocoa

#27 QuickLook

@end

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

5/5

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

  • 1.
  • 2.
    2014/2/23 Cocoa #27 QuickLook $ echo 'MacBook Air' | open s s l bf -e -k apple2 -ou t tes t.en c $ h exdu m p -C tes t.en c 00000000 53 61 6c 74 65 64 5f 5f 09 40 94 44 e3 d9 23 71 |Salted__.@.D??#q| 00000010 9d e9 86 7f 3b 62 e1 80 09 5e 2f a4 b4 8d 7e 7e |.?..;b?..^/??.~~| 00000020 $ ech o 'MacBook Air' | open s s l bf -k apple2 -ou t tes t2 .en c $ h exdu m p -C tes t2 .en c 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 $ open s s l bf -d -k apple2 -in tes t.en c MacBook Air $ open s s l bf -P -k apple2 salt=17731E3E93896C77 key=3C27DE1D1DD84C0FA67B6A49E423DF4A iv =B7FBF5536765B277 $ ech o 'MacBook Air' | open s s l bf -e -K 3 C2 7 DE1 D1 DD8 4 C0 FA6 7 B6 A4 9 E4 2 3 DF4 A -iv B7 FBF5 5 3 6 7 6 5 B2 7 7 -ou t tes t3 .en c $ h exdu m p -C tes t3 .en c 00000000 25 d0 54 e1 59 fa 67 11 13 16 32 7a c8 87 b6 8b |%?T?Y?g...2z?.?.| 00000010 $ ech o 'MacBook Air' | open s s l bf -e -K 3 C2 7 DE1 D1 DD8 4 C0 FA6 7 B6 A4 9 E4 2 3 DF4 A -iv B7 FBF5 5 3 6 7 6 5 B2 7 7 -ou t tes t4 .en c $ h exdu m p -C tes t4 .en c 00000000 25 d0 54 e1 59 fa 67 11 13 16 32 7a c8 87 b6 8b |%?T?Y?g...2z?.?.| 00000010 $ open s s l bf -in tes t3 .en c -d -K 3 C2 7 DE1 D1 DD8 4 C0 FA6 7 B6 A4 9 E4 2 3 DF4 A -iv B7 FBF5 5 3 6 7 6 5 B2 7 7 MacBook Air file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 2/5
  • 3.
  • 4.
    2014/2/23 Cocoa #27 QuickLook #import "AppController.h" @implementationAppController - (void)awakeFromNib { NSString *rPath = [[NSBundle mainBundle] resourcePath]; NSString *encFile = [rPath stringByAppendingPathComponent:@"data.enc"]; /* $ openssl bf -P -k apple2 salt=17731E3E93896C77 key=3C27DE1D1DD84C0FA67B6A49E423DF4A iv =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); } file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_OpenSSL-msyk/index.html 4/5
  • 5.