-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNSFileManager-AES.m
More file actions
93 lines (87 loc) · 2.92 KB
/
NSFileManager-AES.m
File metadata and controls
93 lines (87 loc) · 2.92 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// NSFileManager-AES.m
// Encryption
//
// Created by Jeff LaMarche on 2/12/09.
// Copyright 2009 Jeff LaMarche Consulting. All rights reserved.
//
#import "NSFileManager-AES.h"
#import "rijndael.h"
@implementation NSFileManager(AES)
-(BOOL)AESEncryptFile:(NSString *)inPath toFile:(NSString *)outPath usingPassphrase:(NSString *)pass error:(NSError **)error
{
unsigned long rk[RKLENGTH(KEYBITS)];
unsigned char key[KEYLENGTH(KEYBITS)];
const char *password = [pass UTF8String];
for (int i = 0; i < sizeof(key); i++)
key[i] = password != 0 ? *password++ : 0;
int nrounds = rijndaelSetupEncrypt(rk, key, KEYBITS);
FILE *fp = fopen([inPath UTF8String], "r");
FILE *output = fopen([outPath UTF8String], "wb");
if (output == NULL)
{
*error = [NSError errorWithDomain:@"AES Encryption" code:1000 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"File Open Error", @"File Open Error") forKey:AESEncryptionErrorDescriptionKey]];
return NO;
}
while (1)
{
unsigned char plaintext[16];
unsigned char ciphertext[16];
int j;
for (j = 0; j < sizeof(plaintext); j++)
{
int c = getc(fp);
if (c == EOF)
break;
plaintext[j] = c;
}
if (j == 0)
break;
for (; j < sizeof(plaintext); j++)
plaintext[j] = ' ';
rijndaelEncrypt(rk, nrounds, plaintext, ciphertext);
if (fwrite(ciphertext, sizeof(ciphertext), 1, output) != 1)
{
fclose(output);
*error = [NSError errorWithDomain:@"AES Encryption" code:1000 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"File write error", @"File write error") forKey:AESEncryptionErrorDescriptionKey]];
return NO;
}
}
fclose(output);
fclose(fp);
return YES;
}
-(BOOL)AESDecryptFile:(NSString *)inPath toFile:(NSString *)outPath usingPassphrase:(NSString *)pass error:(NSError **)error
{
unsigned long rk[RKLENGTH(KEYBITS)];
unsigned char key[KEYLENGTH(KEYBITS)];
const char *password = [pass UTF8String];
for (int i = 0; i < sizeof(key); i++)
key[i] = password != 0 ? *password++ : 0;
FILE *fp = fopen([inPath UTF8String], "r");
FILE *output = fopen([outPath UTF8String], "wb");
if (output == NULL)
{
*error = [NSError errorWithDomain:@"AES Encryption" code:1000 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"File Open Error", @"File Open Error") forKey:AESEncryptionErrorDescriptionKey]];
return NO;
}
int nrounds = rijndaelSetupDecrypt(rk, key, KEYBITS);
while (1)
{
unsigned char plaintext[16];
unsigned char ciphertext[16];
if (fread(ciphertext, sizeof(ciphertext), 1, fp) != 1)
break;
rijndaelDecrypt(rk, nrounds, ciphertext, plaintext);
if (fwrite(plaintext, sizeof(plaintext), 1, output) != 1)
{
fclose(output);
*error = [NSError errorWithDomain:@"AES Encryption" code:1000 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"File write error", @"File write error") forKey:AESEncryptionErrorDescriptionKey]];
return NO;
}
}
fclose(output);
fclose(fp);
return YES;
}
@end