DES解密,16位key,解密返回nil 3C

本人在开发中遇到一个des加解密的问题。先上代码
#import "DesEncryption.h"
#import
#import "GTMBase64.h"

@implementation DesEncryption
#pragma mark +++++
//8位key解密有效(已验证)
+(NSString*) DecryptUseDES:(NSString*)cipherText key:(NSString*)key {
// 利用 GTMBase64 解碼 Base64 字串
NSData* cipherData = [GTMBase64 decodeString:cipherText];
unsigned char buffer[1024];
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;

// IV 偏移量不需使用
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding| kCCOptionECBMode,
                                      [key UTF8String],
                                      kCCKeySizeDES,
                                      nil,
                                      [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] autorelease];
}
return plainText;

}
////8位key加密有效(已验证)
+(NSString *) EncryptUseDES:(NSString *)clearText key:(NSString *)key
{
NSData *data = [clearText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
unsigned char buffer[1024];
memset(buffer, 0, sizeof(char));
size_t numBytesEncrypted = 0;

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding| kCCOptionECBMode,
                                      [key UTF8String],
                                      kCCKeySizeDES,
                                      nil,
                                      [data bytes],
                                      [data length],
                                      buffer,
                                      1024,
                                      &numBytesEncrypted);

NSString* plainText = nil;
if (cryptStatus == kCCSuccess) {
    NSData *dataTemp = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
    plainText = [GTMBase64 stringByEncodingData:dataTemp];
}else{
    NSLog(@"DES加密失败");
}
return plainText;

}
例如:秘钥:abcdefgh
明文:123456
密文:sHTd/9exYuk=

如果秘钥换成:abcdefghabcdefgh
明文:123456
密文:WcbWa42yxuo=

不做任何改动,依旧用上面的解密方法 ,返回的plainText 是nil
期间试图改变kCCKeySizeDES为kCCKeySizeAES128(因为考虑到秘钥长度变为16位),可是结果还是徒劳,
本人尝试改变CCCryptorStatus里面的各种参数,一无所获,特发此贴求助

1个回答

楼主解决了吗,我也遇到这样的情况啊,返回总是nil

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问