使用前需导入Crypto包: Utils工具类: 使用前 调用DesUtil的 不能直接调用DesUtil里的方法进行加密解密 否则会报空指针异常<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-crypto</artifactId> <version>5.2.1.RELEASE</version> </dependency>
import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; /** * @author Piconjo * @date 2020/6/1 20:15 */ public class DesUtil { private static DesUtil instance; private Key key; private Cipher encryptCipher; private Cipher decryptCipher; // 密钥 private static final String SECRET_KEY="iloveumybaby"; protected DesUtil() { } protected DesUtil(String strKey) { key = setKey(strKey); try { encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE, key); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } } // 初始化实例 public static DesUtil getInstance() { if (instance == null) { instance = new DesUtil(SECRET_KEY); } return instance; } // 根据参数生成KEY private Key setKey(String strKey) { try { KeyGenerator _generator = KeyGenerator.getInstance("DES"); _generator.init(new SecureRandom(strKey.getBytes())); return _generator.generateKey(); } catch (Exception e) { e.printStackTrace(); } return null; } // 加密 - String明文输入 String密文输出 public String setEncString(String strMing) { BASE64Encoder base64en = new BASE64Encoder(); try { byte[] byteMing = strMing.getBytes("UTF-8"); byte[] byteMi = getEncCode(byteMing); return base64en.encode(byteMi); } catch (Exception e) { e.printStackTrace(); } return null; } // 加密 - byte[]明文输入 byte[]密文输出 private byte[] getEncCode(byte[] byteS) { byte[] byteFina = null; try { byteFina = encryptCipher.doFinal(byteS); } catch (Exception e) { e.printStackTrace(); } return byteFina; } // 解密 - String密文输入 String明文输出 public String setDesString(String strMi) { BASE64Decoder base64De = new BASE64Decoder(); try { byte[] byteMi = base64De.decodeBuffer(strMi); byte[] byteMing = getDesCode(byteMi); return new String(byteMing, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return null; } // 解密 - byte[]密文输入 byte[]明文输出 private byte[] getDesCode(byte[] byteD) { byte[] byteFina = null; try { byteFina = decryptCipher.doFinal(byteD); } catch (Exception e) { e.printStackTrace(); } return byteFina; } // 单测 public static void main(String[] args) { DesUtil desUtil = DesUtil.getInstance(); String code= dtDesUtil.setEncString("111111@qq.com"); System.out.println(code); System.out.println(dtDesUtil.setDesString(code)); } }
使用须知:
getInstance()
方法 生成DesUtil类的实例
然后再使用返回的DesUtil对象进行密码加密解密操作
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算