由于最近涉及到的项目需要在与后台通信时,需要加解密,而后台发出来得Json是通过Encode加密了的,所以在接受后,需要在ios中用stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding方法来解密成UTF8。当然同样的,我需要发送请求给后台时,用stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding来加密。
iOS:接受--->解密---->加密:
- (void)reloadData
{
NSURL *url = [NSURL URLWithString:@"http://cqzjt.com:8081/testapp/servlet/Appinfo"];
NSString *post=@"postData";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[request setTimeoutInterval:10.0];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (error) {
NSLog(@"Httperror:%@%d", error.localizedDescription,error.code);
}else{
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HttpResponseCode:%d", responseCode);
NSLog(@"接受加密数据 %@",responseString);
NSString *decodeUrl = [responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"解密数据:%@",decodeUrl);
NSString *encodeUrl = [decodeUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"加密数据:%@",encodeUrl);
}
}];
}
输出结果:![[2014-03-20]iOS与JAVA <wbr>通信时Encode,Decode加解密。 [2014-03-20]iOS与JAVA <wbr>通信时Encode,Decode加解密。](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
JAVA:加密-->解密:
public void setEncodeResult(String encodeResult)
{
char[] src = encodeResult.toCharArray();//将待编码字串拆分成字符数组
StringBuilder sb = new StringBuilder();//保存编码后字符
//将待编码字串拆分成字符数组
for(int i = 0; i< src.length; i++)
{
if(Character.isDigit(src[i]))
{
if(i != src.length-1)
{//满足条件3
char[] temp = new char[Character.getNumericValue(src[i])+1];
Arrays.fill(temp,src[i+1]);
sb.append(temp);
sb.append("_");
}
else
{//满足条件2
sb.append(src[i]);
}
}
else if(src[i] == '_')//满足条件5
{
sb.append("\\UL");
sb.append("_");
}
else if(i == src.length-1)//满足条件1,且到了字串结尾
{
sb.append(src[i]);
}
else//满足条件1,且未到字串结尾
{
sb.append(src[i]);
sb.append("_");
}
}
this.encodeResult = new String(sb);//创建返回编码后字串
}
//获得编码后结果
public String getEncodeResult()
{
return encodeResult;
}
//解码设置
public void setDecodeResult(String encodeResult)
{
String[] temp = encodeResult.split("_");
StringBuilder sb = new StringBuilder();
for(int i = 0; i< temp.length; i++)
{
if(temp[i].equals("\\UL"))
sb.append("_");
else if(temp[i].length()>1)
sb.append(temp[i].length()-1);
else
sb.append(temp[i]);
}
this.decodeResult = new String(sb);
}
//获得解码后结果
public String getDecodeResult()
{
return decodeResult;
}
public static void main(String[] args)
{
System.out.println("请输入待编码字符串(以回车键结束):");
String source = "";
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
source = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
Encode e = new Encode();
e.setEncodeResult(source);
System.out.println("编码后结果:"+e.getEncodeResult());
e.setDecodeResult(e.getEncodeResult());
System.out.println("解码后结果:"+e.getDecodeResult());
}
}