iOS DES ECB 模式加密

本文详细介绍了在iOS中使用DES算法的ECB模式进行数据加密的方法,包括具体实现代码和Base64编码过程,为iOS开发者提供了实用的加密技术指导。
//iOS DES  ECB 模式加密 
#import <CommonCrypto/CommonCryptor.h>


static Byte iv[] = {1,2,3,4,5,6,7,8};
+(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key

 {
    
         NSString *ciphertext = nil;
    
         const char *textBytes = [plainText UTF8String];
    
         NSUInteger dataLength = [plainText length];
     

        unsigned char buffer[1024];
    
         memset(buffer, 0, sizeof(char));
    
         size_t numBytesEncrypted = 0;
   
         CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                                 
                                                                                            kCCOptionECBMode|kCCOptionPKCS7Padding,  //kCCOptionECBMode  kCCOptionPKCS7Padding
                                                 
                                                                                            [key UTF8String], kCCKeySizeDES,
                                                 
                                                                                            iv,
                                                 
                                                                                          textBytes, dataLength,
                                                 
                                                                                            buffer, 1024,
                                                 
                                                                                            &numBytesEncrypted);
            if (cryptStatus == kCCSuccess) {
        
                
                 NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
                //NSLog(@"ssf:%s",buffer);
                ciphertext = [ViewController base64Encoding:data];
               // NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
                //        Byte* bb = (Byte*)[data bytes];
                //        ciphertext = [self parseByteArray2HexString:bb];
        
             }
    
         return ciphertext;
   
     
}



 static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


+(NSString *)base64Encoding:(NSData*) text
{
    if (text.length == 0)
        return @"";
    
    char *characters = malloc(text.length*3/2);
    
    if (characters == NULL)
        return @"";
    
    int end = text.length - 3;
    int index = 0;
    int charCount = 0;
    int n = 0;
    
    while (index <= end) {
        int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16)
        | (((int)(((char *)[text bytes])[index + 1]) & 0x0ff) << 8)
        | ((int)(((char *)[text bytes])[index + 2]) & 0x0ff);
        
        characters[charCount++] = encodingTable[(d >> 18) & 63];
        characters[charCount++] = encodingTable[(d >> 12) & 63];
        characters[charCount++] = encodingTable[(d >> 6) & 63];
        characters[charCount++] = encodingTable[d & 63];
        
        index += 3;
        
        if(n++ >= 14)
        {
            n = 0;
            characters[charCount++] = ' ';
        }
    }
    
    if(index == text.length - 2)
    {
        int d = (((int)(((char *)[text bytes])[index]) & 0x0ff) << 16)
        | (((int)(((char *)[text bytes])[index + 1]) & 255) << 8);
        characters[charCount++] = encodingTable[(d >> 18) & 63];
        characters[charCount++] = encodingTable[(d >> 12) & 63];
        characters[charCount++] = encodingTable[(d >> 6) & 63];
        characters[charCount++] = '=';
    }
    else if(index == text.length - 1)
    {
        int d = ((int)(((char *)[text bytes])[index]) & 0x0ff) << 16;
        characters[charCount++] = encodingTable[(d >> 18) & 63];
        characters[charCount++] = encodingTable[(d >> 12) & 63];
        characters[charCount++] = '=';
        characters[charCount++] = '=';
    }
    NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];
    return rtnStr;
}

 

转载于:https://www.cnblogs.com/qingjoin/p/3347585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值