Java uses des/3des/aes these three kinds of algorithms to implement symmetric encryption respectively

Source: Internet
Author: User
Tags decrypt generator



Reprint Please specify source: http://blog.csdn.net/smartbetter/article/details/54017759



There are two words to say:
1) algorithm and data structure is an important part of programming, if you lose the algorithm and data structure, you have lost everything.
2) programming is the algorithm and data structure, algorithm and data structure is the soul of programming.
Note that this is not what I said, is a number of programmers summed up, the words are really very incisive, if you want to long-term sustainable development, more research algorithms are still very necessary, today I give you to say the encryption algorithm in the symmetric encryption algorithm, and here will teach you to use the symmetric encryption algorithm programming. Contains Des, 3DES, and AES three symmetric encryption algorithms for programming use, dry goods full.


1. Symmetric cipher algorithm


Symmetric cipher algorithm is the most widely used and most frequent encryption algorithm in the world. It is not only used in the software industry, it is also popular in the hardware industry. The symmetric encryption algorithm is a priority in every infrastructure that involves security requirements.



The symmetric cipher algorithm has the same cryptographic key and decryption key, and the encryption and decryption process is mutually inverse for most symmetric cipher algorithms.



(1) Encryption and decryption communication model






(2) Features: algorithm disclosure, low computational capacity, fast encryption speed, high encryption efficiency



(3) Weaknesses: Both parties use the same key, security is not guaranteed



Symmetric passwords have both stream and block ciphers, but they are now commonly used in block ciphers:



(4) block cipher working mode

1) ECB: electronic codebook (the most commonly used, each encryption generates an independent ciphertext group, and will not affect other ciphertext groups, that is, the same ciphertext is generated after the same plaintext is encrypted)
2) CBC: ciphertext link (commonly, plaintext needs to be XORed with the previous ciphertext before encryption, that is, the same plaintext is encrypted to generate different ciphertexts)

In addition to these two common working modes, there are:

3) CFB: ciphertext feedback
4) OFB: output feedback
5) CTR: Counter
These five working modes are mainly applied by algorithms in cryptography when performing inference.

6. Block password filling method

1) NoPadding: No padding
2) PKCS5Padding:
3) ISO10126Padding:
7. Commonly used symmetric passwords:

1) DES (Data Encryption Standard)
2) 3DES (Triple DES, DESede, triple DES encryption algorithm)
3) AES (Advanced Encryption Standard, Advanced Data Encryption Standard, AES algorithm can effectively resist DES attack algorithms)
First look at a simple comparison of these three algorithms:

Algorithm Key length Default key length Working mode Filling method
DES 56 56 ECB, CBC, PCBC, CTR, CTS, CFB, CFB8-CFB128, OFB, OFB8-OFB128 NoPadding, PKCS5Padding, ISO10126Padding
3DES 112, 168 168 ECB, CBC, PCBC, CTR, CTS, CFB, CFB8-CFB128, OFB, OFB8-OFB128 NoPadding, PKCS5Padding, ISO10126Padding
AES 128, 192, 256 128 ECB, CBC, PCBC, CTR, CTS, CFB, CFB8-CFB128, OFB, OFB8-OFB128 NoPadding, PKCS5Padding, ISO10126Padding
Below we look at how to implement symmetric encryption using three algorithms: DES / 3DES / AES:

2.DES algorithm
1.DES: data encryption standard, is a typical algorithm in the field of symmetric encryption algorithms
2. Features: short key (56 bits), short life cycle (to avoid being cracked)
3.Java implementation

1) Generate a key

KeyGenerator keyGen = KeyGenerator.getInstance ("DES"); // Key generator
keyGen.init (56); // Initialize the key generator
SecretKey secretKey = keyGen.generateKey (); // Generate the key
byte [] key = secretKey.getEncoded (); // Key byte array
2) Encryption

SecretKey secretKey = new SecretKeySpec (key, "DES"); // Recovery key
Cipher cipher = Cipher.getInstance ("DES"); // Cipher completes the encryption or decryption work class
cipher.init (Cipher.ENCRYPT_MODE, secretKey); // Cipher initialization, encryption mode
byte [] cipherByte = cipher.doFinal (data); // encrypted data
3) Decrypt

SecretKey secretKey = new SecretKeySpec (key, "DES"); // Recovery key
Cipher cipher = Cipher.getInstance ("DES"); // Cipher completes the encryption or decryption work class
cipher.init (Cipher.DECRYPT_MODE, secretKey); // Cipher initialization, decryption mode
byte [] cipherByte = cipher.doFinal (data); // Decrypt data
We can see that we just set different modes for encryption and decryption.

3.3DES algorithm
1.3DES: increase the key length to 112 or 168 bits, increase security by increasing the number of iterations
2. Disadvantages: slower processing speed, longer key calculation time, and low encryption efficiency
3.Java implementation

1) Generate a key

KeyGenerator keyGen = KeyGenerator.getInstance ("DESede"); // Key generator
keyGen.init (168); // Can specify the key length as 112 or 168, the default is 168
SecretKey secretKey = keyGen.generateKey (); // Generate the key
byte [] key = secretKey.getEncoded (); // Key byte array
2) 3DES encryption

SecretKey secretKey = new SecretKeySpec (key, "DESede"); // Recovery key
Cipher cipher = Cipher.getInstance ("DESede"); // Cipher completes the encryption or decryption work class
cipher.init (Cipher.ENCRYPT_MODE, secretKey); // Cipher initialization, decryption mode
byte [] cipherByte = cipher.doFinal (data); // encrypted data
3) 3DES decryption

SecretKey secretKey = new SecretKeySpec (key, "DESede"); // Recovery key
Cipher cipher = Cipher.getInstance ("DESede"); // Cipher completes the encryption or decryption work class
cipher.init (Cipher.DECRYPT_MODE, secretKey); // Cipher initialization, decryption mode
byte [] cipherByte = cipher.doFinal (data); // Decrypt data
4.AES algorithm (recommended)
1.AES: Advanced data encryption standard, can effectively resist all known attacks against the DES algorithm
2. Features: short key establishment time, good sensitivity, low memory requirements, and high security
3.Java implementation

1) Generate a key

KeyGenerator keyGen = KeyGenerator.getInstance ("AES"); // Key generator
keygen.init (128); // default 128, can be 192 or 256 after no policy permission
SecretKey secretKey = keyGen.generateKey (); // Generate the key
byte [] key = secretKey.getEncoded (); // Key byte array
2) AES encryption

SecretKey secretKey = new SecretKeySpec (key, "AES"); // Recovery key
Cipher cipher = Cipher.getInstance ("AES"); // Cipher completes the encryption or decryption work class
cipher.init (Cipher.ENCRYPT_MODE, secretKey); // Cipher initialization, decryption mode
byte [] cipherByte = cipher.doFinal (data); // encrypted data
3) AES decryption

SecretKey secretKey = new SecretKeySpec (key, "AES"); // Recovery key
Cipher cipher = Cipher.getInstance ("AES"); // Cipher completes the encryption or decryption work class
cipher.init (Cipher.DECRYPT_MODE, secretKey); // Cipher initialization, decryption mode
byte [] cipherByte = cipher.doFinal (data); // Decrypt data
In order to facilitate the use, I have written tools for the three algorithms DES / 3DES / AES at the address: https://github.com/smartbetter/Android-UtilsLibrary (new DES / 3DES / AES tools).

At this point, the three algorithms of DES / 3DES / AES implement symmetric encryption. That's it, I like to remember "top"!

Java uses DES / 3DES / AES to implement symmetric encryption

Related Article

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.