Today see a 3DES encryption algorithm code, with the parameters is desede/cbc/pkcs5padding, feel more unfamiliar, so learned a bit.
The Java code encountered is as follows:
Cipher cipher=cipher.getinstance ("desede/cbc/pkcs5padding");
Previously written code, given the parameters are des or desede. In fact, Desede is shorthand, and it is equivalent to desede/ecb/pkcs5padding. This parameter is divided into three segments.
-The first paragraph is the name of the cryptographic algorithm, such as Desede is actually 3-des. This section can also put other symmetric encryption algorithms, such as Blowfish.
-The second paragraph is the block encryption mode, in addition to CBC and ECB, it can be none/cfb/qfb and so on. The most common is CBC and ECB. Des uses packet encryption to encrypt plaintext in 8-byte (64-bit) groupings. If each group is handled independently, it is the ECB. The CBC processing method is to first use the initial Vector IV for the first set of encryption, and then use the first set of ciphertext as the key to the second group of encryption, and then complete the entire encryption operation. If the contents of two groupings in clear text are the same, the ECB will get exactly the same ciphertext, but CBC will not.
-the third paragraph refers to the Fill method of the last grouping. In most cases, clear text is not just a multiple of 64 bits. For the last group, if the length is less than 64 bits, the data needs to be populated to 64 bits. Pkcs5padding is a common method of filling, and if not specified, the default is it.
To add, although the valid key length of DES is 56 bits, the required key length is 64 bits (8 bytes). 3DES requires 24 bytes.
C#
Sometimes the contents of Java encryption in C # can not be decrypted, the general reason is that the parameters are set incorrectly. The TripleDES object acquired by C # must have the mode and padding two properties set. mode and padding are enumerated types. Mode has corresponding enumeration values for the ECB and CBC, but the padding pkcs5padding need to correspond to PKCS7.
Sample code
TripleDES d = tripledes.create ();
D.mode = CIPHERMODE.CBC;
d.padding = PADDINGMODE.PKCS7;
About the desede/cbc/pkcs5padding in DES encryption