- (NSString *)encryptForPlainText:(NSString *)plainText
{
//保存加密后的字符
NSMutableString *encryption=[NSMutableString string];
//编码转换后的字符串 UTF_8->iso-8859-1
NSString *encoding=[[NSString alloc]initWithData:[plainText dataUsingEncoding:NSUTF8StringEncoding] encoding:NSISOLatin1StringEncoding];
for(int i=0;i
//异或后的字符
char cipher=(char)([encoding characterAtIndex:i]^0x99);
//NSLog(@"%c转成16进制的字符串:%@,%@",cipher,[NSString stringWithFormat:@"%hhx",cipher],[NSString stringWithFormat:@"%x",cipher&0xff]);
//转成16进制形式的字符串 \x8b转成8b字符串
NSString *strCipher= [NSString stringWithFormat:@"%hhx",cipher];
if(strCipher.length == 1){
[encryption appendFormat:@"0%@",strCipher];
}else{
[encryption appendString:strCipher];
}
}
NSData *data = [encryption dataUsingEncoding:NSUTF8StringEncoding];
NSData *base64Data = [data base64EncodedDataWithOptions:0];
NSString *base64Str = [[NSString alloc]initWithData:base64Data encoding:NSUTF8StringEncoding];
return base64Str;
}
- (NSString *)decryptForEncryption:(NSString *)encryption {
NSData *data = [[NSData alloc]initWithBase64EncodedString:encryption options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//保存解密后的字符
NSMutableString *decryption=[NSMutableString string];
//解码字符
NSString *decoding=nil;
for(int i=0;i
//截取16进制形式的字符串 \x8b中的8b
NSString *tem=[string substringWithRange:NSMakeRange(i*2, 2)];
char *endptr;
//把16进制形式的字符串转为字符
char n=(char)(int)strtoul([tem UTF8String],&endptr,16);
//判断是否为加密字符
if (n=='\0'&&*endptr!='\0') {
[decryption setString:@""];
break;
}
[decryption appendFormat:@"%c",(char)(n^0x99)];
}
if (![decryption isEqualToString:@""]) {
//编码后的字符串 iso-8859-1 -> UTF_8
decoding=[[NSString alloc]initWithData:[[decryption copy] dataUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
}
if (decoding==nil) {
decoding=string;
}
return decoding;
}