iOS blowfish加密解密

來源:互聯網
上載者:User

標籤:gen   ror   字元   odi   hbase   userinfo   erro   ddd   encrypt   

遇到一個需求,支付密碼提交到伺服器時使用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加密解密

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.