我试图在 iOS 上实现一种加密方式。匹配在 JAVA 上运行的那个。
但我尝试的每一件事都会导致不同的加密模式
这是我用于加密的 Java 代码:
public static String encrypt(String plaintext)
throws Exception
{
Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sharedkey, "DESede"), new IvParameterSpec(sharedvector));
byte[] encrypted = c.doFinal(plaintext.getBytes("UTF-8"));
return Base64.encode(encrypted);
}
有什么简单的方法可以在目标 c 中使用 DESede/CBC/PKCS5Padding 吗?
这是我正在使用的代码,但它给出了不同的结果
+ (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);
//my shared key i changed to Zeros
NSMutableData *payload = [[NSMutableData alloc] init];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
[payload appendBytes:"00" length:1];
NSString *key = [[NSString alloc]initWithData:payload encoding:NSUTF8StringEncoding];
//my iV i changed it to zeros
NSMutableData *IV = [[NSMutableData alloc] init];
[IV appendBytes:"00" length:1];
[IV appendBytes:"00" length:1];
[IV appendBytes:"00" length:1];
[IV appendBytes:"00" length:1];
[IV appendBytes:"00" length:1];
[IV appendBytes:"00" length:1];
[IV appendBytes:"00" length:1];
[IV appendBytes:"00" length:1];
NSLog(@"key byte is %s", [payload bytes]);
NSLog(@"IV byte is %s", [IV 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 *)[payload bytes], //"123456789012345678901234", //key
kCCKeySize3DES,
[IV bytes], //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];
}
else
{
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
NSLog(@"data is: %@", myData);
result = [myData base64EncodedString];
// result = [[NSString alloc]initWithData:myData encoding:NSUTF8StringEncoding];
}
return result;
}