温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中有哪些加密与解密的方法

发布时间:2020-11-10 15:47:44 来源:亿速云 阅读:220 作者:Leah 栏目:编程语言

Java中有哪些加密与解密的方法?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

一、常用的加密/解密算法

1.Base64

严格来说Base64并不是一种加密/解密算法,而是一种编码方式。Base64不生成密钥,通过Base64编码后的密文就可以直接“翻译”为明文,但是可以通过向明文中添加混淆字符来达到加密的效果。

2.DES

DES是一种基于56位密钥的对称算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。现在DES已经不是一种安全的加密算法,已被公开破解,现在DES已经被高级加密标准(AES)所代替。

3.3DES

3DES是DES的一种派生算法,主要提升了DES的一些实用所需的安全性。

4.AES

AES是现在对称加密算法中最流行的算法之一。

二、实现所需的一些库

为了实现上述的算法,我们可以实用JDK自带的实现,也可以使用一些开源的第三方库,例如Bouncy Castle(https://www.bouncycastle.org/)和comnons codec(https://commons.apache.org/proper/commons-codec/)。

三、具体实现

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 ();
 }
 //使用JDK的base64实现,
 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();
 } 
 }
 //使用apache的commonsCodec实现
 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);
 }
 //使用bouncyCastlede实现
 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 {
  //生成密钥Key
  KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
  keyGenerator.init(56);
  SecretKey secretKey = keyGenerator.generateKey();
  byte[] bytesKey = secretKey.getEncoded();
  //KEY转换
  DESKeySpec deSedeKeySpec = new DESKeySpec(bytesKey);
  SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
  Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
  //加密
  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));
  //解密
  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 自动生成的 catch 块
  e.printStackTrace();
 } catch (InvalidKeySpecException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (IllegalBlockSizeException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (BadPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 }
 }
 public static void bcDES (){
 try {
  //使用BouncyCastle 的DES加密
  Security.addProvider(new BouncyCastleProvider());
  //生成密钥Key
  KeyGenerator keyGenerator = KeyGenerator.getInstance("DES","BC");
  keyGenerator.init(56);
  SecretKey secretKey = keyGenerator.generateKey();
  byte[] bytesKey = secretKey.getEncoded();
  //KEY转换
  DESKeySpec deSedeKeySpec = new DESKeySpec(bytesKey);
  SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
  Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
  //加密
  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));
  //解密
  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 自动生成的 catch 块
  e.printStackTrace();
 } catch (InvalidKeySpecException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (IllegalBlockSizeException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (BadPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchProviderException e) {
  // TODO 自动生成的 catch 块
  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 {
  //生成密钥Key
  KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
  keyGenerator.init(168);
  SecretKey secretKey = keyGenerator.generateKey();
  byte[] bytesKey = secretKey.getEncoded();
  //KEY转换
  DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(bytesKey);
  SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
  Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
  //加密
  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));
  //解密
  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 自动生成的 catch 块
  e.printStackTrace();
 } catch (InvalidKeySpecException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (IllegalBlockSizeException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (BadPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 }
 }
public static void bcTripleDES () {
 try {
  Security.addProvider(new BouncyCastleProvider());
  //生成密钥Key
  KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede","BC");
  keyGenerator.getProvider();
  keyGenerator.init(168);
  SecretKey secretKey = keyGenerator.generateKey();
  byte[] bytesKey = secretKey.getEncoded();
  //KEY转换
  DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(bytesKey);
  SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
  Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
  //加密
  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));
  //解密
  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 自动生成的 catch 块
  e.printStackTrace();
 } catch (InvalidKeySpecException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (IllegalBlockSizeException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (BadPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchProviderException e) {
  // TODO 自动生成的 catch 块
  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 {
  //生成Key
  KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  keyGenerator.init(128); 
  //keyGenerator.init(128, new SecureRandom("seedseedseed".getBytes())); 
  //使用上面这种初始化方法可以特定种子来生成密钥,这样加密后的密文是唯一固定的。
  SecretKey secretKey = keyGenerator.generateKey();
  byte[] keyBytes = secretKey.getEncoded();
  //Key转换
  Key key = new SecretKeySpec(keyBytes, "AES");
  //加密
  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) );
  //解密
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] decodeResult = cipher.doFinal(encodeResult);
  System.out.println("AESdecode : " + new String (decodeResult));
 } catch (NoSuchAlgorithmException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (InvalidKeyException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (IllegalBlockSizeException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (BadPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 }
 }
 public static void bcAES (){
 try {
  //使用BouncyCastle 的DES加密
  Security.addProvider(new BouncyCastleProvider());
  //生成Key
  KeyGenerator keyGenerator = KeyGenerator.getInstance("AES","BC");
  keyGenerator.getProvider();
  keyGenerator.init(128); 
  SecretKey secretKey = keyGenerator.generateKey();
  byte[] keyBytes = secretKey.getEncoded();
  //Key转换
  Key key = new SecretKeySpec(keyBytes, "AES");
  //加密
  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) );
  //解密
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] decodeResult = cipher.doFinal(encodeResult);
  System.out.println("AESdecode : " + new String (decodeResult));
 } catch (NoSuchAlgorithmException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (InvalidKeyException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (IllegalBlockSizeException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (BadPaddingException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 } catch (NoSuchProviderException e) {
  // TODO 自动生成的 catch 块
  e.printStackTrace();
 }
 }
 public static void main(String[] args) {
 jdkAES();
 bcAES();
 }
}

看完上述内容,你们掌握Java中有哪些加密与解密的方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI