IOS or Algorithmic summary (XOR)

Source: Internet
Author: User

Method 1:

Encryption

-(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];

}

Decrypt

-(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;

}

Method 2:

Client built-in private keyStaticNSStringConst *PRIVATEKEY =@ "Ef37c9111210854f5986fc9ebb5548b2ae";@implementationNSData (Xorencrypt)-(NSData *) xor_decrypt{return [Self xor_encrypt];}Encryption-(NSData *) xor_encrypt{Get the length of the keyNsinteger length = privatekey.length;Converting an OC string to a C stringConstchar *keys = [Privatekey cstringusingencoding:Nsasciistringencoding];UnsignedChar Ckey[length]; memcpy (Ckey, keys, length);//data initialization, space unassigned mates use appendbytes nsmutabledata *encryptData = [ [nsmutabledata alloc] initwithcapacity:length]; //get byte pointer const byte *point =  Self.bytes; for (int i = 0; i < SELF.LENGTH; i++) {int l = i% length; //calculates the current position byte, the key byte to be the XOR operation of the key, char C = ckey[l]; byte B = (byte) ((Point[i]) ^ c); //xor operation [encryptData appendbytes:&b length:1]; //append byte} return encryptdata.copy;}  @end             

Method 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)

/** Encryption */

-(NSData *) Xor_encrypt;

/** Decryption */

-(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

Client built-in private key

static NSString Const *privatekey = @ "Ef37c9111210854f5986fc9ebb5548b2ae";

@implementation NSData (Xorencrypt)

Decrypt

-(NSData *) Xor_decrypt {

return [self xor_encrypt];

}

Encryption

-(NSData *) Xor_encrypt {

Get the length of the key

Nsinteger length = privatekey.length;

Converting an OC string to a C string

const char *keys = [Privatekey cstringusingencoding:nsasciistringencoding];

unsigned char ckey[length];

memcpy (Ckey, keys, length);

Data initialization, space not allocated with use of appendbytes

Nsmutabledata *encryptdata = [[Nsmutabledata alloc] initwithcapacity:length];

Get byte pointer

Const Byte *point = self.bytes;

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

int L = i% length; Calculates the key bytes of the current position byte, and the XOR operation of the key

char C = ckey[l];

byte B = (byte) ((Point[i]) ^ c); XOR or operation

[EncryptData appendbytes:&b length:1]; Append bytes

}

return encryptdata.copy;

}

@end

IOS or Algorithmic summary (XOR)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.