思路:我要做的是加密一段文字,想到的方法是找到一定数量的随机汉字(比如:10000个),把这段文字随机插入到随机汉字中,记录相应的顺序,在解密数据。
1.产生随机汉字的方法如下:
// 拼接一个number个随机汉子的字符串
- (NSMutableString *)appendStringWithCHINESEwithNumber:(NSInteger)num{
NSMutableString *mutableString = [NSMutableString string];
for (NSInteger i = 0; i < num;i++){
NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSInteger randomH = 0xA1+arc4random()%(0xFE - 0xA1+1);
NSInteger randomL = 0xB0+arc4random()%(0xF7 - 0xB0+1);
NSInteger number = (randomH<<8)+randomL;
NSData *data = [NSData dataWithBytes:&number length:2];
NSString *string = [[NSString alloc] initWithData:data encoding:gbkEncoding];
// NSLog(@"%@",string);
[mutableString appendString:string];
}
return mutableString;
}
2.产生随机不重复数的方法
// 得到从0到number之间不重复的随机数 count个
-(NSMutableArray *)getHowManyNumberCount:(NSInteger)count fromZeroToNumber:(NSInteger)number{
NSMutableArray *numberArray = [NSMutableArray array];
for (NSInteger i = 0; i < number; i++) {
NSString *str = [NSString stringWithFormat:@"%ld",i];
[numberArray addObject:str];
}
NSMutableArray *countArray = [NSMutableArray array];
for (NSInteger j = 0; j < count; j++) {
NSInteger index = arc4random()%(number+1);
[countArray addObject:numberArray[index]];
[numberArray removeObjectAtIndex:index];
number--;
}
return countArray;
}
3.加密字符串
// 全局的字典
@property(nonatomic,copy)NSMutableDictionary *dict;
@property(nonatomic,copy)NSMutableString *chineseStr;
// 加密汉字字符串
-(NSMutableString *)wendToEncryPtionWithString:(NSString *)str;
-(NSMutableString *)wendToEncryPtionWithString:(NSString *)str{
//
// self.dictArray = [NSMutableArray array];
// 初始化字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 获取str.length个随机数数组(0到10000)
NSMutableArray *numberArray = [self getHowManyNumberCount:str.length fromZeroToNumber:10000];
// 获取由10000个汉子随机组合的字符串
NSMutableString *mutableStr = [self appendStringWithCHINESEwithNumber:10000];
NSLog(@"mutableStr:%@",mutableStr);
NSLog(@"length:%ld",mutableStr.length);
// 给字典赋值
for (NSInteger i = 0; i < str.length; i++) {
NSString *string = [NSString stringWithFormat:@"%ld",i];
[dict setValue:numberArray[i] forKey:string];
[mutableStr replaceCharactersInRange:NSMakeRange([numberArray[i] integerValue], 1) withString:[str substringWithRange:NSMakeRange(i, 1)]];
}
self.dict = dict;
self.chineseStr = mutableStr;
return mutableStr;
}
4.解密字符串
// 解密字符串
-(NSMutableString *)getStrFromEncryPtion;
-(NSMutableString *)getStrFromEncryPtion{
NSMutableString *mutableStr = [NSMutableString string];
for (NSInteger i = 0; i < self.dict.count; i++) {
NSString *str = [NSString stringWithFormat:@"%ld",i];
NSInteger index = [[self.dict valueForKey:str] integerValue];
[mutableStr appendString:[self.chineseStr substringWithRange:NSMakeRange(index, 1)]];
}
return mutableStr;
}
5.将封装的类,打包成静态库.a文件,直接拖入工程引入头文件即可使用。打包.a静态库的方法
屏幕快照 2016-05-29 上午11.09.53.png6.思考
我觉得这样做意义并不大,给文字加密的一般用来传送消息,但是加密解密不能分开,最好的方法是能产生公钥和密钥,或者是把一段错乱的文字发去后,把相应的文字顺序以某种形式传递过去,无奈技术和思路有限,只能想到将文字的顺序生成plist文件也发送过去进行解码,详见Demo下载地址中的JYC_encryption类
网友评论