Created
May 17, 2017 09:56
-
-
Save AnnieNinaJoyceV/65182deed8268f4e0009664a176c7208 to your computer and use it in GitHub Desktop.
3DES(DESede) + MD5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSData *)md5DataFromString:(NSString *)input | |
{ | |
const char *cStr = [input UTF8String]; | |
unsigned char digest[16]; | |
CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call | |
NSData *data = [NSData dataWithBytes:digest length:CC_MD5_DIGEST_LENGTH]; | |
return data; | |
} | |
- (NSString*) doCipher:(NSString*)plainText secretKey:(NSString *)secret operation:(CCOperation)encryptOrDecrypt | |
{ | |
const void *vplainText; | |
size_t plainTextBufferSize; | |
if (encryptOrDecrypt == kCCDecrypt) | |
{ | |
NSData *EncryptData = [[NSData alloc] initWithBase64EncodedString:plainText options:NSDataBase64DecodingIgnoreUnknownCharacters]; | |
plainTextBufferSize = [EncryptData length]; | |
vplainText = [EncryptData bytes]; | |
} | |
else | |
{ | |
plainTextBufferSize = [plainText length]; | |
vplainText = (const void *) [plainText UTF8String]; | |
} | |
CCCryptorStatus ccStatus; | |
uint8_t *bufferPtr = NULL; | |
size_t bufferPtrSize = 0; | |
size_t movedBytes = 0; | |
// uint8_t iv[kCCBlockSize3DES]; | |
// memset((void *) iv, 0x00, (size_t) sizeof(iv)); | |
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1); | |
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t)); | |
memset((void *)bufferPtr, 0x0, bufferPtrSize); | |
NSData *testData = [self md5DataFromString:secret]; | |
const char *constSource = [testData bytes]; | |
unsigned char source[24]; | |
memcpy(source, constSource, sizeof (source)); | |
for (int j = 0, k = 16; j < 8;) { | |
source[k++] = source[j++]; | |
} | |
ccStatus = CCCrypt(encryptOrDecrypt, | |
kCCAlgorithm3DES, | |
kCCOptionPKCS7Padding | kCCOptionECBMode, | |
source, | |
kCCKeySize3DES, | |
NULL, //iv, | |
vplainText, | |
plainTextBufferSize, | |
(void *)bufferPtr, | |
bufferPtrSize, | |
&movedBytes); | |
if (ccStatus == kCCSuccess) NSLog(@"SUCCESS"); | |
else if (ccStatus == kCCParamError) return @"PARAM ERROR"; | |
else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL"; | |
else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE"; | |
else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT"; | |
else if (ccStatus == kCCDecodeError) return @"DECODE ERROR"; | |
else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED"; | |
NSString *result; | |
if (encryptOrDecrypt == kCCDecrypt) | |
{ | |
result = [[NSString alloc] initWithData: [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding]; | |
} | |
else | |
{ | |
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes]; | |
result = [myData base64EncodedStringWithOptions: 0]; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment