Java/IOS通用异或加解密字符串

1、Java的异或加解密算法

在使用异或加解密时,加密和解密的密钥必须保持相同,否则解密时无法恢复为原来的内容。

 [java] 

  1. public class XOREncryptAndDecrypt {  
  2.       
  3.     //密钥 加解密的密钥必须相同 可自定义  
  4.     private static final String secretKey ="abcdefghijklmnopqrstuvwxyz";   
  5.       
  6.     /** 
  7.      * 字符串加密 
  8.      * @param plainText 要加密的字符串 
  9.      * @return 加密后的字符串 
  10.      */  
  11.     public static String encrypt(String plainText){  
  12.          String encryption = "";  
  13.          try {  
  14.              plainText =  new String(plainText.getBytes("UTF-8"),"iso-8859-1");  
  15.             } catch (Exception e) {  
  16.             }  
  17.          char[] cipher=new char[plainText.length()];  
  18.          for(int i=0,j=0;i<plainText.length();i++,j++){  
  19.              if(j==secretKey.length())  
  20.                  j=0;  
  21.              cipher[i]=(char) (plainText.charAt(i)^secretKey.charAt(j));  
  22.              String strCipher= Integer.toHexString(cipher[i]);  
  23.              if(strCipher.length() == 1){  
  24.                  encryption+="0"+strCipher;  
  25.              }else{  
  26.                  encryption+=strCipher;  
  27.              }  
  28.          }  
  29.          return encryption;  
  30.      }  
  31.   
  32.     /** 
  33.      * 解密字符串 
  34.      * @param encryption 要解密的字符串 
  35.      * @return 解密后的字符串 
  36.      */  
  37.     public static String decrypt(String encryption) {  
  38.          char[] decryption=new char[encryption.length()/2];  
  39.          for(int i=0,j=0;i<encryption.length()/2;i++,j++){  
  40.              if(j==secretKey.length())  
  41.                  j=0;  
  42.              char n=(char)(int)Integer.valueOf(encryption.substring(i*2,i*2+2),16);  
  43.              decryption[i]=(char)(n^secretKey.charAt(j));  
  44.          }  
  45.          String decoding="";  
  46.          try {  
  47.              decoding = new String(String.valueOf(decryption).getBytes("iso-8859-1"),"UTF-8");  
  48.                 } catch (Exception e) {  
  49.             }  
  50.          return decoding;  
  51.      }  
  52.   
  53.     /** 
  54.      * @param args 
  55.      */  
  56.     public static void main(String[] args) {  
  57.         String name="你好";  
  58.          String tem=XOREncryptAndDecrypt.encrypt(name);  
  59.          System.out.println(tem);  
  60.          System.out.println(XOREncryptAndDecrypt.decrypt(tem));  
  61.   
  62.     }  
  63.   
  64. }  


2、IOS加解密算法

 在使用异或加解密时,加密和解密的密钥必须保持相同,否则解密时无法恢复为原来的内容。如果想在iOS与Java中通用,必须保持Java与IOS中的密钥一致。

1).h文件内容

 [objc] 

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface XOREncryptAndDecrypt : NSObject  
  4.   
  5. /** 
  6.  *  字符串加密 
  7.  * 
  8.  *  @param plainText 要加密的字符串 
  9.  * 
  10.  *  @return 加密后的字符串 
  11.  */  
  12. + (NSString *)encryptForPlainText:(NSString *)plainText;  
  13.   
  14. /** 
  15.  *  解密字符串 
  16.  * 
  17.  *  @param encryption 要解密的字符串 
  18.  * 
  19.  *  @return 要解密的字符串 如果不为加密字符则返回原字符串 
  20.  */  
  21. + (NSString *)decryptForEncryption:(NSString *)encryption;  
  22.   
  23. @end  


2).m文件内容

 [objc] 

  1. #import "XOREncryptAndDecrypt.h"  
  2. @implementation XOREncryptAndDecrypt  
  3. static NSString *secretKey =@"abcdefghijklmnopqrstuvwxyz"; ///<密钥 加解密的密钥必须相同 可自定义  
  4. #pragma mark 加密字符串  
  5. + (NSString *)encryptForPlainText:(NSString *)plainText  
  6. {  
  7. //保存加密后的字符  
  8. NSMutableString *encryption=[NSMutableString string];  
  9. //编码转换后的字符串 UTF_8->iso-8859-1  
  10. NSString *encoding=[[NSString alloc]initWithData:[plainText dataUsingEncoding:NSUTF8StringEncoding] encoding:NSISOLatin1StringEncoding];  
  11. for(int i=0,j=0;i<encoding.length;i++,j++){  
  12. if(j==secretKey.length){  
  13.             j=0;  
  14.         }  
  15. //异或后的字符  
  16. char cipher=(char)([encoding characterAtIndex:i]^[secretKey characterAtIndex:j]);  
  17. //NSLog(@"%c转成16进制的字符串:%@,%@",cipher,[NSString stringWithFormat:@"%hhx",cipher],[NSString stringWithFormat:@"%x",cipher&0xff]);  
  18. //转成16进制形式的字符串 x8b转成8b字符串  
  19. NSString *strCipher= [NSString stringWithFormat:@"%hhx",cipher];  
  20. if(strCipher.length == 1){  
  21.             [encryption appendFormat:@"0%@",strCipher];  
  22.         }else{  
  23.             [encryption appendString:strCipher];  
  24.         }  
  25.     }  
  26. return encryption;  
  27. }  
  28. #pragma mark 解密 如果不为加密字符则返回原字符  
  29. + (NSString *)decryptForEncryption:(NSString *)encryption {  
  30. //保存解密后的字符  
  31. NSMutableString *decryption=[NSMutableString string];  
  32. //解码字符  
  33. NSString *decoding=nil;  
  34. for(int i=0,j=0;i<encryption.length/2;i++,j++){  
  35. if(j==secretKey.length){  
  36.             j=0;  
  37.         }  
  38. //截取16进制形式的字符串 x8b中的8b  
  39. NSString *tem=[encryption substringWithRange:NSMakeRange(i*2, 2)];  
  40. char  *endptr;  
  41. //把16进制形式的字符串转为字符  
  42. char n=(char)(int)strtoul([tem UTF8String],&endptr,16);  
  43. //判断是否为加密字符  
  44. if (n==''&&*endptr!='') {  
  45.             [decryption setString:@""];  
  46. break;  
  47.         }  
  48.         [decryption appendFormat:@"%c",(char)(n^[secretKey characterAtIndex:j])];  
  49.     }  
  50. if (![decryption isEqualToString:@""]) {  
  51. //编码后的字符串 iso-8859-1 -> UTF_8  
  52.         decoding=[[NSString alloc]initWithData:[[decryption copy] dataUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];  
  53.     }  
  54. if (decoding==nil) {  
  55.         decoding=encryption;  
  56.     }  
  57. return decoding;  
  58. }  
  59. @end  
原文地址:https://www.cnblogs.com/akiha/p/6507011.html