iOS 抑或演算法總結(XOR)

來源:互聯網
上載者:User

標籤:style   sig   des   call   gen   file   私密金鑰   cal   contents   

方法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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.