设为首页 加入收藏

TOP

Android实现DES和3DES算法(一)
2017-01-20 08:15:17 】 浏览:3660
Tags:Android 实现 DES 3DES 算法

DES算法如下:


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;


public class DESUtils {
?private static byte[] parse(String str) {
? byte[] b = new byte[str.length() / 2];
? for (int i = 0, n = str.length(); i < n; i += 2) {
? ?b[i / 2] = (byte) (Integer.parseInt(str.substring(i, i + 2), 16));
? }
? return b;
?}


?/**
? * 加密
? * @param src
? * @param password
? * @return
? */
?public static byte[] enCode(byte[] src, String password) {
? try {
? ?SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
? ?SecretKey key = keyFactory.generateSecret(new DESKeySpec(parse(password)));// 密钥
? ?Cipher cipher = Cipher.getInstance("DES");
? ?cipher.init(Cipher.ENCRYPT_MODE, key);
? ?return cipher.doFinal(src);// 明文
? } catch (Exception e) {
? ?e.printStackTrace();
? }
? return null;


?}


?/**
? * 解密
? * @param src
? * @param password
? * @return
? * @throws Exception
? */
?public static byte[] deCode(byte[] src, String password) throws Exception {
? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
? SecretKey key = keyFactory.generateSecret(new DESKeySpec(parse(password)));// 密钥
? Cipher cipher = Cipher.getInstance("DES");
? cipher.init(Cipher.DECRYPT_MODE, key);
? return cipher.doFinal(src);// 密文
?}
}


3DES算法是进行了3次DES算法。
E是代表加密,D代表解密,K1,K2,K3代表3个密钥。加密解密都是用DES算法,一般来说密钥需要3个或两个密钥,如果3个密钥都相同,则变成了DES算法,如果对加密强度要求没有那么高,可以用两个密钥,对应的是K1和K3密钥相同。
3DES加密过程:EK1(加密)-->DK2(解密)-->EK3(加密)
3DES解密过程:DK3(解密)-->EK2(加密)-->DK1(解密)


3DES算法如下:


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class DES3Utils {


private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish
? ?
? ? //keybyte为加密密钥,长度为24字节
? ? //src为被加密的数据缓冲区(源)
? ? public static byte[] encryptMode(byte[] keybyte, byte[] src) {
? ? ? try {
? ? ? ? ? ? //生成密钥
? ? ? ? ? ? SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);


? ? ? ? ? ? //加密
? ? ? ? ? ? Cipher c1 = Cipher.getInstance(Algorithm);
? ? ? ? ? ? c1.init(Cipher.ENCRYPT_MODE, deskey);
? ? ? ? ? ? return c1.doFinal(src);
? ? ? ? } catch (java.security.NoSuchAlgorithmException e1) {
? ? ? ? ? ? e1.printStackTrace();
? ? ? ? } catch (javax.crypto.NoSuchPaddingException e2) {
? ? ? ? ? ? e2.printStackTrace();
? ? ? ? } catch (java.lang.Exception e3) {
? ? ? ? ? ? e3.printStackTrace();
? ? ? ? }
? ? ? ? return null;
? ? }


? ? //keybyte为加密密钥,长度为24字节
? ? //src为加密后的缓冲区
? ? public static byte[] decryptMode(byte[] keybyte, byte[] src) {? ? ?
? ? try {
? ? ? ? ? ? //生成密钥
? ? ? ? ? ? SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);


? ? ? ? ? ? //解密
? ? ? ? ? ? Cipher c1 = Cipher.getInstance(Algorithm);
? ? ? ? ? ? c1.init(Cipher.DECRYPT_MODE, deskey);
? ? ? ? ? ? return c1.doFinal(src);
? ? ? ? } catch (java.security.NoSuchAlgorithmException e1) {
? ? ? ? ? ? e1.printStackTrace();
? ? ? ? } catch (javax.crypto.NoSuchPaddingException e2) {
? ? ? ? ? ? e2.printStackTrace();
? ? ? ? } catch (java.lang.Exception e3) {
? ? ? ? ? ? e3.printStackTrace();
? ? ? ? }
? ? ? ? return null;
? ? }


? ? //转换成十六进制字符串
? ? public static String byte2hex(byte[] b) {
? ? ? ? String hs="";
? ? ? ? String stmp="";


? ? ? ? for (int n=0;n? ? ? ? ? ? stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
? ? ? ? ? ? if (stmp.length()==1) hs=hs+"0"+stmp;
? ? ? ? ? ? else hs=hs+stmp;
? ? ? ? ? ? if (n? ? ? ? }
? ? ? ? return hs.toUpperCase();
? ? }
}


调用


//DES
?//DES加密
?byte[] en = DESUtils.enCode(hexStringToBytes("3132333435363738"), "A1A2A3A4A5A6A7A8");
?//DES解密?
?try {
? byte[] dn = DES

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android 6.0动态权限申请 下一篇SpringMVC项目中使用Google Kaptc..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目