· 9 years ago · Nov 25, 2016, 08:06 AM
1public class EncriptionDecriptionUtils {
2 private static byte[] nrlmEncription = { 0x77, 0x71, 0x72, 0x76, 0x52,
3 0x76, 0x44, 0x56, 0x68, 0x66, 0x75, 0x68, 0x77, 0x4b, 0x6f, 0x7d };
4
5 public String decriptionOfData(String data) throws Exception {
6 SecretKeySpec skeySpec = new SecretKeySpec(nrlmEncription, "AES");
7 Cipher cipher = Cipher.getInstance("AES");
8 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
9 byte[] decrypted = cipher.doFinal(Base64.decode(data, Base64.DEFAULT));
10 return new String(decrypted);
11 }
12 public String encriptionOfData(String data) throws Exception {
13 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
14 final SecretKeySpec secretKey = new SecretKeySpec(nrlmEncription, "AES");
15 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
16 return Base64.encodeToString(cipher.doFinal(data.getBytes()), Base64.DEFAULT);
17 }
18}
19
20#import "NSData+AES.h"
21#import <CommonCrypto/CommonCryptor.h>
22
23
24@implementation EncInfo
25
26@end
27
28@implementation NSData (AES)
29
30#pragma mark - Public Methods
31
32- (NSData*) EncryptAES
33{
34 char keyPtr[kCCKeySizeAES128+1];
35 bzero( keyPtr, sizeof(keyPtr) );
36 NSString * strKey;
37 [strKey getCString: keyPtr maxLength: sizeof(keyPtr) encoding:NSUTF8StringEncoding];
38size_t numBytesEncrypted = 0;
39
40 NSUInteger dataLength = [self length];
41
42 size_t bufferSize = dataLength + kCCBlockSizeAES128;
43 void *buffer = malloc(bufferSize);
44 const unsigned char iv[] = { 0x77, 0x71, 0x72, 0x76, 0x52,
45 0x76, 0x44, 0x56, 0x68, 0x66, 0x75, 0x68, 0x77, 0x4b, 0x6f, 0x7d };
46
47 CCCryptorStatus result = CCCrypt( kCCEncrypt,
48 kCCAlgorithmAES128,
49 kCCOptionPKCS7Padding,
50 iv,
51 kCCKeySizeAES128,
52 nil,
53 [self bytes], [self length],
54 buffer, bufferSize,
55 &numBytesEncrypted );
56
57 if( result == kCCSuccess )
58 return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
59 else {
60 NSLog(@"Failed AES");
61 }
62 return nil;
63 }
64
65- (NSData *) DecryptAES
66{
67 char keyPtr[kCCKeySizeAES128+1];
68 bzero( keyPtr, sizeof(keyPtr) );
69 NSString * strKey;
70 [strKey getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding];
71
72 size_t numBytesEncrypted = 0;
73
74 NSUInteger dataLength = [self length];
75
76 size_t bufferSize = dataLength + kCCKeySizeAES128;
77 void *buffer_decrypt = malloc(bufferSize);
78 const unsigned char iv[] = { 0x77, 0x71, 0x72, 0x76, 0x52,
79 0x76, 0x44, 0x56, 0x68, 0x66, 0x75, 0x68, 0x77, 0x4b, 0x6f, 0x7d };
80
81 CCCryptorStatus result = CCCrypt( kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding,
82 iv, kCCKeySizeAES128,
83 nil,
84 [self bytes], [self length],
85 buffer_decrypt, bufferSize,
86 &numBytesEncrypted );
87
88 if( result == kCCSuccess )
89 return [NSData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];
90
91 return nil;
92
93 }