@interface NSString (Encrypt)
+ (NSString*)stringEncrypt:(NSString*)str;
+ (NSString*)stringDecrypt:(NSString*)str;
@end
#import "NSString+Encrypt.h"
#import "DTBase64Coding.h"
static NSString* DATA_KEY =@"DATA";
staticByte UDE_KEY =99;
@implementation NSString (Encrypt)
+ (NSString*)stringEncrypt:(NSString*)str
{
NSString* tagStr = [str stringByAppendingString:DATA_KEY];
NSData* EncodeAsData = [NSKeyedArchiverarchivedDataWithRootObject:tagStr];
NSMutableData *mutableData = [[NSMutableDataalloc]initWithData:EncodeAsData];
Byte *t = [mutableData mutableBytes];
for (int i =0; i < [mutableDatalength]; i++)
{
*t = *t ^ UDE_KEY;
t++;
}
return [DTBase64CodingstringByEncodingData:mutableData];
}
+ (NSString*)stringDecrypt:(NSString*)str
{
NSString *result = @"";
NSData* data = [DTBase64CodingdataByDecodingString:str];
if (data)
{
NSMutableData *dataCopy = [NSMutableDatadataWithData:data];
Byte *t = [dataCopy mutableBytes];
for (int i =0; i < [dataCopylength]; i++)
{
*t = *t ^ UDE_KEY;
t++;
}
NSString *enKeyTagResult = nil;
@try
{
enKeyTagResult = [NSKeyedUnarchiverunarchiveObjectWithData:dataCopy];
}
@catch (NSException * e)
{
enKeyTagResult = nil;
}
if (enKeyTagResult && [enKeyTagResult hasSuffix:DATA_KEY])
{
result = [enKeyTagResult substringToIndex:[enKeyTagResultlength] - [DATA_KEY length]];
}
}
return result;
}
@end