我有一个用 obj-c 代码加密的文件:
NSMutableData *fileData = [NSMutableData new];
// Get file data.
[[self encrypt::fileData WithKey:@"SOME_KEY"]]
- (NSMutableData*) encrypt:(NSMutableData*)data WithKey: (NSString *) key
{
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyBuffer[kCCKeySizeAES128+1]; // room for terminator (unused)
bzero( keyBuffer, sizeof(keyBuffer) ); // fill with zeroes (for padding)
[key getCString: keyBuffer maxLength: sizeof(keyBuffer) encoding: NSUTF8StringEncoding];
// encrypts in-place, since this is a mutable data object
size_t numBytesEncrypted = 0;
size_t returnLength = ([data length] + kCCKeySizeAES256) & ~(kCCKeySizeAES256 - 1);
char* returnBuffer = malloc(returnLength * sizeof(uint8_t) );
CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmAES128 , kCCOptionPKCS7Padding | kCCOptionECBMode,
keyBuffer, kCCKeySizeAES128, nil,
[data bytes], [data length],
returnBuffer, returnLength,
&numBytesEncrypted);
if(result == kCCSuccess)
return [NSMutableData dataWithBytes:returnBuffer length:numBytesEncrypted];
else
return nil;
}
我想用java读取这个文件。我在尝试
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
// What is salt ??
KeySpec spec = new PBEKeySpec("SOME_KEY", salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
String plaintext = new String(cipher.doFinal(ciphertext), "UTF-8");
System.out.println(plaintext);
我对obj-c一无所知。你认为我能完成这件事吗?谢谢你。