DES加解密时 Given final block not properly padded 的解决方案
事情的經過是這個樣子的。。。。。。
??????? 先說說問題是怎么出現的。根據客戶需求,需要完成一個一鍵登錄的功能,于是我的項目中就誕生了DesUtil,但是經過幾百次測試,發現有一個登錄直接報錯!難道又遇到神坑啦!!
?????? 讓我們先看看源代碼,干貨來了!
?????? package com.kwp.main.util.security;
import java.io.IOException;
import java.security.SecureRandom;
?
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
?
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
?
public class DesUtil {
?
??? private final static String DES = "DES";
??? public final static String KEY = "EA22DAB57022E2560A376749E3408196A9E287D800E068E5";
??? /**
???? * Description 根據鍵值進行加密
???? * @param data
???? * @param key? 加密鍵byte數組
???? * @return
???? * @throws Exception
???? */
??? public static String encrypt(String data, String key) throws Exception {
??????? byte[] bt = encrypt(data.getBytes(), key.getBytes());
??????? String strs = new BASE64Encoder().encode(bt);
??????? return strs;
??? }
?
??? /**
???? * Description 根據鍵值進行解密
???? * @param data
???? * @param key? 加密鍵byte數組
???? * @return
???? * @throws IOException
???? * @throws Exception
???? */
??? public static String decrypt(String data, String key) throws IOException,
??????????? Exception {
??????? if (data == null)
??????????? return null;
??????? BASE64Decoder decoder = new BASE64Decoder();
??????? byte[] buf = decoder.decodeBuffer(data);
??????? byte[] bt = decrypt(buf,key.getBytes());
??????? return new String(bt);
??? }
?
??? /**
???? * Description 根據鍵值進行加密
???? * @param data
???? * @param key? 加密鍵byte數組
???? * @return
???? * @throws Exception
???? */
??? private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
??????? // 生成一個可信任的隨機數源
??????? SecureRandom sr = new SecureRandom();
?
??????? // 從原始密鑰數據創建DESKeySpec對象
??????? DESKeySpec dks = new DESKeySpec(key);
?
??????? // 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
??????? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
??????? SecretKey securekey = keyFactory.generateSecret(dks);
?
??????? // Cipher對象實際完成加密操作
??????? Cipher cipher = Cipher.getInstance(DES);
?
??????? // 用密鑰初始化Cipher對象
??????? cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
?
??????? return cipher.doFinal(data);
??? }
??? ?
??? ?
??? /**
???? * Description 根據鍵值進行解密
???? * @param data
???? * @param key? 加密鍵byte數組
???? * @return
???? * @throws Exception
???? */
??? private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
??????? // 生成一個可信任的隨機數源
??????? SecureRandom sr = new SecureRandom();
?
??????? // 從原始密鑰數據創建DESKeySpec對象
??????? DESKeySpec dks = new DESKeySpec(key);
?
??????? // 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
??????? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
??????? SecretKey securekey = keyFactory.generateSecret(dks);
?
??????? // Cipher對象實際完成解密操作
??????? Cipher cipher = Cipher.getInstance(DES);
?
??????? // 用密鑰初始化Cipher對象
??????? cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
?
??????? return cipher.doFinal(data);
??? }
?? ?
??? public static void main(String[] args)throws Exception{
?? ??? ?//加密
?? ??? ?System.out.println(DesUtil.encrypt("zhaohui", DesUtil.KEY));
?? ??? ?System.out.println(DesUtil.encrypt("0", DesUtil.KEY));
?? ??? ?//解密
?? ??? ?System.out.println(DesUtil.decrypt("/sVcz2jGgPQ=", DesUtil.KEY));
??? }
}
總結
以上是生活随笔為你收集整理的DES加解密时 Given final block not properly padded 的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Gradle编译问题
- 下一篇: 韦东山freeRTOS系列教程之【第三章