Home > Java > javaTutorial > body text

Implementation of three symmetric encryption algorithms DES/3DES/AES in Java

巴扎黑
Release: 2017-04-30 10:07:28
Original
1456 people have browsed it

There are two sentences that say this:

1) Algorithms and data structures are an important part of programming. If you lose algorithms and data structures, you will lose everything.

2) Programming is algorithms and data structures. Algorithms and data structures are the soul of programming.

Note that this is not what I said. It was summed up by countless programmers. What I said is very practical and insightful. If you want long-term sustainable development, it is still necessary to study more algorithms. Today I will tell you about encryption algorithms. Symmetric encryption algorithm, and here we will teach you the programming and use of symmetric encryption algorithm. It includes the programming and use of three symmetric encryption algorithms: DES, 3DES and AES, and is full of useful information.

1.Symmetric cryptographic algorithm

Symmetric cryptographic algorithms are the most widely used and most frequently used encryption algorithms today. It is not only used in the software industry, but is also popular in the hardware industry. Whenever security requirements are involved in various infrastructures, symmetric encryption algorithms will be given priority.

The encryption key and decryption key of symmetric cryptographic algorithms are the same. For most symmetric cryptographic algorithms, the encryption and decryption processes are reversed.

(1) Encryption and decryption communication model

Implementation of three symmetric encryption algorithms DES/3DES/AES in Java

(2) Features: open algorithm, small amount of calculation, fast encryption speed, high encryption efficiency

(3) Weakness: Both parties use the same key, and security cannot be guaranteed

Symmetric ciphers include stream ciphers and block ciphers, but block ciphers are commonly used now:

(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 plaintext will produce the same ciphertext after encryption)


  • 2) CBC: Ciphertext link (commonly used, before plaintext encryption needs to be XORed with the previous ciphertext, that is, the same plaintext will produce different ciphertext after encryption)


  • 3) CFB: ciphertext feedback


  • 4) OFB: Output feedback


  • 5) CTR: Counter


These five working modes are mainly used by algorithms in cryptography when performing derivation calculations.

6. Block cipher filling method

  • 1) NoPadding: No padding


  • 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 the attack algorithm against DES)


Let’s first look at a simple comparison of these three algorithms:

algorithm Key length Default key length Operating 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 the three algorithms 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: short key (56 bits), short life cycle (to avoid being cracked)

3.Java implementation

1) Generate key

KeyGenerator keyGen = KeyGenerator.getInstance("DES");//密钥生成器
keyGen.init(56);//初始化密钥生成器
SecretKey secretKey = keyGen.generateKey();//生成密钥
byte[] key = secretKey.getEncoded();//密钥字节数组
Copy after login

2) Encryption

SecretKey secretKey = new SecretKeySpec(key, "DES");//恢复密钥
Cipher cipher = Cipher.getInstance("DES");//Cipher完成加密或解密工作类
cipher.init(Cipher.ENCRYPT_MODE, secretKey);//对Cipher初始化,加密模式
byte[] cipherByte = cipher.doFinal(data);//加密data
Copy after login

3) Decryption

SecretKey secretKey = new SecretKeySpec(key, "DES");//恢复密钥
Cipher cipher = Cipher.getInstance("DES");//Cipher完成加密或解密工作类
cipher.init(Cipher.DECRYPT_MODE, secretKey);//对Cipher初始化,解密模式
byte[] cipherByte = cipher.doFinal(data);//解密data
Copy after login

We 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 to improve security by increasing the number of iterations

2. Disadvantages: slow processing speed, long key calculation time, low encryption efficiency

3.Java implementation

1) Generate key

KeyGenerator keyGen = KeyGenerator.getInstance("DESede");//密钥生成器
keyGen.init(168);  //可指定密钥长度为112或168,默认为168   
SecretKey secretKey = keyGen.generateKey();//生成密钥
byte[] key = secretKey.getEncoded();//密钥字节数组
Copy after login

2) 3DES encryption

SecretKey secretKey = new SecretKeySpec(key, "DESede");//恢复密钥
Cipher cipher = Cipher.getInstance("DESede");//Cipher完成加密或解密工作类
cipher.init(Cipher.ENCRYPT_MODE, secretKey);//对Cipher初始化,解密模式
byte[] cipherByte = cipher.doFinal(data);//加密data
Copy after login

3) 3DES decryption

SecretKey secretKey = new SecretKeySpec(key, "DESede");//恢复密钥
Cipher cipher = Cipher.getInstance("DESede");//Cipher完成加密或解密工作类
cipher.init(Cipher.DECRYPT_MODE, secretKey);//对Cipher初始化,解密模式
byte[] cipherByte = cipher.doFinal(data);//解密data
Copy after login

4.AES algorithm (recommended)

1.AES: Advanced Data Encryption Standard, which 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 key

KeyGenerator keyGen = KeyGenerator.getInstance("AES");//密钥生成器
keygen.init(128);  //默认128,获得无政策权限后可为192或256
SecretKey secretKey = keyGen.generateKey();//生成密钥
byte[] key = secretKey.getEncoded();//密钥字节数组
Copy after login

2) AES encryption

SecretKey secretKey = new SecretKeySpec(key, "AES");//恢复密钥
Cipher cipher = Cipher.getInstance("AES");//Cipher完成加密或解密工作类
cipher.init(Cipher.ENCRYPT_MODE, secretKey);//对Cipher初始化,解密模式
byte[] cipherByte = cipher.doFinal(data);//加密data
Copy after login

3) AES decryption

SecretKey secretKey = new SecretKeySpec(key, "AES");//恢复密钥
Cipher cipher = Cipher.getInstance("AES");//Cipher完成加密或解密工作类
cipher.init(Cipher.DECRYPT_MODE, secretKey);//对Cipher初始化,解密模式
byte[] cipherByte = cipher.doFinal(data);//解密data
Copy after login

For ease of use, I wrote tool classes for the three algorithms DES/3DES/AES, address: https://github.com/smartbetter/Android-UtilsLibrary (new DES/3DES/AES tool class).

The above is the detailed content of Implementation of three symmetric encryption algorithms DES/3DES/AES in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!