Java - DES Encryption and Decryption example
Key Concepts
- Symmetric Encryption: DES (Data Encryption Standard) is a symmetric key encryption algorithm, meaning the same key is used for both encryption and decryption.
- DES Algorithm: DES uses a 56-bit key (plus 8 bits for parity) to encrypt blocks of data (64 bits each). It performs several complex transformations (16 rounds of encryption) to secure the data.
- Java Cryptography Architecture (JCA): The
javax.crypto
package provides the necessary tools for implementing encryption and decryption, including key generation, cipher operations, and transformations.
Here’s an example of using the DES (Data Encryption Standard) algorithm in Java for encryption and decryption. This example uses the javax.crypto
package, which provides a straightforward way to implement cryptographic algorithms.
Code Example
Explanation
Key Generation:
- The
KeyGenerator
is used to generate a DES key of 56 bits.
- The
Encryption:
- A
Cipher
instance is created for the "DES" transformation. - The cipher is initialized in encryption mode with the generated key.
- The input text is encrypted and encoded in Base64 for readability.
- A
Decryption:
- The cipher is initialized in decryption mode with the same key.
- The Base64-encoded encrypted text is decoded and decrypted back to the original text.
Sample Output
Notes
- Security: DES is considered insecure by modern standards due to its short key length. Use AES (Advanced Encryption Standard) for better security.
- Dependencies: This example uses built-in Java libraries, so no additional dependencies are required.