1. Overview
In Internet-based applications, the sender encrypts and transmits the characters in a certain way, while the receiver decrypts the characters based on the agreed key. In this way, even if the characters transmitted are intercepted, it will not be easily identified. In addition, many application environments are complex. The server is a Java application, and the client has Java applications and smart phone applications. We take the server as a Java application and the client as the smartphone IOS application as an example to encrypt a character on the server and transmit it to the client for decryption. On the client, we encrypt another character and transmit it to the server for decryption, such a complicated process.
There are many implementation methods for this requirement, such as using HTTPS and secure digital certificates, which are widely used in the financial industry.
Des is used here.AlgorithmTo ensure secure character transmission. First of all, I do not know much about the DES algorithm. The following discussion must have some shortcomings, such as omissions and errors. Please read them for reference and tell me if errors are found. Thank you first.
2. Java character encoding
All computer characters are encoded according to a certain character set, and truly transmitted in the network is bytes.
Send a string of characters such as "Miki westward journey" on the client:Mikixiyou@126.comIt will be encoded according to the character set of the client to form a byte stream. Before being transmitted to a server, it also needs to be encoded using base64 and then transmitted to the server.
After receiving the message, the server uses base64 to decode it and then decodes it based on its default character set to form a string. If the default character set used by the client and the server is the same, for example, GBK, the text of this character is correctly displayed. If it is incorrect, if the client uses GBK encoding and the server uses utf8 encoding, garbled characters will occur. We often see garbled characters such as question marks in the browser, which is caused by inconsistent character sets.
Public static void main (string ARGs []) throws exception {
String source = "Miki westward journey | mikixiyou@126.com ";
Stringcharsetname = system. getproperty ("file. encoding ");
System. Out. println ("file. encodingis" + charsetname );
System. Out. println ("Source =" + source );
System. Out. println (parsebyte2hexstr (source. getbytes ("GBK ")));
System. Out. println (parsebyte2hexstr (source. getbytes ()));
System. Out. println (parsebyte2hexstr (source. getbytes ("UTF-8 ")));
String source_utf8 = newstring (source. getbytes ("UTF-8"), "UTF-8 ");
System. Out. println ("source_utf8 =" + source_utf8 );
String source_gbk = newstring (source. getbytes ("GBK"), "GBK ");
System. Out. println ("source_gbk =" + source_gbk );
}
Generally, we use this method to encode the default Character Set of the source string of source. getbytes.
String source = "westweb abc@126.com ";
System. Out. println (parsebyte2hexstr (source. getbytes ()));
The output result is
Cef7d3ce616263403132362e636f6d
First two bytesCef7Indicates "West", the last two bytesD3ceIndicates "game ". The GBK character set uses two-byte encoding for Chinese characters.
The default Character Set of the string source can be obtained through the system attribute, which is the file encoding of every Java. The method is as follows:
Stringcharsetname = system. getproperty ("file. encoding ");
System. Out. println ("file. encodingis" + charsetname );
The output result is
File. encoding is GBK
If encoding is obtained by the UTF-8 character set, the output byte stream is output by UTF-8 encoding.
System. Out. println (parsebyte2hexstr (source. getbytes ("UTF-8 ")));
String source_utf8 = newstring (source. getbytes ("UTF-8"), "UTF-8 ");
The output result is
E8a5bfe6b8b8616263403132362e636f6d
The first three bytes e8a5bf indicate "West", and the last three bytes e6b8b8 indicate "game ". The UTF-8 Character Set uses three-byte encoding for Chinese characters.
In the Internet, the transmitted byte stream also needs to be base64-encoded. I don't know if it is because the byte stream is too long or requires base64 encoding for compression or other purposes.
Base64 is easy to use.Source codeA lot. These two methods are basically used. "string encode (byte [] data)" encodes the byte array into a string, "byte [] Decode (string S) "To restore the string to a byte array.
3. java byte Encryption
Import the javax. crypto. cipher; package in the Java class and use cipher. getinstance ("des/CBC/pkcs5padding.
Note that the pkcs5padding algorithm is used here. The key can only be 8 bytes.
In iOS, the supported DES encryption algorithm is kccoptionpkcs7padding | kccoptionecbmode. When pkcs7padding is used, its key can be 8 bytes or not. If the key is not 8 bytes, The pkcs5padding algorithm on the Java end cannot be decrypted.
I know little about the DES algorithm. Here I just want to explain my understanding. When the key is 8 bytes, encryption and decryption of pkcs7padding and pkcs5padding are common. Therefore, you don't have to worry about how to use different algorithms. How can IOS support Java encryption algorithms, or even Des.
I don't think it is necessary. What we do is engineering, a method of implementation of requirements. As long as you comply with the eight-byte key conventions, you can achieve your needs, and you need to find other algorithms. Well, I understand that you think this is not safe, but it is not absolutely safe.
Back to the question, the implementation of DES encryption in Java is as follows:
Private Static byte [] IV = {1, 2, 3, 4, 5, 6, 7, 8 };
Public static byte [] encryptdes (string encryptstring, string encryptkey)
Throws exception {
System. Out. println ("willencrypteddata with UTF-8 encoding =" + parsebyte2hexstr (encryptstring. getbytes ("UTF-8 ")));
Ivparameterspec zeroiv = new ivparameterspec (IV );
Secretkeyspec key = newsecretkeyspec (encryptkey. getbytes (), "des ");
Cipher cipher = cipher. getinstance ("des/CBC/pkcs5padding ");
Cipher. INIT (Cipher. encrypt_mode, key, zeroiv );
Byte [] encrypteddata = cipher. dofinal (encryptstring. getbytes ("UTF-8 "));
System. Out. println ("didencrypteddata =" + parsebyte2hexstr (encrypteddata ));
Return encrypteddata;
}
Public static stringencryptdeswithbase64 (string encryptstring, string encryptkey) throws exception
{
Return xybase64.encode (encryptdes (encryptstring, encryptkey ));
}
The implementation of des decryption in Java is as follows:
Public static string decryptdes (byte [] encrypteddata, stringdecryptkey)
Throws exception {
System. Out. println ("willdecrypteddata =" + parsebyte2hexstr (encrypteddata ));
Ivparameterspec zeroiv = new ivparameterspec (IV );
Secretkeyspec key = newsecretkeyspec (decryptkey. getbytes ("UTF-8"), "des ");
Cipher cipher = cipher. getinstance ("des/CBC/pkcs5padding ");
Cipher. INIT (Cipher. decrypt_mode, key, zeroiv );
Byte decrypteddata [] = cipher. dofinal (encrypteddata );
System. Out. println ("diddecrypteddata with UTF-8 encoding =" + parsebyte2hexstr (decrypteddata ));
String decryptedstring = new string (decrypteddata, "UTF-8 ");
System. Out. println ("diddecryptedstring with UTF-8 encoding =" + decryptedstring );
Return decryptedstring;
}
Public static stringdecryptdeswithbase64 (string encryptedstring, string decryptkey) throws exception
{
Byte [] encrypteddata = xybase64.decode (encryptedstring );
Return decryptdes (encrypteddata, decryptkey );
}
Debug in main () and the result is as expected.
Public static void main (string [] ARGs) throws exception {
String plaintext = "abcdefghihjjjkelaemn ";
String keytext = "20120401 ";
Plaintext = "Miki westbound | mikixiyou@126.com ";
Keytext = "abcd1234 ";
Byte [] encrypteddata = encryptdes (plaintext, keytext );
String decryptedstring = decryptdes (encrypteddata, keytext );
String ciphertext = parsebyte2hexstr (encrypteddata );
System. Out. println ("plaintext:" + plaintext );
System. Out. println ("key:" + keytext );
System. Out. println ("ciphertext base64 encoding:" + ciphertext );
System. Out. println ("decrypted:" + decryptedstring );
String encryptedstring = encryptdeswithbase64 (plaintext, keytext );
Decryptedstring = decryptdeswithbase64 (encryptedstring, keytext );
System. Out. println ("plaintext:" + plaintext );
System. Out. println ("key:" + keytext );
System. Out. println ("ciphertext:" + encryptedstring );
System. Out. println ("decrypted:" + decryptedstring );
}
The output result is as follows:
Will encrypteddata with UTF-8 encoding = e8a5bfe6b8b8616263403132362e636f6d
Did encrypteddata = a69c602b3f74bd6273de730d6214026b8fe538e4ab9f8547
Will decrypteddata = a69c602b3f74bd6273de730d6214026b8fe538e4ab9f8547
Did decrypteddata with UTF-8 encoding = e8a5bfe6b8b8616263403132362e636f6d
Did decryptedstring with UTF-8 encoding = Miki westward journey | mikixiyou@126.com
Plain text: Miki westward journey | mikixiyou@126.com
Key: abcd1234
Ciphertext base64 encoding: a69c602b3f74bd6273de730d6214026b8fe538e4ab9f8547
Decrypted: Miki westward journey | mikixiyou@126.com
Will encrypteddata with UTF-8 encoding = e8a5bfe6b8b8616263403132362e636f6d
Did encrypteddata = a69c602b3f74bd6273de730d6214026b8fe538e4ab9f8547
Will decrypteddata = a69c602b3f74bd6273de730d6214026b8fe538e4ab9f8547
Did decrypteddata with UTF-8 encoding = e8a5bfe6b8b8616263403132362e636f6d
Did decryptedstring with UTF-8 encoding = Miki westward journey | mikixiyou@126.com
Plain text: Miki westward journey | mikixiyou@126.com
Key: abcd1234
Ciphertext: ppxgkz90vwjz3nmnyhqca4/loosrn4vh
Decrypted: Miki westward journey | mikixiyou@126.com
In this way, we implement encryption and decryption in Java. However, such an encryption and decryption algorithm must be implemented on the client to complete the task.
4. Objective-C character encoding
To be continued
5. Objective-C byte Encryption
To be continued
6. References
Http://www.cnblogs.com/midea0978/articles/1437257.html
Http://www.cnblogs.com/silentjesse/archive/2011/11/04/2235674.html
Objective-C and DES encryption in Java are consistent. I can't find the original author in this document. I only saw a lot of reposts, so I didn't write hyperlinks.