首页 > 代码库 > iOS 抑或算法总结(XOR)

iOS 抑或算法总结(XOR)

方法1:

//加密

-(NSString *)obfuscate:(NSData *)string withKey:(NSString *)key

{

     NSData *data = http://www.mamicode.com/string;

     char *dataPtr = (char *) [data bytes];

   char *keyData = http://www.mamicode.com/(char *) [[key dataUsingEncoding:NSUTF8StringEncoding] bytes];

     char *keyPtr = keyData;

     int keyIndex = 0;

     for (int x = 0; x < [data length]; x++){

      *dataPtr = *dataPtr++ ^ *keyPtr++;

          if (++keyIndex == [key length]) keyIndex = 0, keyPtr = keyData;

}

      return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

}

//解密

 -(NSString*)encodeString:(NSString*)data :(NSString*)key{

    NSString *result=[NSString string];

    for(int i=0; i < [data length]; i++){

        int chData = http://www.mamicode.com/[data characterAtIndex:i];

        for(int j=0;j<[key length];j++){

            int chKey = [key characterAtIndex:j];

            chData = http://www.mamicode.com/chData^chKey;

        }

        result=[NSString stringWithFormat:@"%@%@",result,[NSString stringWithFormat:@"%c",chData]];

    }

    return result;

}

方法2:

// 客户端内置私钥
static NSString const *privateKey = @"ef37c9111210854f5986fc9ebb5548b2ae";

@implementation NSData(XOREncrypt)
- (NSData *)xor_decrypt
{
    return [self xor_encrypt];
}

// 加密
- (NSData *)xor_encrypt
{
    // 获取key的长度
    NSInteger length = privateKey.length;

    // 将OC字符串转换为C字符串
    const char *keys = [privateKey cStringUsingEncoding:NSASCIIStringEncoding];

    unsigned char cKey[length];

    memcpy(cKey, keys, length);

    // 数据初始化,空间未分配 配合使用 appendBytes
    NSMutableData *encryptData = http://www.mamicode.com/[[NSMutableData alloc] initWithCapacity:length];

    // 获取字节指针
    const Byte *point = self.bytes;

    for (int i = 0; i < self.length; i++) {
        int l = i % length;                     // 算出当前位置字节,要和密钥的异或运算的密钥字节
        char c = cKey[l];
        Byte b = (Byte) ((point[i]) ^ c);       // 异或运算
        [encryptData appendBytes:&b length:1];  // 追加字节
    }
    return encryptData.copy;
}
@end

方法3:

#import <Foundation/Foundation.h>

@interface SQHXOREncryptUtil : NSObject

+ (BOOL)xor_encryptWithString:(NSString *)originString toFile:(NSString *)file;

+ (NSData *)xor_encryptWithFilename:(NSString *)filename type:(NSString *)type;

+ (NSData *)xor_encryptWithFile:(NSString *)file;

+ (NSData *)xor_encryptWithString:(NSString *)originString;

@end

@interface NSData(XOREncrypt)

/** 加密 */

- (NSData *)xor_encrypt;

/** 解密 */

- (NSData *)xor_decrypt;

@end

#import "SQHXOREncryptUtil.h"

@implementation SQHXOREncryptUtil

+ (BOOL)xor_encryptWithString:(NSString *)originString toFile:(NSString *)file {

    NSData *encryptData = http://www.mamicode.com/[self xor_encryptWithString:originString];

    return [encryptData writeToFile:file atomically:YES];

}

+ (NSData *)xor_encryptWithFilename:(NSString *)filename type:(NSString *)type {

    NSString *filepath = [[NSBundle mainBundle] pathForResource:filename ofType:type];

    return [self xor_encryptWithFile:filepath];

}

+ (NSData *)xor_encryptWithFile:(NSString *)file {

    NSData *data = http://www.mamicode.com/[NSData dataWithContentsOfFile:file];

    return [data xor_encrypt];

}

+ (NSData *)xor_encryptWithString:(NSString *)originString {

    NSData *data = http://www.mamicode.com/[originString dataUsingEncoding:NSUTF8StringEncoding];

    return [data xor_encrypt];

}

@end

 // 客户端内置私钥

static NSString const *privateKey = @"ef37c9111210854f5986fc9ebb5548b2ae";

@implementation NSData(XOREncrypt)

// 解密

- (NSData *)xor_decrypt {

    return [self xor_encrypt];

}

// 加密

- (NSData *)xor_encrypt {

    // 获取key的长度

    NSInteger length = privateKey.length;

    // 将OC字符串转换为C字符串

    const char *keys = [privateKey cStringUsingEncoding:NSASCIIStringEncoding];

    unsigned char cKey[length];

    memcpy(cKey, keys, length);

    // 数据初始化,空间未分配 配合使用 appendBytes

    NSMutableData *encryptData = http://www.mamicode.com/[[NSMutableData alloc] initWithCapacity:length];

    // 获取字节指针

    const Byte *point = self.bytes;

    for (int i = 0; i < self.length; i++) {

        int l = i % length;                     // 算出当前位置字节,要和密钥的异或运算的密钥字节

        char c = cKey[l];

        Byte b = (Byte) ((point[i]) ^ c);       // 异或运算

        [encryptData appendBytes:&b length:1];  // 追加字节

    }

    return encryptData.copy;

 }

@end

 

iOS 抑或算法总结(XOR)