1

我用谷歌搜索了很多,但能够在 Objective-C 中使用 PKCS5 填充获得 Blowfish ECB 算法。

我已经尝试过这里的代码,但它没有给我正确的加密数据。即使我已经尝试过这里的代码,但它没有使用 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 填充

4

1 回答 1

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);

输出:

钥匙 426c6f77 66697368
字符串原始测试数据
数据原始 54657374 44617461
数据加密 ba5eb956 7e73ae1a b5513ea1 75a14019
encryptedBase64String ul65Vn5zrhrdmeYV9B5rQA==
数据解密 54657374 44617461
stringDecrypted TestData
于 2015-06-16T11:09:07.347 回答