2010-08-09 100 views
2

我想在iphone上使用3des加密一些東西,它必須匹配java和.NET的結果。iPhone 3Des加密匹配Java和.NET密鑰問題,SecretKeySpec?

的代碼,我是:

+ (NSString*) doCipher:(NSString*)plainText:(CCOperation)encryptOrDecrypt { 

const void *vplainText; 
size_t plainTextBufferSize; 

if (encryptOrDecrypt == kCCDecrypt) 
{ 
    NSData *EncryptData = [NSData dataWithBase64EncodedString:plainText]; 
    plainTextBufferSize = [EncryptData length]; 
    vplainText = [EncryptData bytes]; 
} 
else 
{ 
    NSData *tempData = [plainText dataUsingEncoding:NSASCIIStringEncoding]; 
    plainTextBufferSize = [tempData length]; 
    vplainText = [tempData bytes]; 
} 

CCCryptorStatus ccStatus; 
uint8_t *bufferPtr = NULL; 
size_t bufferPtrSize = 0; 
size_t movedBytes = 0; 
// uint8_t ivkCCBlockSize3DES; 

bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1); 
bufferPtr = malloc(bufferPtrSize * sizeof(uint8_t)); 
memset((void *)bufferPtr, 0x0, bufferPtrSize); 

NSString *key = [NSString MD5:@"HSDNIFFU"]; 

NSData *_keyData = [key dataUsingEncoding:NSASCIIStringEncoding]; 

NSLog(@"key byte is %s", [_keyData bytes]); 

// Initialization vector; dummy in this case 0's. 
uint8_t iv[kCCBlockSize3DES]; 
memset((void *) iv, 0x0, (size_t) sizeof(iv)); 

ccStatus = CCCrypt(encryptOrDecrypt, 
        kCCAlgorithm3DES, 
        kCCOptionPKCS7Padding, 
        (const void *)[_keyData bytes], //"123456789", //key 
        kCCKeySize3DES, 
        iv, //iv, 
        vplainText, //plainText, 
        plainTextBufferSize, 
        (void *)bufferPtr, 
        bufferPtrSize, 
        &movedBytes); 

//if (ccStatus == kCCSuccess) NSLog(@"SUCCESS"); 
/*else*/ if (ccStatus == kCCParamError) return @"PARAM ERROR"; 
else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL"; 
else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE"; 
else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT"; 
else if (ccStatus == kCCDecodeError) return @"DECODE ERROR"; 
else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED"; 

NSString *result; 

if (encryptOrDecrypt == kCCDecrypt) 
{ 

// result = [[NSString alloc] initWithData: [NSData dataWithBytes:(const void *)bufferPtr length:[(NSUInteger)movedBytes] encoding:NSASCIIStringEncoding]]; 
    result = [[[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSASCIIStringEncoding] autorelease]; 
} 
else 
{ 
    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes]; 
    NSLog(@"data is: %@", myData); 
    result = [myData base64Encoding]; 
} 
return result; 

}

此代碼成功地加密和解密的字符串。正如你所看到的,它在密鑰上使用了md5。但是,它不符合.NET和java的結果。

從Java開發人員的Java代碼看起來像:

public static byte[] encryptTripleDES(String message) throws Exception { 
    final MessageDigest md = MessageDigest.getInstance("md5"); 
    final byte[] digestOfPassword = md.digest("--KEY--".getBytes("utf-8")); 
    final SecretKey key = new SecretKeySpec(digestOfPassword, "DESede"); 
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
    final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); 
    cipher.init(Cipher.ENCRYPT_MODE, key, iv); 
    return cipher.doFinal(message.getBytes("utf-8")); 
} 

可惜我不是太熟悉Java加密。我注意到,在KEY上使用md5後,它使用SecretKeySpec方法創建一個SecretKey。我的Java開發人員告訴我他需要使用密鑰的字節數組創建一個secretkey,以便它可以匹配.NET密鑰。

任何人都可以解釋這對我更好,並幫助我一個解決方案,以匹配的.NET和Java的同行結果?

回答

1

我想通了。我在md5之後生成的密鑰是32個字節。從閱讀其他帖子看來,kCCKeySize3DES似乎只有24個字節。如果我把一個24字節或更少的密鑰(沒有MD5),這似乎很好地工作。

+0

你能幫我在這裏嗎? http://stackoverflow.com/questions/12607937/3des-result-in-java-produces-different-result-from-3des-ios-version – kinghomer 2012-09-29 15:42:54

+0

@ tony.tc.leung,你在java端改變了什麼和IOS方,請將代碼清楚,這將是很大的幫助。謝謝 – 2013-06-26 12:01:11