以下這裡是官網給的範例,其實照著走就可以了..那我到底為啥花了這麼多時間 Orz
CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init];
BOOL success;
success = [crypt UnlockComponent: @"Anything for 30-day trial"];
if (success != YES) {
NSLog(@"%@",crypt.LastErrorText);
return;
}
以上這串一定要寫進code,不寫的話不給用,也別想在他的include資料夾找他的source code破解,沒加密才怪啊,不過網路上可能有破解版就是?
// Attention: use "blowfish2" for the algorithm name: crypt.CryptAlgorithm = @"blowfish2"; // CipherMode may be "ecb", "cbc", or "cfb" crypt.CipherMode = @"cbc"; // KeyLength (in bits) may be a number between 32 and 448. // 128-bits is usually sufficient. The KeyLength must be a // multiple of 8. crypt.KeyLength = [NSNumber numberWithInt:128]; // The padding scheme determines the contents of the bytes // that are added to pad the result to a multiple of the // encryption algorithm's block size. Blowfish has a block // size of 8 bytes, so encrypted output is always // a multiple of 8. crypt.PaddingScheme = [NSNumber numberWithInt:0]; // EncodingMode specifies the encoding of the output for // encryption, and the input for decryption. // It may be "hex", "url", "base64", or "quoted-printable". crypt.EncodingMode = @"hex"; // An initialization vector is required if using CBC or CFB modes. // ECB mode does not use an IV. // The length of the IV is equal to the algorithm's block size. // It is NOT equal to the length of the key. NSString *ivHex; ivHex = @"0001020304050607"; [crypt SetEncodedIV: ivHex encoding: @"hex"]; // The secret key must equal the size of the key. For // 256-bit encryption, the binary secret key is 32 bytes. // For 128-bit encryption, the binary secret key is 16 bytes. NSString *keyHex; keyHex = @"000102030405060708090A0B0C0D0E0F"; [crypt SetEncodedKey: keyHex encoding: @"hex"]; // Encrypt a string... // The input string is 44 ANSI characters (i.e. 44 bytes), so // the output should be 48 bytes (a multiple of 8). // Because the output is a hex string, it should // be 96 characters long (2 chars per byte). NSString *encStr; encStr = [crypt EncryptStringENC: @"The quick brown fox jumps over the lazy dog."]; NSLog(@"%@",encStr); // Now decrypt: NSString *decStr; decStr = [crypt DecryptStringENC: encStr]; NSLog(@"%@",decStr);
以上除了最後的加解密example可以不用管,其它照著放進去再修改就行了。
需要更改的地方可能有幾個:
1. EncodingMode =@"hex";
這邊看個人喜好喜歡用什麼加解密的input/output格式,例如使用hex,則data進去後,出來就是一串hex。
2. ivHex
為16進位的加密字串,由於blowfish有8 bytes的block size且IV的長度與block size相同,所以這邊改成自己的加密字串,並且只需要16個字即可。至於為啥是blowfish 是8 bytes,請google XD
3. keyHex
這邊的長度就是一開始設定的keylength長度,由於是128/8=16bytes,所以要用32個16進位的字去填滿。
4. crypt的方法SetEncodedKey: encoding:@"hex" 中的encoding:
這邊雖然範例中沒有提,但其實是可以改的,例如我在使用的時候就是把兩個encoding的@"hex"改為@"ascii"。我的情況是改成@"hex"會導致解碼錯誤。
自解:我這邊 PHP API放置的server預設編碼方式是utf-8,因此用ascii字元集編碼,才不會導致傳過去的資料,在API那一邊看會有亂碼產生,這也是為什麼我改成hex的話,會導致解碼錯誤。
最後使用設定好的crypt的EncryptStringENC以及DecryptStringENC去加解密就可以了。
下一回是最終回,使用在與PHP API資料加解密的傳輸。
補上chilkat對於iOS的blowfish reference documentation
http://www.chilkatsoft.com/refdoc/objcCkoCrypt2Doc.html
