des的加密和解密(無Base64的轉碼)

--加密 加密

public static byte[] encrypt(String message, String key)    code

           throws Exception {   
       Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");   
 
       DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));   
 
       SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");   
       SecretKey secretKey = keyFactory.generateSecret(desKeySpec);   
       IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));   
       cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);   
       BASE64Decoder base64Decoder = new BASE64Decoder();
       return cipher.doFinal(message.getBytes("UTF-8"));   

   }  ip


--解密 ci

public static String decrypt(String message, String key)   
           throws Exception {   
byte[] bytesrc = stringToBytes(message);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
//return new String(pasByte, "UTF-8");
return new String(retByte, "UTF-8"); 
   }
get

相關文章
相關標籤/搜索