Ios 加密NST字符串
我想对NSString进行加密,使其不可读。安全级别与方式无关(换句话说,如果有人解密文本,他们就不会窃取任何敏感信息)Ios 加密NST字符串,ios,objective-c,Ios,Objective C,我想对NSString进行加密,使其不可读。安全级别与方式无关(换句话说,如果有人解密文本,他们就不会窃取任何敏感信息) NSString *myTextToEncrypt = @"Hello World!"; [myTextToEncrypt encrypt]; // myTextToEncrypt is now something unreadable, like '2rwzdn1405' 那么我就可以解开这个字符串了 [myTextToEncrypt unencrypt]; // m
NSString *myTextToEncrypt = @"Hello World!";
[myTextToEncrypt encrypt];
// myTextToEncrypt is now something unreadable, like '2rwzdn1405'
那么我就可以解开这个字符串了
[myTextToEncrypt unencrypt]; // myTextToEncrypt should now be @"Hello World!" again
如何做到这一点?我读过一些关于CommonCrypto和AES加密的文章,但这一切对于我尝试做的事情来说似乎都是杀伤力过大(我读过的加密方法都是针对密码或其他敏感数据块的)你可以用它来做到这一点
Objective-C中有一些可用的实现(as)
请注意,内容在编码后会大30%左右。最简单的方法是使用您自己的加密,例如 Utils.h
@interface Utils : NSObject
+(NSString*)encyptString:(NSString*)str;
+(NSString*)decryptString:(NSString*)str;
@end
Utils.m
#import "Utils.h"
int offset = 15;
@implementation Utils
+(NSString*)encyptString:(NSString*)str
{
NSMutableString *encrptedString = [[NSMutableString alloc] init];
for (int i = 0; i < str.length; i++) {
unichar character = [str characterAtIndex:i];
character += offset;
[encrptedString appendFormat:@"%C",character];
}
return encrptedString;
}
+(NSString*)decryptString:(NSString*)str
{
NSMutableString *decrptedString = [[NSMutableString alloc] init];
for (int i = 0; i < str.length; i++) {
unichar character = [str characterAtIndex:i];
character -= offset;
[decrptedString appendFormat:@"%C",character];
}
return decrptedString;
}
@end
日志
这不是加密,这是编码,更容易穿透。@Jasconius穿透有多容易?我想用编码来“加密”一些文本消息。@Inderkumarathore偏移量需要有一个限制吗?或者无论文本是什么,我都可以传递我想要的数字吗?
NSString *str = @"hello world";
NSString *enr = [Utils encyptString:str];
NSLog(@"Encrypted Text=%@", enr);
NSLog(@"Decrypted Text=%@", [Utils decryptString:enr]);
2013-08-11 10:44:09.409 DeviceTest[445:c07] Encrypted Text=wt{{~/~{s
2013-08-11 10:44:09.412 DeviceTest[445:c07] Decrypted Text=hello world