iOS:DES加密iOS和Android的匹配

android端的加密代码:

public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";

private static String encode(String key, byte[] data) throws Exception {
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
        AlgorithmParameterSpec paramSpec = iv;
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data);
        return Base64.encodeToString(bytes, 0);
    } catch (Exception e) {
        throw new Exception(e);
    }
}

首先要关注的是 ALGORITHM_DES = “DES/CBC/PKCS5Padding”这个变量,从中我们可以看出这是DES的CBC加密模式,用的是PKCS5Padding的填充模式。

之后代码使用ALGORITHM_DES来变量初始化Cipher

Cipher cipher = Cipher.getInstance(ALGORITHM_DES);

OC段加密代码

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding,
                                      [key UTF8String], kCCKeySizeDES,
                                      iv,
                                      textBytes, dataLength,
                                      buffer, 1024,
                                      &numBytesEncrypted);

参数说明:

kCCEncrypt:告诉函数执行加密过程,对应的也存在解密过程;
kCCAlgorithmDES:告诉函数执行DES加密;
kCCOptionPKCS7Padding:运用CBC加密模式,并且使用PKCS7Padding的填充模式;
[key UTF8String]:秘钥;
kCCKeySizeDES:秘钥的大小,(kCCKeySizeDES=8) ;
iv:可选的初始矢量 ;
textBytes:要加密的源数据;
dataLength: 数据的大小 ;
buffer:加密后的返回数据;
numBytesEncrypted:返回的数据大小;

JAVA和OC的差异说明:

对于iOS来说,只有PKCS7Padding这一种填充方式,而JAVA有多种。iOS的DES中加密数据包单位长度是8字节,8字节的情况下PKCS7Padding等价与JAVA的PKCS5Padding填充方式。

具体OC代码如下:

//DES加密
+ (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));
    const void *iv = (const void *)[key UTF8String];
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String], kCCKeySizeDES,
                                          iv,
                                          textBytes, dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        ciphertext = [data base64EncodedStringWithOptions:0];
    }
    return ciphertext;
}

//DES解密
- (NSString *) decryptUseDES:(NSString*)cipherText key:(NSString*)key
{
    NSData *cipherData = [[NSData alloc] initWithBase64EncodedString:self options:0];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    Byte iv[] = {1,2,3,4,5,6,7,8};
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          iv,
                                          [cipherData bytes],
                                          [cipherData length],
                                          buffer,
                                          1024,
                                          &numBytesDecrypted);
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    return plainText;
}

头文件:

#import <CommonCrypto/CommonCryptor.h>

结尾:

两边需要同步的地方:

1、算法:都是DES;
2、模式:
CBC = kCCOptionPKCS7Padding
EBC = kCCOptionPKCS7Padding|kCCOptionECBMode
3、秘钥:例如:tianwanggaidihu
4、IV变量:
JAVA:
IvParameterSpec iv = new IvParameterSpec(“12347890”.getBytes());
OC:
Byte iv[] = {1,2,3,4,7,8,9,0};
5、编码模式:base64。

参考链接:

DES:
http://blog.csdn.net/u010184533/article/details/38975871
http://blog.csdn.net/dqmj2/article/details/47983351

3DES:
http://blog.csdn.net/lyq8479/article/details/8062867#

发布了14 篇原创文章 · 获赞 11 · 访问量 4万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览