Created
January 20, 2011 22:39
-
-
Save xslim/788840 to your computer and use it in GitHub Desktop.
encode and decode with PBEWithMD5AndDES
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
/* | |
Usage | |
NSString *password = @"1111"; | |
NSString *message = @"xxx"; | |
NSData *inData = [message dataUsingEncoding:NSUTF8StringEncoding]; | |
NSData *encData = [securityUtils encryptPBEWithMD5AndDESData:inData password:password]; | |
NSString *encString = [encData base64EncodedString]; | |
NSString *encString = @"LP+YKAUNy88="; | |
NSData *decData = [securityUtils decryptPBEWithMD5AndDESData:[encString base64DecodedBytes] password:password]; | |
NSString *decodedString = [[[NSString alloc] initWithData:decData encoding:NSUTF8StringEncoding] autorelease]; | |
*/ | |
- (NSData *)encryptPBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password { | |
return [self encodePBEWithMD5AndDESData:inData password:password direction:1]; | |
} | |
- (NSData *)decryptPBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password { | |
return [self encodePBEWithMD5AndDESData:inData password:password direction:0]; | |
} | |
- (NSData *)encodePBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password direction:(int)direction | |
{ | |
// Change salt and number of iterations for your project !!! | |
static const char gSalt[] = | |
{ | |
(unsigned char)0xaa, (unsigned char)0xd1, (unsigned char)0x3c, (unsigned char)0x31, | |
(unsigned char)0x53, (unsigned char)0xa2, (unsigned char)0xee, (unsigned char)0x05 | |
}; | |
unsigned char *salt = (unsigned char *)gSalt; | |
int saltLen = strlen(gSalt); | |
int iterations = 15; | |
EVP_CIPHER_CTX cipherCtx; | |
unsigned char *mResults; // allocated storage of results | |
int mResultsLen = 0; | |
const char *cPassword = [password UTF8String]; | |
unsigned char *mData = (unsigned char *)[inData bytes]; | |
int mDataLen = [inData length]; | |
SSLeay_add_all_algorithms(); | |
X509_ALGOR *algorithm = PKCS5_pbe_set(NID_pbeWithMD5AndDES_CBC, | |
iterations, salt, saltLen); | |
memset(&cipherCtx, 0, sizeof(cipherCtx)); | |
if (algorithm != NULL) | |
{ | |
EVP_CIPHER_CTX_init(&(cipherCtx)); | |
if (EVP_PBE_CipherInit(algorithm->algorithm, cPassword, strlen(cPassword), | |
algorithm->parameter, &(cipherCtx), direction)) | |
{ | |
EVP_CIPHER_CTX_set_padding(&cipherCtx, 1); | |
int blockSize = EVP_CIPHER_CTX_block_size(&cipherCtx); | |
int allocLen = mDataLen + blockSize + 1; // plus 1 for null terminator on decrypt | |
mResults = (unsigned char *)OPENSSL_malloc(allocLen); | |
unsigned char *in_bytes = mData; | |
int inLen = mDataLen; | |
unsigned char *out_bytes = mResults; | |
int outLen = 0; | |
int outLenPart1 = 0; | |
if (EVP_CipherUpdate(&(cipherCtx), out_bytes, &outLenPart1, in_bytes, inLen)) | |
{ | |
out_bytes += outLenPart1; | |
int outLenPart2 = 0; | |
if (EVP_CipherFinal(&(cipherCtx), out_bytes, &outLenPart2)) | |
{ | |
outLen += outLenPart1 + outLenPart2; | |
mResults[outLen] = 0; | |
mResultsLen = outLen; | |
} | |
} else { | |
unsigned long err = ERR_get_error(); | |
ERR_load_crypto_strings(); | |
ERR_load_ERR_strings(); | |
char errbuff[256]; | |
errbuff[0] = 0; | |
ERR_error_string_n(err, errbuff, sizeof(errbuff)); | |
NSLog(@"OpenSLL ERROR:\n\tlib:%s\n\tfunction:%s\n\treason:%s\n", | |
ERR_lib_error_string(err), | |
ERR_func_error_string(err), | |
ERR_reason_error_string(err)); | |
ERR_free_strings(); | |
} | |
NSData *encryptedData = [NSData dataWithBytes:mResults length:mResultsLen]; //(NSData *)encr_buf; | |
//NSLog(@"encryption result: %@\n", [encryptedData base64EncodedString]); | |
return encryptedData; | |
} | |
} | |
return nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you convert this Encryption to swift class .