我已经google了很多但是能够在Objective-C中使用PKCS5填充获得Blowfish ECB算法。
我尝试过来自here的代码,但它没有给我正确的加密数据。即使我尝试过来自here的代码,但它不是PKSC5填充。
不幸的是我必须使用Blowfish(没有其他选项)来转换Objective-C中的以下JAVA代码
String objVal=<the json>;
SecretKeySpec lKeySpec = new SecretKeySpec(lKey.getBytes("UTF8"),"Blowfish");
Cipher lCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
lCipher.init(Cipher.ENCRYPT_MODE, lKeySpec);
byte[] lPassword = objVal.getBytes("UTF8");
byte[] lEncryptPassword = lCipher.doFinal(lPassword);
String lEncryptString = new BASE64Encoder().encode(lEncryptPassword);
StringBuffer nString = new StringBuffer();
for (int i = 0; i < lEncryptString.length(); i++) {
int a = lEncryptString.charAt(i);
if (a != 13 && a != 10 && !lEncryptString.substring(i, i + 1).equals(" ")){
nString.append(lEncryptString.charAt(i));
}
return nString.toString();
然后加密的json被编码:
String returnData=<encrypted json>
byte[] inputBytes = returnData.getBytes();
returnData = DatatypeConverter.printBase64Binary(inputBytes);
任何人都尝试并测试了使用PKSC5填充的Blowfish ECB算法的解决方案。
提前谢谢。
我知道这个问题已经(好几次)被问过,但要么没有回答PKCS5填充
答案 0 :(得分:3)
示例代码:
添加Security.framework
#import <CommonCrypto/CommonCryptor.h>
+ (NSData *)doBlowfish:(NSData *)dataIn
context:(CCOperation)kCCEncrypt_or_kCCDecrypt
key:(NSData *)key
options:(CCOptions)options
iv:(NSData *)iv
error:(NSError **)error
{
CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0;
NSMutableData *dataOut = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeBlowfish];
ccStatus = CCCrypt( kCCEncrypt_or_kCCDecrypt,
kCCAlgorithmBlowfish,
options,
key.bytes,
key.length,
(iv)?nil:iv.bytes,
dataIn.bytes,
dataIn.length,
dataOut.mutableBytes,
dataOut.length,
&cryptBytes);
if (ccStatus == kCCSuccess) {
dataOut.length = cryptBytes;
}
else {
if (error) {
*error = [NSError errorWithDomain:@"kEncryptionError"
code:ccStatus
userInfo:nil];
}
dataOut = nil;
}
return dataOut;
}
测试:
NSError *error;
NSData *key = [@"Blowfish" dataUsingEncoding:NSUTF8StringEncoding];
NSString *stringOriginal = @"TestData";
NSData *dataOriginal = [stringOriginal dataUsingEncoding:NSUTF8StringEncoding];;
NSLog(@"key %@", key);
NSLog(@"stringOriginal %@", stringOriginal);
NSLog(@"dataOriginal %@", dataOriginal);
NSData *dataEncrypted = [Test doBlowfish:dataOriginal
context:kCCEncrypt
key:key
options:kCCOptionPKCS7Padding | kCCOptionECBMode
iv:nil
error:&error];
NSLog(@"dataEncrypted %@", dataEncrypted);
NSString *encryptedBase64String = [dataEncrypted base64EncodedStringWithOptions:0];
NSLog(@"encryptedBase64String %@", encryptedBase64String);
NSData *dataToDecrypt = [[NSData alloc] initWithBase64EncodedString:encryptedBase64String options:0];
NSData *dataDecrypted = [Test doBlowfish:dataToDecrypt
context:kCCDecrypt
key:key
options:kCCOptionPKCS7Padding | kCCOptionECBMode
iv:nil
error:&error];
NSLog(@"dataDecrypted %@", dataDecrypted);
NSString *stringDecrypted = [[NSString alloc] initWithData:dataDecrypted encoding:NSUTF8StringEncoding];
NSLog(@"stringDecrypted %@", stringDecrypted);
输出:
key 426c6f77 66697368 stringOriginal TestData dataOriginal 54657374 44617461 dataEncrypted ba5eb956 7e73ae1a b5513ea1 75a14019 encryptedBase64String ul65Vn5zrhrdmeYV9B5rQA== dataDecrypted 54657374 44617461 stringDecrypted TestData