Java commonly used encryption and decryption method details
- 2020-06-07 04:35:53
- OfStack
Security has become an increasingly important issue. How to encrypt and decrypt important data in Java is the main content of this paper.
1. Common encryption/decryption algorithms
1.Base64
Strictly speaking, Base64 is not an encryption/decryption algorithm, but a coding method. Base64 does not generate a key. The ciphertext encoded by Base64 can be directly "translated" into plaintext, but the encryption effect can be achieved by adding confounding characters to the text.
2.DES
DES is a symmetric algorithm based on a 56-bit key. It was established as the Federal Data processing Standard (FIPS) by the National Bureau of Standards of the US federal government in 1976, and then widely spread internationally. DES is no longer a secure encryption algorithm and has been publicly cracked. DES has been replaced by the Advanced Encryption Standard (AES).
3.3DES
3DES is a derivative algorithm of DES, which mainly improves the security of DES 1.
4.AES
AES is now one of the most popular symmetric encryption algorithms.
2. Implement 1 of the libraries required
In order to achieve the above algorithm, we can "learn" practical JDK implementation, also can use 1 some open source of third party libraries, such as Bouncy Castle (https: / / www. bouncycastle. org /) and comnons codec (https: / / commons. apache. org/proper/commons - codec /).
3. Concrete implementation
1.Base64
package com.tancky.security;
import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Demo {
private static String src = "TestBase64";
public static void main(String[] args) {
Base64Demo.jdkBase64();
Base64Demo.commonsCodecBase64 ();
Base64Demo.bouncyCastleBase64 ();
}
// use JDK the base64 The implementation,
public static void jdkBase64 (){
BASE64Encoder encoder = new BASE64Encoder();
String encode = encoder.encode(Base64Demo.src.getBytes());
System.out.println("encode: " + encode);
BASE64Decoder decoder = new BASE64Decoder();
try {
String decode = new String ( decoder.decodeBuffer(encode));
System.out.println("decode: " + decode);
} catch (IOException e) {
e.printStackTrace();
}
}
// use apache the commonsCodec implementation
public static void commonsCodecBase64 (){
byte[] encodeBytes = org.apache.commons.codec.binary.Base64.encodeBase64(Base64Demo.src.getBytes());
String encode = new String (encodeBytes);
System.out.println("encode: " + encode);
byte[] decodeBytes = org.apache.commons.codec.binary.Base64.decodeBase64(encode);
String decode = new String(decodeBytes);
System.out.println("decode: " + decode);
}
// use bouncyCastlede implementation
public static void bouncyCastleBase64 () {
byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(Base64Demo.src.getBytes()) ;
String encode = new String (encodeBytes);
System.out.println("encode: " + encode);
byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encode);
String decode = new String(decodeBytes);
System.out.println("decode: " + decode);
}
}
2.DES
package com.tancky.security;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
public class DESDemo {
private static String src = "TestDES";
public static void jdkDES () {
try {
// Generate the key Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//KEY conversion
DESKeySpec deSedeKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
// encryption
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] encodeResult = cipher.doFinal(DESDemo.src.getBytes());
System.out.println("DESEncode :" + Hex.toHexString(encodeResult));
// decryption
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println("DESDncode :" + new String (DecodeResult));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
}
}
public static void bcDES (){
try {
// use BouncyCastle the DES encryption
Security.addProvider(new BouncyCastleProvider());
// Generate the key Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES","BC");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//KEY conversion
DESKeySpec deSedeKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
// encryption
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] encodeResult = cipher.doFinal(DESDemo.src.getBytes());
System.out.println("DESEncode :" + Hex.toHexString(encodeResult));
// decryption
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println("DESDncode :" + new String (DecodeResult));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Autogenerated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
DESDemo.jdkDES ();
DESDemo.bcDES();
}
}
3.3DES
package com.tancky.security;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
public class TripleDESDemo {
private static String src = "TestTripleDES";
public static void jdkTripleDES () {
try {
// Generate the key Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
keyGenerator.init(168);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//KEY conversion
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
// encryption
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] encodeResult = cipher.doFinal(TripleDESDemo.src.getBytes());
System.out.println("TripleDESEncode :" + Hex.toHexString(encodeResult));
// decryption
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println("TripleDESDncode :" + new String (DecodeResult));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
}
}
public static void bcTripleDES () {
try {
Security.addProvider(new BouncyCastleProvider());
// Generate the key Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede","BC");
keyGenerator.getProvider();
keyGenerator.init(168);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//KEY conversion
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
// encryption
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] encodeResult = cipher.doFinal(TripleDESDemo.src.getBytes());
System.out.println("TripleDESEncode :" + Hex.toHexString(encodeResult));
// decryption
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println("TripleDESDncode :" + new String (DecodeResult));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Autogenerated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
jdkTripleDES ();
bcTripleDES ();
}
}
4.AES
package com.tancky.security;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
public class AESDemo {
private static String src = "TestAES";
public static void jdkAES (){
try {
// generate Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
//keyGenerator.init(128, new SecureRandom("seedseedseed".getBytes()));
// The above initialization method can be used to generate the key from a specific seed, so that the encrypted ciphertext is unique 1 Fixed.
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyBytes = secretKey.getEncoded();
//Key conversion
Key key = new SecretKeySpec(keyBytes, "AES");
// encryption
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encodeResult = cipher.doFinal(AESDemo.src.getBytes());
System.out.println("AESencode : " + Hex.toHexString(encodeResult) );
// decryption
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodeResult = cipher.doFinal(encodeResult);
System.out.println("AESdecode : " + new String (decodeResult));
} catch (NoSuchAlgorithmException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
}
}
public static void bcAES (){
try {
// use BouncyCastle the DES encryption
Security.addProvider(new BouncyCastleProvider());
// generate Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES","BC");
keyGenerator.getProvider();
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyBytes = secretKey.getEncoded();
//Key conversion
Key key = new SecretKeySpec(keyBytes, "AES");
// encryption
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encodeResult = cipher.doFinal(AESDemo.src.getBytes());
System.out.println("AESencode : " + Hex.toHexString(encodeResult) );
// decryption
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodeResult = cipher.doFinal(encodeResult);
System.out.println("AESdecode : " + new String (decodeResult));
} catch (NoSuchAlgorithmException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Autogenerated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Autogenerated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
jdkAES();
bcAES();
}
}