java Cipher.getInstance的相等目标C结果(" DESede / ECB / NoPadding")

时间:2016-10-25 14:14:13

标签: objective-c 3des

完成Java代码以生成3DES结果,段代码如下:

Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding"); 
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(data);

现在,我需要在Objective-C中实现相同的结果,下面是我的尝试,但结果甚至不是很接近。

+(NSString *) try3DES:(NSString *)plainText key:(NSString *)key
{
    NSString *ciphertext = nil;
    NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [textData length];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding,
                                      [key UTF8String],
                                      kCCKeySizeDES,
                                      nil,
                                      [textData bytes],
                                      dataLength,
                                      buffer,
                                      1024,
                                      &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];

        ciphertext = [self convertDataToHexStr:data];
    }
    return ciphertext;
}

有人可以提供帮助吗?

0 个答案:

没有答案