IOS Blowfish Encryption Decryption

Source: Internet
Author: User

Encounter a demand, pay password submitted to the server using Blowfish encryption, online data is very few, found the code is also with the results of online encryption, online encryption (to confirm the encryption results are incorrect):

Blowfish Online Encryption

Blowfish Encryption Mode: ECB

Fill mode: pkcs5padding

Output: Base64

Character Set: UTF8

Finally, a positive solution was found on the StackOverflow:

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

is to call the native API, which is slightly encapsulated below, written nsstring classification, classification. m files are as follows:

#import<CommonCrypto/CommonCryptor.h>//Core Code+ (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.M Utablebytes, Dataout.length,&cryptbytes); if(Ccstatus = =kccsuccess) {Dataout.length=cryptbytes; }    Else {        if(Error) {*error = [Nserror errorwithdomain:@"Kencryptionerror"Code:ccstatus Userinfo:nil]; } dataout=Nil; }        returndataout;}//returns a Base64 string-encryption-(NSString *) Blowfishencodingwithkey: (NSString *) pkey{if(pkey.length<8|| Pkey.length> About) {NSLog (@"the length of the key value must be between [8,56]"); returnNil; } 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:KCCOPTIONPKCS7PA Dding|Kccoptionecbmode Iv:nil Error:&ERROR];//NSLog (@ "dataencrypted%@", dataencrypted);NSString*encryptedbase64string = [dataencrypted base64encodedstringwithoptions:0];//NSLog (@ "encryptedbase64string%@", encryptedbase64string);    returnencryptedbase64string; }//requires a Base64 string call, which returns the decryption result-decryption-(NSString *) Blowfishdecodingwithkey: (NSString *) pkey{if(pkey.length<8|| Pkey.length> About) {NSLog (@"the length of the key value must be between [8,56]"); returnNil; } 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);    returnstringdecrypted;}

Use only:

NSString * base64 = [@ "123456" blowfishencodingwithkey:@ "12345678" * Result = [base64 blowfishdecodingwithkey:@ "12345678"];   NSLog (@ " encrypted base64:%@    decryption results:%@", Base64,result);

Related references:

Pinterest (I tried this, the results of encryption and the results of the site is part of the difference, temporarily do not know why)

IOS Blowfish Encryption Decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.