JAVA DESUtils加密工具


https://blog.csdn.net/jiangxuexuanshuang/article/details/88002289

示例代碼:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
/** * @Description: 對參數進行DES和Base64加密 */ public String encryptParam(String sourceParam, String key) throws Exception { byte[] byteContent = sourceParam.getBytes("UTF-8"); SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, securekey, random); return Base64.getEncoder().encodeToString(cipher.doFinal(byteContent)); }

 

   //對base64以及DES加密后數據進行解密
    public String decryptParam(String sourceParam, String key) throws Exception {
        byte[] byteContent = Base64.getDecoder().decode(sourceParam);
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        return new String(cipher.doFinal(byteContent), "UTF-8");
    }

測試加解密

   String encryPlatArchiveNo = null; //參數加密
    String decryPlatArchiveNo = null; //參數加密
           try {
               encryPlatArchiveNo = encryptParam(platArchiveNo, "ABC123ABC123ABCD");
               decryPlatArchiveNo= decryptParam(encryPlatArchiveNo, "ABC123ABC123ABCD");
                } catch (Exception e) {
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM