iOS blowfish加密解密

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS blowfish加密解密相关的知识,希望对你有一定的参考价值。

遇到一个需求,支付密码提交到服务器时使用blowfish加密,网上资料很少,找到的代码也跟在线加密出来的结果对不上,在线加密(用来确认加密结果是否有误):

blowfish在线加密

Blowfish加密模式:ECB

填充模式:PKCS5Padding

输出:base64

字符集:UTF8

最后在stackoverflow上找到了正解:

https://stackoverflow.com/questions/30860101/how-to-implement-blowfish-ecb-algorithm-pkcs5-padding-in-ios

 

是调用了原生API,自己稍微封装了下,写成NSString分类,分类.m文件中内容如下:

#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;
}
//返回的是base64字符串-加密
- (NSString *)blowFishEncodingWithKey:(NSString *)pkey{
    if (pkey.length<8 || pkey.length>56) {
        NSLog(@"key值的长度必须在[8,56]之间");
        return nil;
    }
    NSError *error;
    NSData *key = [pkey dataUsingEncoding:NSUTF8StringEncoding];
    NSString *stringOriginal = self;
    NSData *dataOriginal = [stringOriginal dataUsingEncoding:NSUTF8StringEncoding];;
    
//    NSLog(@"key %@", key);
//    NSLog(@"stringOriginal %@", stringOriginal);
//    NSLog(@"dataOriginal   %@", dataOriginal);
    
    NSData *dataEncrypted = [NSString doBlowfish:dataOriginal
                                                   context:kCCEncrypt
                                                       key:key
                                                   options:kCCOptionPKCS7Padding | kCCOptionECBMode
                                                        iv:nil
                                                     error:&error];
//    NSLog(@"dataEncrypted  %@", dataEncrypted);
    
    NSString *encryptedBase64String = [dataEncrypted base64EncodedStringWithOptions:0];
//    NSLog(@"encryptedBase64String  %@", encryptedBase64String);
    return encryptedBase64String;
    
    
    
    
}
//需要base64字符串调用,返回的是解密结果-解密
- (NSString *)blowFishDecodingWithKey:(NSString *)pkey{
    if (pkey.length<8 || pkey.length>56) {
        NSLog(@"key值的长度必须在[8,56]之间");
        return nil;
    }
    NSError *error;
    NSData *key = [pkey dataUsingEncoding:NSUTF8StringEncoding];
    
    NSData *dataToDecrypt = [[NSData alloc] initWithBase64EncodedString:self options:0];
    
    NSData *dataDecrypted = [NSString 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);
    return stringDecrypted;
}

使用时只需:

NSString * base64 = [@"123456" blowFishEncodingWithKey:@"12345678"];
NSString * result = [base64 blowFishDecodingWithKey:@"12345678"];
NSLog(@"加密后的base64:%@    解密结果:%@",base64,result);

相关参考资料:

简书(我试过这个,加密出来的结果跟网站上的结果差一部分,暂时不知道原因)

以上是关于iOS blowfish加密解密的主要内容,如果未能解决你的问题,请参考以下文章

将 Blowfish 加密从 Java 转换为 Python

如何将 Blowfish 加密添加到 Winsock 数据?

对称加密和非对称加密

几种加密算法的测试,包括对称加密和非对称加密

使用 nodejs crypto 与 php 的 mcrypt 解密 Blowfish-ecb

Blowfish 和 Blowfish-compat 有啥区别?