iOS 抑或算法总结(XOR)

Posted 仲长可倾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 抑或算法总结(XOR)相关的知识,希望对你有一定的参考价值。

方法1:

//加密

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

{

     NSData *data = string;

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

   char *keyData = (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 = [data characterAtIndex:i];

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

            int chKey = [key characterAtIndex:j];

            chData = 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 = [[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 = [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 = [NSData dataWithContentsOfFile:file];

    return [data xor_encrypt];

}

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

    NSData *data = [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 = [[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)的主要内容,如果未能解决你的问题,请参考以下文章

iOS算法总结-堆排序

常见数据结构与算法整理总结

ACdream 1023 抑或

按位(位与或抑或)

SLAM算法总结——经典SLAM算法框架总结

iOS 证书详解