澳门威利斯人_威利斯人娱乐「手机版」

来自 威利斯人娱乐 2019-11-30 23:37 的文章
当前位置: 澳门威利斯人 > 威利斯人娱乐 > 正文

iOS遇到的加密,IOS开发各种加解密

IOS开采各个加解密,编解码

1.AES加解密,给NSData增多连串 NSData AES

增添头文件#import

- (NSData *)AES256EncryptWithKey:(NSString *)key {//加密

char keyPtr[kCCKeySizeAES256 1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}

  • (NSData *)AES256DecryptWithKey:(NSString *)key {//解密
    char keyPtr[kCCKeySizeAES256 1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [self length];
    size_t bufferSize = dataLength kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
    kCCOptionPKCS7Padding | kCCOptionECBMode,
    keyPtr, kCCBlockSizeAES128,
    NULL,
    [self bytes], dataLength,
    buffer, bufferSize,
    &numBytesDecrypted);
    if (cryptStatus == kCCSuccess) {
    return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
    free(buffer);
    return nil;

}

 

2.base64编解码参谋

GTMDefines.h
GTMBase64.h
GTMBase64.m

 

3.各类加密,数字摘要。给NSString增加类型NSString Encrypto

(1).MD5数字摘要

- (NSString *)md5 {

const char *cStr = [self UTF8String];
unsigned char result[16];
CC_MD5( cStr, strlen(cStr), result );
return [NSString stringWithFormat:@XXXXXXXXXXXXXXXX,
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12]威尼斯人网上娱乐,, result[13], result[14], result[15]
];
}

 

(2)SHA1摘要

 

- (NSString*) sha1
{
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i  )
        [output appendFormat:@x, digest[i]];

    return output;
}

(3)base64编码

- (NSString *) base64
{    
    NSData * data = [self dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    data = [GTMBase64 encodeData:data]; 
    NSString * output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    return output; 
}

(4)SHA1与base64结合

- (NSString *) sha1_base64
{
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, data.length, digest);

    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
    base64 = [GTMBase64 encodeData:base64];

    NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding]; 
    return output; 
}


(5)MD5与base64结合
- (NSString *) md5_base64
{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest );

    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];
    base64 = [GTMBase64 encodeData:base64];

    NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding]; 
    return output;
}

 

4.3DES是生机勃勃种对称的加密方法,因为用的同二个密钥。

对此加解密的安全性什么大家能够google,baidu本人找资料参谋。

自己也但是是粗略的说一下通讯加密中的意气风发种可实现方案而已。

同等的3DES加密中央也都以联合的,系统也平素提供了API,基本代码如下

//3des加解密

  • (NSString*)TripleDES:(NSString*)plainText encryptOrDecrypt:(CCOperation)encryptOrDecrypt
    {
    const void *vplainText;
    size_t plainTextBufferSize;

if (encryptOrDecrypt == kCCDecrypt)//解密
{
NSData *EncryptData = [GTMBase64 decodeData:[plainText dataUsingEncoding:NSUTF8StringEncoding]];
plainTextBufferSize = [EncryptData length];
vplainText = [EncryptData bytes];
}
else //加密
{
NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
plainTextBufferSize = [data length];
vplainText = (const void *)[data bytes];
}

CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;

bufferPtrSize = (plainTextBufferSize kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
// memset((void *) iv, 0x0, (size_t) sizeof(iv));

const void *vkey = (const void *) [DESKEY UTF8String];
// NSString *initVec = @init Vec;
//const void *vinitVec = (const void *) [initVec UTF8String];
// Byte iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
ccStatus = CCCrypt(encryptOrDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
vkey,
kCCKeySize3DES,
nil,
vplainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
//if (ccStatus == kCCSuccess) NSLog(@SUCCESS);
/*else if (ccStatus == kCC ParamError) return @PARAM ERROR;
else if (ccStatus == kCCBufferTooSmall) return @BUFFER TOO SMALL;
else if (ccStatus == kCCMemoryFailure) return @MEMORY FAILURE;
else if (ccStatus == kCCAlignmentError) return @ALIGNMENT;
else if (ccStatus == kCCDecodeError) return @DECODE ERROR;
else if (ccStatus == kCCUnimplemented) return @UNIMPLEMENTED; */

NSString *result;

if (encryptOrDecrypt == kCCDecrypt)
{
result = [[[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr
length:(NSUInteger)movedBytes]
encoding:NSUTF8StringEncoding]
autorelease];
}
else
{
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
result = [GTMBase64 stringByEncodingData:myData];
}

return result;
}

1.AES加解密,给NSData加多连串 NSData AES 加多头文件#import - (NSData *)AES256EncryptWithKey:(NSString *)key {//加密 char keyPtr...

付出中遇见需求对密码加密之后,进行上传的标题。然后精晓了大器晚成晃可用,会用到的加密方法。

1.异或加密

安装字符串“t”为加密所需KEY,举办加密(依照要求不风度翩翩进行沟通)

威尼斯人网上娱乐 1

2.DES加密

Base64codeFunc.h

威尼斯人网上娱乐 2

Base64codeFunc.m   导入iOS自带加密库<CommonCrypto/CommonCryptor.h>

DES加密方法达成:

威尼斯人网上娱乐 3

DES解密方法达成:

威尼斯人网上娱乐 4

DES文本数据加密:

威尼斯人网上娱乐 5

DES文本数据解密:

威尼斯人网上娱乐 6

base64格式字符串转文本数据:

威尼斯人网上娱乐 7

威尼斯人网上娱乐 8

文本数据转换为base64格式字符串:

威尼斯人网上娱乐 9

本文由澳门威利斯人发布于威利斯人娱乐,转载请注明出处:iOS遇到的加密,IOS开发各种加解密

关键词: 澳门威利斯人 IOS