随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

Android Des加密解密

算法转自:http://www.linuxidc.com/Linux/2011-08/41866.htm 

复制代码
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import android.util.Base64;
public class DesUtils {
 public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
 /**
  * DES算法,加密
  * @param data 待加密字符串
  * @param key 加密私钥,长度不能够小于8位
  * @return 加密后的字节数组,一般结合Base64编码使用
  * @throws CryptException 异常
  */
 public static String encode(String key, String data) throws Exception {
  return encode(key, data.getBytes());
 }
 /**
  * DES算法,加密
  * @param data  待加密字符串
  * @param key  加密私钥,长度不能够小于8位
  * @return 加密后的字节数组,一般结合Base64编码使用
  * @throws CryptException
  *             异常
  */
 private static String encode(String key, byte[] data) throws Exception {
  try {
   DESKeySpec dks = new DESKeySpec(key.getBytes());
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   // key的长度不能够小于8位字节
   Key secretKey = keyFactory.generateSecret(dks);
   Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
   IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
   AlgorithmParameterSpec paramSpec = iv;
   cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
   byte[] bytes = cipher.doFinal(data);
   return Base64.encodeToString(bytes, 0);
  } catch (Exception e) {
   throw new Exception(e);
  }
 }
 /**
  * DES算法,解密
  * @param data  待解密字符串
  * @param key 解密私钥,长度不能够小于8位
  * @return 解密后的字节数组
  * @throws Exception  异常
  */
 private static byte[] decode(String key, byte[] data) throws Exception {
  try {
   DESKeySpec dks = new DESKeySpec(key.getBytes());
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   // key的长度不能够小于8位字节
   Key secretKey = keyFactory.generateSecret(dks);
   Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
   IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
   AlgorithmParameterSpec paramSpec = iv;
   cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
   return cipher.doFinal(data);
  } catch (Exception e) {
   throw new Exception(e);
  }
 }
 
 /**
  * 解密.
  * @param key
  * @param data
  * @return
  * @throws Exception
  */
 public static String decode(String key, String data) {
  byte[] datas;
  String value = null;
  try {
   if (System.getProperty("os.name") != null
     && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
       .getProperty("os.name").equalsIgnoreCase("linux"))) {
    datas = decode(key, Base64.decode(data, 0));
   } else {
    datas = decode(key, Base64.decode(data, 0));
   }
   value = new String(datas);
  } catch (Exception e) {
   value = "";
  }
  return value;
 }
}
复制代码

 

posted on   飘杨......  阅读(5127)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示