十六进制转二进制的时候会用到
self.setSecretDic = [[NSMutableDictionaryalloc]initWithCapacity:16];
[self.setSecretDicsetObject:@"0000"forKey:@"0"];
[self.setSecretDicsetObject:@"0001"forKey:@"1"];
[self.setSecretDicsetObject:@"0010"forKey:@"2"];
[self.setSecretDicsetObject:@"0011"forKey:@"3"];
[self.setSecretDicsetObject:@"0100"forKey:@"4"];
[self.setSecretDicsetObject:@"0101"forKey:@"5"];
[self.setSecretDicsetObject:@"0110"forKey:@"6"];
[self.setSecretDicsetObject:@"0111"forKey:@"7"];
[self.setSecretDicsetObject:@"1000"forKey:@"8"];
[self.setSecretDicsetObject:@"1001"forKey:@"9"];
[self.setSecretDicsetObject:@"1010"forKey:@"a"];
[self.setSecretDicsetObject:@"1011"forKey:@"b"];
[self.setSecretDicsetObject:@"1100"forKey:@"c"];
[self.setSecretDicsetObject:@"1101"forKey:@"d"];
[self.setSecretDicsetObject:@"1110"forKey:@"e"];
[self.setSecretDicsetObject:@"1111"forKey:@"f"];
二进制转十六进制的时候会用到
self.removeSecretDic = [[NSMutableDictionaryalloc] initWithCapacity:16];
[self.removeSecretDicsetObject:@"0"forKey:@"0000"];
[self.removeSecretDicsetObject:@"1"forKey:@"0001"];
[self.removeSecretDicsetObject:@"2"forKey:@"0010"];
[self.removeSecretDicsetObject:@"3"forKey:@"0011"];
[self.removeSecretDicsetObject:@"4"forKey:@"0100"];
[self.removeSecretDicsetObject:@"5"forKey:@"0101"];
[self.removeSecretDicsetObject:@"6"forKey:@"0110"];
[self.removeSecretDicsetObject:@"7"forKey:@"0111"];
[self.removeSecretDicsetObject:@"8"forKey:@"1000"];
[self.removeSecretDicsetObject:@"9"forKey:@"1001"];
[self.removeSecretDicsetObject:@"a"forKey:@"1010"];
[self.removeSecretDicsetObject:@"b"forKey:@"1011"];
[self.removeSecretDicsetObject:@"c"forKey:@"1100"];
[self.removeSecretDicsetObject:@"d"forKey:@"1101"];
[self.removeSecretDicsetObject:@"e"forKey:@"1110"];
[self.removeSecretDicsetObject:@"f"forKey:@"1111"];
#pragma mark - 加密
- (void)setSecretWithString:(NSString *)string{
NSData *binaryData = [stringdataUsingEncoding:NSUTF8StringEncoding];
Byte *binaryByte = (Byte *)[binaryDatabytes];
NSString *hexString=@"";
for(int i=0;i<[binaryDatalength];i++)
{
NSString *newHexStr = [NSStringstringWithFormat:@"%x",binaryByte[i]&0xff];
if([newHexStrlength]==1){
hexString = [NSStringstringWithFormat:@"%@0%@",hexString,newHexStr];
}else{
hexString = [NSStringstringWithFormat:@"%@%@",hexString,newHexStr];
}
}
NSLog(@"16进制数为:\n%@",hexString);
[selfhexStringTranslateToBinaryString:hexString];
}
#pragma mark - 将十六进制转化为二进制
- (void)hexStringTranslateToBinaryString:(NSString *)hexString{
NSString *binaryString=[[NSStringalloc]init];
for (int i =0; i < [hexStringlength]; i++) {
NSRange rage;
rage.length =1;
rage.location = i;
NSString *key = [hexStringsubstringWithRange:rage];
binaryString = [NSStringstringWithFormat:@"%@%@",binaryString,[NSStringstringWithFormat:@"%@",[self.setSecretDicobjectForKey:key]]];
}
NSLog(@"转化后的二进制为:\n%@",binaryString);
[selfchangeBinaryString:binaryString];
}
#pragma mark - 移位(改变二进制数的某一个或某几个位)
- (void)changeBinaryString:(NSString *)binaryString{
NSMutableArray *binaryArray = [[NSMutableArrayalloc]init];
for (int i=0; i<[binaryStringlength]; i++) {
NSRange rage;
rage.length =1;
rage.location = i;
NSString *character = [binaryStringsubstringWithRange:rage];
[binaryArray addObject:character];
}
[binaryArray exchangeObjectAtIndex:0withObjectAtIndex:binaryArray.count-1];
NSString *newBinaryString =@"";
for (NSInteger i =0; i < binaryArray.count; i++) {
newBinaryString = [newBinaryString stringByAppendingFormat:@"%@",[binaryArrayobjectAtIndex:i]];
}
//newBinaryString就是移位后的数据
NSLog(@"移位后的二进制为:\n%@",newBinaryString);
[selfbinaryStringToHexString:newBinaryString];
}
#pragma mark - 二进制转为十六进制字符串
- (void)binaryStringToHexString:(NSString *)newBinaryString{
NSString *hexString=@"";
for (int i=0; i<[newBinaryStringlength]/4; i++) {
NSRange rage;
rage.length =4;
rage.location = i*4;
NSString *key = [newBinaryStringsubstringWithRange:rage];
hexString = [NSStringstringWithFormat:@"%@%@",hexString,[NSStringstringWithFormat:@"%@",[self.removeSecretDicobjectForKey:key]]];
}
NSLog(@"二进制字符串转为十六进制字符串:\n%@",hexString);
[selfunlokHexStringTranslateToBinaryString:hexString];
}