Code Monkey home page Code Monkey logo

zybxorencryptdemo's Introduction

ZYBXOREncryptDemo

OC 数据异或加密解密工具

ZYBXOREncryptUtil.h

@interface ZYBXOREncryptUtil : NSObject +(ZYBXOREncryptUtil * )shareInstance; //加密 -(NSString *)xor_encrypWithString:(NSString *)str;

//解密 -(NSString *)xor_decryptWithString:(NSString *)str;

@end

@interface NSData(XOREncrypt) /** 加密 */

  • (NSString *)xor_encrypt;

/** 解密 */

  • (NSString *)xor_decrypt;

@end

ZYBXOREncryptUtil.m

@implementation ZYBXOREncryptUtil static ZYBXOREncryptUtil *shareInstance =nil; +(ZYBXOREncryptUtil *)shareInstance { static dispatch_once_t once; dispatch_once(&once, ^{ shareInstance =[[self alloc]init]; }); return shareInstance; } //当使用alloc的时候回调用这个方法 (保证唯一性) +(instancetype)allocWithZone:(struct _NSZone *)zone { if(shareInstance==nil) { static dispatch_once_t once; dispatch_once(&once, ^{ shareInstance=[super allocWithZone:zone]; }); } return shareInstance; } //加密 -(NSString *)xor_encrypWithString:(NSString *)str { NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

return [data xor_encrypt]; } //解密 -(NSString *)xor_decryptWithString:(NSString *)str { NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

return [data xor_decrypt]; } @end //秘钥 static NSString const *PWD_MOBILE_V1 = @"UYWIWEHIUHCNOWEA23F3ASD98ASDNLK0230NSDLKCFN20";

@implementation NSData(XOREncrypt)

//解密

  • (NSString *)xor_decrypt { return [self xor_encrypt]; }

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

// 将OC字符串转换为C字符串 const char *keys = [PWD_MOBILE_V1 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 [[NSString alloc]initWithData:encryptData.copy encoding:NSUTF8StringEncoding]; }

@end

zybxorencryptdemo's People

Contributors

zybgithub avatar

Stargazers

马小羊 avatar CCST3chnology avatar Tomoconozaki avatar RubyPain avatar  avatar Adam avatar ZHOUYUBIN avatar

Watchers

James Cloos avatar ZHOUYUBIN avatar RubyPain avatar

Forkers

rubypain

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.