English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Security issues have become an increasingly important issue, and how to encrypt and decrypt important data in Java is the main content of this article.
1. Common Encryption/decryption algorithm
1.Base;64
Strictly speaking, Base64is not an encryption/decryption algorithm, but it is a coding method. Base64No key is generated, using Base64The encoded ciphertext can be directly 'translated' into plaintext, but the effect of encryption can be achieved by adding confusion characters to the plaintext.
2.DES
DES is based on56bit key symmetric algorithm,1976year it was designated as the Federal Information Processing Standard (FIPS) by the National Bureau of Standards of the United States government, and then it spread widely internationally. Now DES is no longer a secure encryption algorithm, having been publicly cracked, and has been replaced by the Advanced Encryption Standard (AES).
3.3DES
3DES is a derived algorithm of DES, mainly improving the security requirements of DES.
4.AES
AES is one of the most popular symmetric encryption algorithms now.
2. Required Libraries for Implementation
To implement the above algorithms, we can use the JDK's built-in implementation, or we can use some open-source third-party libraries, such as Bouncy Castle (https://www.bouncycastle.org/) and Commons Codec (https://commons.apache.org/proper/commons-codec/).
3. Specific Implementation
1.Base;64
package com.tancky.security; import java.io.IOException; import sun.misc.BASE;64Decoder; import sun.misc.BASE;64Encoder; public class Base64Demo { private static String src = "TestBase64"; public static void main(String[] args) { Base64Demo.jdkBase64(); Base64Demo.commonsCodecBase64 (); Base64Demo.bouncyCastleBase64 (); } //using JDK's base64implementation, public static void jdkBase64 (){ BASE;64Encoder encoder = new BASE;64Encoder(); String encode = encoder.encode(Base;64Demo.src.getBytes()); System.out.println("encode: "); + encode); BASE;64Decoder decoder = new BASE;64Decoder(); try { String decode = new String(decoder.decodeBuffer(encode)); System.out.println("decode: "); + decode); } e.printStackTrace(); } } //using Apache's 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); } //using 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 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 Automatically generated catch block e.printStackTrace(); } catch (InvalidKeySpecException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } } public static void bcDES () { try { //Use BouncyCastle's DES encryption Security.addProvider(new BouncyCastleProvider()); //Generate 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 Automatically generated catch block e.printStackTrace(); } catch (InvalidKeySpecException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (NoSuchProviderException e) { // TODO Automatically generated 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 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 Automatically generated catch block e.printStackTrace(); } catch (InvalidKeySpecException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } } public static void bcTripleDES () { try { Security.addProvider(new BouncyCastleProvider()); //Generate 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 Automatically generated catch block e.printStackTrace(); } catch (InvalidKeySpecException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (NoSuchProviderException e) { // TODO Automatically generated 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())); //Using this initialization method, a key can be generated with a specific seed, making the encrypted ciphertext unique and 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 Automatically generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } } public static void bcAES () { try { //Use BouncyCastle's 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 Automatically generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Automatically generated catch block e.printStackTrace(); } catch (NoSuchProviderException e) { // TODO Automatically generated catch block e.printStackTrace(); } } public static void main(String[] args) { jdkAES(); bcAES(); } }
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to support the Yelling Tutorial more!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)