Giter Club home page Giter Club logo

ttsm4's Introduction

TTSM4

国密算法SM4的OC封装

Install

pod 'TTSM4', '~> 1.0.0'

用法

1.CBC模式:需要密钥和初始化向量来初始化,且长度都为16字节

NSString *secretKey = @"JeF8U9wHFOMfs2Y8";//密钥

NSString *iv = @"UISwD9fW6cFh9SNS";//初始化向量

TTAlgorithmSM4 *sm4 = [TTAlgorithmSM4 cbcSM4WithKey:secretKey iv:iv];

//原始数据
NSDictionary *msgDic = @{@"name":@"TTwong", @"country":@"**"};
NSError *error;
NSData *msgData = [NSJSONSerialization dataWithJSONObject:msgDic options:0 error:&error];
//原始字符串
NSString *msgString = [[NSString alloc]initWithData:msgData encoding:NSUTF8StringEncoding];

//加密后的字符串
NSString *encryptionString = [sm4 encryption:msgString];

//解密后的字符串
NSString *decryptionString = [sm4 decryption:encryptionString];

//恢复到原始数据
NSMutableData *msgdata = [[NSMutableData alloc]initWithData:[decryptionString dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:msgdata options:NSJSONReadingMutableLeaves error:&error];

2.ECB模式:需要密钥来初始化,密钥长度为16字节

NSString *secretKey = @"JeF8U9wHFOMfs2Y8";//密钥

TTAlgorithmSM4 *sm4 = [TTAlgorithmSM4 ecbSM4WithKey:secretKey];

//原始数据
NSDictionary *msgDic = @{@"name":@"TTwong", @"country":@"**"};
NSError *error;
NSData *msgData = [NSJSONSerialization dataWithJSONObject:msgDic options:0 error:&error];
NSString *msgString = [[NSString alloc]initWithData:msgData encoding:NSUTF8StringEncoding];

//加密后的字符串
NSString *encryptionString = [sm4 encryption:msgString];

//解密后的字符串
NSString *decryptionString = [sm4 decryption:encryptionString];

//恢复到原始数据
NSMutableData *msgdata = [[NSMutableData alloc]initWithData:[decryptionString dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:msgdata options:NSJSONReadingMutableLeaves error:&error];

ttsm4's People

Contributors

ttoooo avatar

Watchers

 avatar

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.