DES 算法

 1.iOS DES算法实现
 2.输出结果
 3.安卓端代码比较
 4.注意事项
// 1.iOS DES算法实现
//
//  ViewController.m
//  DES加密
//
//  Created by mac on 14/12/9.
//  Copyright (c) 2014年 刘佳达. All rights reserved.
//

#import "ViewController.h"
#import "GTMBase64.h"
#import "GTMDefines.h"
#import 
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *key = @"abcdefgh";//密钥
    NSLog(@"加密后的数据是:%@", [self encrypUseDES:@"123 456" key:key]);
    NSLog(@"解密后的数据是:%@", [self decryptUseDES:@"aR/T8qHQ4uY=" key:key]);
}
//DES加密
- (NSString *)encrypUseDES:(NSString *)plainText key:(NSString*)key
{
    NSString *ciphertext = nil;
    NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [textData length];
    unsigned char buffer[dataLength*10];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | 
                                          kCCOptionECBMode,
                                          [key UTF8String], kCCKeySizeDES,
                                          nil,
                                          [textData bytes],[textData length],
                                          buffer, dataLength*10,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer 
                                      length: (NSUInteger)numBytesEncrypted];
        ciphertext = [[NSString alloc]initWithData:[GTMBase64 
    encodeData:data] encoding:NSUTF8StringEncoding];

    }
    return ciphertext;
}
//DES解密
- (NSString *) decryptUseDES:(NSString*)cipherText key:(NSString*)key
{
    
    //文件值--base64解密 --
    NSData* cipherData = [GTMBase64 decodeString:cipherText];
    NSUInteger dataLength = [cipherData length];
    unsigned char buffer[dataLength*10];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | 
                                          kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          nil,
                                          [cipherData bytes],
                                          [cipherData length],
                                          buffer,
                                          dataLength*10,
                                          &numBytesDecrypted);
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData* data = [NSData dataWithBytes:buffer
                                      length:(NSUInteger)numBytesDecrypted];
        plainText = [[NSString alloc] initWithData:data 
    encoding:NSUTF8StringEncoding];
    }
    return plainText;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
2.输出测试结果

2016-10-10 10:25:46.055 DES加密[11808:1082206] 加密后的数据是:aR/T8qHQ4uY=
2016-10-10 10:25:46.056 DES加密[11808:1082206] 解密后的数据是:123 456

为了和安卓统一结果,下面附上安卓代码。

public static String desCrypto(String content, String password) {
         try {
      return base64Encode(desCrypto(content.getBytes("utf-8"), password));
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } 
         return null;
    }
  public static byte[] desCrypto(byte[] datasource, String password) {              
        try{  
        SecureRandom random = new SecureRandom();  
        DESKeySpec desKey = new DESKeySpec(password.getBytes());  
        //创建一个密匙工厂,然后用它把DESKeySpec转换成  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
        SecretKey securekey = keyFactory.generateSecret(desKey);  
        //Cipher对象实际完成加密操作  
        Cipher cipher = Cipher.getInstance("DES");  
        //用密匙初始化Cipher对象  
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);  
        //现在,获取数据并加密  
        //正式执行加密操作  
        return cipher.doFinal(datasource);  
        }catch(Throwable e){  
                e.printStackTrace();  
        }  
        return null;  
}
4.注意事项

   CCCryptorStatus CCCrypt(
    CCOperation op,       // 设置  kCCEncrypt 、kCCDecrypt    加密|解密
    CCAlgorithm alg,      // kCCAlgorithmDES    哪种算法
    CCOptions options,    // kCCOptionPKCS7Padding |kCCOptionECBMode, 两种方式
    const void *key,      // [key UTF8String]
    size_t keyLength,     // kCCKeySizeDES
    const void *iv,       // iOS使用nil,和安卓统一。
    const void *dataIn,   // [textData bytes]
    size_t dataInLength,  // [textData length]
    void *dataOut,        // buffer
    size_t dataOutAvailable, // dataLength*10
    size_t *dataOutMoved) // &numBytesEncrypted
    __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

你可能感兴趣的:(DES 算法)