在使用异或加解密时,加密和解密的密钥必须保持相同,否则解密时无法恢复为原来的内容。
[java]
- public class XOREncryptAndDecrypt {
- //密钥 加解密的密钥必须相同 可自定义
- private static final String secretKey ="abcdefghijklmnopqrstuvwxyz";
- /**
- * 字符串加密
- * @param plainText 要加密的字符串
- * @return 加密后的字符串
- */
- 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;
- }
- /**
- * 解密字符串
- * @param encryption 要解密的字符串
- * @return 解密后的字符串
- */
- 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;
- }
- /**
- * @param args
- */
- 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
- /**
- * 字符串加密
- *
- * @param plainText 要加密的字符串
- *
- * @return 加密后的字符串
- */
- + (NSString *)encryptForPlainText:(NSString *)plainText;
- /**
- * 解密字符串
- *
- * @param encryption 要解密的字符串
- *
- * @return 要解密的字符串 如果不为加密字符则返回原字符串
- */
- + (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];
- //编码转换后的字符串 UTF_8->iso-8859-1
- 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]);
- //NSLog(@"%c转成16进制的字符串:%@,%@",cipher,[NSString stringWithFormat:@"%hhx",cipher],[NSString stringWithFormat:@"%x",cipher&0xff]);
- //转成16进制形式的字符串 x8b转成8b字符串
- 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;
- }
- //截取16进制形式的字符串 x8b中的8b
- NSString *tem=[encryption substringWithRange:NSMakeRange(i*2, 2)];
- char *endptr;
- //把16进制形式的字符串转为字符
- char n=(char)(int)strtoul([tem UTF8String],&endptr,16);
- //判断是否为加密字符
- if (n==' '&&*endptr!=' ') {
- [decryption setString:@""];
- break;
- }
- [decryption appendFormat:@"%c",(char)(n^[secretKey characterAtIndex:j])];
- }
- if (![decryption isEqualToString:@""]) {
- //编码后的字符串 iso-8859-1 -> UTF_8
- decoding=[[NSString alloc]initWithData:[[decryption copy] dataUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
- }
- if (decoding==nil) {
- decoding=encryption;
- }
- return decoding;
- }
- @end