1、Java的异或加解密算法
在使用异或加解密时,加密和解密的密钥必须保持相同,否则解密时无法恢复为原来的内容。
[java]
- public class XOREncryptAndDecrypt {
-
-
- private static final String secretKey ="abcdefghijklmnopqrstuvwxyz";
-
-
- public static String encrypt(String plainText){
- String encryption = "";
- try {
- plainText = new String(plainText.getBytes("UTF-8"),"iso-8859-1");
- } catch (Exception e) {
- }
- char[] cipher=new char[plainText.length()];
- for(int i=0,j=0;i<plainText.length();i++,j++){
- if(j==secretKey.length())
- j=0;
- cipher[i]=(char) (plainText.charAt(i)^secretKey.charAt(j));
- String strCipher= Integer.toHexString(cipher[i]);
- if(strCipher.length() == 1){
- encryption+="0"+strCipher;
- }else{
- encryption+=strCipher;
- }
- }
- return encryption;
- }
-
-
- public static String decrypt(String encryption) {
- char[] decryption=new char[encryption.length()/2];
- for(int i=0,j=0;i<encryption.length()/2;i++,j++){
- if(j==secretKey.length())
- j=0;
- char n=(char)(int)Integer.valueOf(encryption.substring(i*2,i*2+2),16);
- decryption[i]=(char)(n^secretKey.charAt(j));
- }
- String decoding="";
- try {
- decoding = new String(String.valueOf(decryption).getBytes("iso-8859-1"),"UTF-8");
- } catch (Exception e) {
- }
- return decoding;
- }
-
-
- public static void main(String[] args) {
- String name="你好";
- String tem=XOREncryptAndDecrypt.encrypt(name);
- System.out.println(tem);
- System.out.println(XOREncryptAndDecrypt.decrypt(tem));
-
- }
-
- }
2、IOS加解密算法
在使用异或加解密时,加密和解密的密钥必须保持相同,否则解密时无法恢复为原来的内容。如果想在iOS与Java中通用,必须保持Java与IOS中的密钥一致。
1).h文件内容
[objc]
- #import <Foundation/Foundation.h>
-
- @interface XOREncryptAndDecrypt : NSObject
-
- + (NSString *)encryptForPlainText:(NSString *)plainText;
-
- + (NSString *)decryptForEncryption:(NSString *)encryption;
-
- @end
2).m文件内容
[objc]
- #import "XOREncryptAndDecrypt.h"
- @implementation XOREncryptAndDecrypt
- static NSString *secretKey =@"abcdefghijklmnopqrstuvwxyz";
- #pragma mark 加密字符串
- + (NSString *)encryptForPlainText:(NSString *)plainText
- {
- NSMutableString *encryption=[NSMutableString string];
- NSString *encoding=[[NSString alloc]initWithData:[plainText dataUsingEncoding:NSUTF8StringEncoding] encoding:NSISOLatin1StringEncoding];
- for(int i=0,j=0;i<encoding.length;i++,j++){
- if(j==secretKey.length){
- j=0;
- }
- char cipher=(char)([encoding characterAtIndex:i]^[secretKey characterAtIndex:j]);
- NSString *strCipher= [NSString stringWithFormat:@"%hhx",cipher];
- if(strCipher.length == 1){
- [encryption appendFormat:@"0%@",strCipher];
- }else{
- [encryption appendString:strCipher];
- }
- }
- return encryption;
- }
- #pragma mark 解密 如果不为加密字符则返回原字符
- + (NSString *)decryptForEncryption:(NSString *)encryption {
- NSMutableString *decryption=[NSMutableString string];
- NSString *decoding=nil;
- for(int i=0,j=0;i<encryption.length/2;i++,j++){
- if(j==secretKey.length){
- j=0;
- }
- NSString *tem=[encryption substringWithRange:NSMakeRange(i*2, 2)];
- charchar *endptr;
- char n=(char)(int)strtoul([tem UTF8String],&endptr,16);
- if (n==‘\0‘&&*endptr!=‘\0‘) {
- [decryption setString:@""];
- break;
- }
- [decryption appendFormat:@"%c",(char)(n^[secretKey characterAtIndex:j])];
- }
- if (![decryption isEqualToString:@""]) {
- decoding=[[NSString alloc]initWithData:[[decryption copy] dataUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
- }
- if (decoding==nil) {
- decoding=encryption;
- }
- return decoding;
- }
- @end
Java/IOS通用异或加解密字符串
原文:http://www.cnblogs.com/akiha/p/6507011.html