There are two sentences that say this:
1) Algorithms and data structures are an important part of programming. If you lose the algorithms and data structures, you will lose everything.
2) Programming is algorithms and data structures, and algorithms and data structures are the soul of programming.
Note that this is not what I said, it was summarized by countless programmers. It is very realistic and insightful. If you want to develop continuously for a long time, it is still necessary to study more algorithms. Today I will tell you about the symmetric encryption algorithms in encryption algorithms, and here I will teach you the programming and use of symmetric encryption algorithms. It includes three symmetric encryption algorithms: DES, 3DES and AES, and is full of practical information.
1. Symmetric password algorithm
Symmetric cryptography algorithm is the most widely used and most frequently used encryption algorithm today. It is not only used in the software industry, but also popular in the hardware industry. All kinds of infrastructures will give priority to symmetric encryption algorithms whenever they involve security needs.
The encryption key and decryption key of the symmetric cryptography algorithm are the same. For most symmetric cryptography algorithms, the encryption and decryption process is inversely related.
(1) Encrypt and decrypt communication model
(2) Features: open algorithm, small calculation volume, fast encryption speed, high encryption efficiency
(3) Weakness: Both parties use the same key, and security is not guaranteed
There are two types of symmetric passwords: stream password and packet password, but now the block password is commonly used:
(4) Block password working mode
1) ECB: Electronic cipherbook (the most commonly used, each encryption generates independent ciphertext packets and will not affect other ciphertext packets, that is, the same plaintext is generated after encryption)
2) CBC: ciphertext link (commonly used. Before plaintext encryption, you need to perform XOR operation with the previous ciphertext, that is, different ciphertexts are generated after the same plaintext encryption)
In addition to these two commonly used working modes, there are:
3) CFB: Password feedback
4) OFB: Output feedback
5) CTR: Counter
These five working modes are mainly applied by algorithms in cryptography when performing derivation calculations.
6. How to fill block passwords
1) NoPadding: No fill
2) PKCS5Padding:
3) ISO10126Padding:
7. Commonly used symmetric passwords:
1) DES (Data Encryption Standard, 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 attack algorithms against DES)
Let’s first take a 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 |
Let’s see how to use three algorithms of DES/3DES/AES to implement symmetric encryption:
2. DES algorithm
1.DES: Data encryption standard, a typical algorithm in the field of symmetric encryption algorithms
2. Features: The key is short (56 bits), short life cycle (avoiding cracking)
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 key byte[] key = secretKey.getEncoded();//Key byte array2) Encryption
SecretKey secretKey = new SecretKeySpec(key, "DES");//Recover key Cipher cipher = Cipher.getInstance("DES");//Cipher completes encryption or decrypts working class cipher.init(Cipher.ENCRYPT_MODE, secretKey);//Initializes Cipher, encryption mode byte[] cipherByte = cipher.doFinal(data);//Encrypts data3) Decryption
SecretKey secretKey = new SecretKeySpec(key, "DES");//Recover key Cipher cipher = Cipher.getInstance("DES");//Cipher completes encryption or decrypts working class cipher.init(Cipher.DECRYPT_MODE, secretKey);//Initializes Cipher, decrypts mode byte[] cipherByte = cipher.doFinal(data);//Decrypts dataWe can find that we just set different modes for encryption and decryption.
3.3DES algorithm
1.3DES: Increase the key length to 112 or 168 bits, and improve security by increasing the number of iterations.
2. Disadvantages: slow processing speed, long key calculation time, and low encryption efficiency
3. Java implementation
1) Generate a key
KeyGenerator keyGen = KeyGenerator.getInstance("DESede");//Key generator keyGen.init(168); //The key length can be specified as 112 or 168, and the default is 168. SecretKey secretKey = keyGen.generateKey();//Generate key byte[] key = secretKey.getEncoded();//Key byte array2) 3DES encryption
SecretKey secretKey = new SecretKeySpec(key, "DESede");//Recover key Cipher cipher = Cipher.getInstance("DESede");//Cipher completes encryption or decryption of working class cipher.init(Cipher.ENCRYPT_MODE, secretKey);//Initializes Cipher, decrypts mode byte[] cipherByte = cipher.doFinal(data);//Encrypts data3) 3DES decryption
SecretKey secretKey = new SecretKeySpec(key, "DESede");//Recover key Cipher cipher = Cipher.getInstance("DESede");//Cipher completes encryption or decryption of working class cipher.init(Cipher.DECRYPT_MODE, secretKey);//Initializes Cipher, decrypts mode byte[] cipherByte = cipher.doFinal(data);//Decrypts data4. AES algorithm (recommended)
1.AES: Advanced data encryption standard, able to effectively resist all known attacks against DES algorithms
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, after obtaining no policy permissions, it can be 192 or 256SecretKey secretKey = keyGen.generateKey();//Generate key byte[] key = secretKey.getEncoded();//Key byte array2) AES encryption
SecretKey secretKey = new SecretKeySpec(key, "AES");//Recovery key Cipher cipher = Cipher.getInstance("AES");//Cipher completes encryption or decryption of working class cipher.init(Cipher.ENCRYPT_MODE, secretKey);//Initializes Cipher, decrypts mode byte[] cipherByte = cipher.doFinal(data);//Encrypts data3) AES decryption
SecretKey secretKey = new SecretKeySpec(key, "AES");//Recover key Cipher cipher = Cipher.getInstance("AES");//Cipher completes encryption or decryption of working class cipher.init(Cipher.DECRYPT_MODE, secretKey);//Initializes Cipher, decrypts mode byte[] cipherByte = cipher.doFinal(data);//Decrypts dataFor convenience of use, I have written tool classes for DES/3DES/AES algorithms, address: download address (new DES/3DES/AES tool class).
At this point, the three algorithms of DES/3DES/AES are all done to implement symmetric encryption. I hope it will be helpful to everyone's learning, and I hope everyone will support Wulin.com more.