The encryption algorithm used in the project, because there is a lot of trouble in the middle of adapting to the Android version.
MD5 algorithm and DES algorithm are common two kinds of encryption algorithms. MD5:MD5 is an irreversible encryption algorithm, according to my understanding, the so-called irreversible, is not able to decrypt, then it has what use, its usefulness, most of the login function will use this algorithm. This is followed by my project experience. DES: A block algorithm that uses key encryption, so when using it for encryption, you need a key, plus some settings and a piece of text that you need to encrypt. In iOS, using these two encryption algorithms is very simple, and the <CommonCrypto/CommonCrypto.h> Library of the system gives us the interface of the boundaries provided. In many mobile projects, the back-end services of the Android platform and the iOS platform are unified, such as the process of a login function: 1, the client request the key to the server, the parameters of the request is a mutually agreed on a MD5 encrypted string. We can use the following steps to encrypt the first step: -(NSString *) md5digest{//transcoding to be UTF8 const char* input = [self utf8string]; unsigned char result[cc_md5_digest_length]; CC_MD5 (input, (cc_long) strlen (input), result); nsmutablestring *digest = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2]; for (Nsinteger i = 0; i < cc_md5_digest_length; i++) {[DIGEST appendformat:@ "%02x", Result[i]]; } return digest;}
Through such a method, we can easily get a string of MD5 encryption string, but must and the background agreed, MD5 encryption of the number of bits is 16 or 32 bits, encrypted with the above method 32 bits, of course, there is a connection between them, through the following method can be converted to 16: + (NSString *) trransfrommd532tomd516: (NSString *) md532{NSString * string; for (int i=0; i<24; i++) {string=[md532 Substringwithrange:nsmakerange (8, 16)]; } return string;
It is also important to note that the case after the encryption should also correspond. 2, the service side will get the MD5 string and the agreed good MD5 string to compare, if consistent, can release, return key. 3, the client takes the key, the key is again MD5 encryption, and then through the DES will send data encryption to the server. This step is crucial, let's look at Des's encryption code first + (nsstring *) encryptusedes: (nsstring *) Cleartext key: (nsstring *) Key andiv: ( nsstring *) iv{ //This iv is a des encrypted initialization vector that can be used with the same MD5 character as the key nsdata * date = [iv datausingencoding:nsutf8stringencoding]; nsstring *ciphertext = nil; nsuinteger datalength = [ cleartext length]; nsdata *textdata = [cleartext Datausingencoding:nsutf8stringencoding]; unsigned char buffer[1024]; memset (buffer, 0, sizeof (char)); size_t numbytesencrypted = 0; cccryptorstatus cryptstatus = cccrypt (kccencrypt,//encryption Mode kCCDecrypt representative decryption kccalgorithmdes,//Encryption Method kccoptionpkcs7padding,//Filling Algorithm [key utf8string], //Key string kcckeysizedes,//number of encryption bits [date bytes],//initialization Vector [textData bytes] , dataLength, &Nbsp; buffer, 1024, &numbytesencrypted); if ( cryptstatus == kccsuccess) { nslog (@ "des encryption succeeded"); nsdata *data = [nsdata datawithbytes: Buffer length: (Nsuinteger) numbytesencrypted]; byte* bb = (byte*) [data bytes]; ciphertext = [Base64 parseByteArray2HexString:bb]; }else{ nslog (@ "des encryption failed"); } return ciphertext;} Some notes: (1) Encryption method, iOS is provided by the following types of enum {kCCAlgorithmAES128 = 0, Kccalgorithmaes = 0, Kccalgorithmdes, Kccalgorithm3des, KCCALGORITHMC AST, KCCAlgorithmRC4, kCCAlgorithmRC2, kccalgorithmblowfish};
(2) Filling algorithm Enum {/* options for block ciphers */kccoptionpkcs7padding = 0x0001, Kccoptionecbmode = 0x0002/* s Tream ciphers currently have no options */};
We can find that only two of the official offers, but Java is using Kccoptionpkcs7padding
But don't worry, when the key is 8 bits, the two filling algorithms encrypt the results to try the same. 4, the server through the same way, decrypt the ciphertext, pass with Android. |