`
貌似掉线
  • 浏览: 256695 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

DES加密及解密封装

阅读更多
/*
 * @(#)CipherUtil.java		       Project:androidkit
 * Date:2012-12-18
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.cfuture09.commons.security;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

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

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class CipherUtil {
	public static final String ALGORITHM_DES = "DES";

	/**
	 * 返回可逆算法DES的密钥
	 * 
	 * @param key
	 *            前8字节将被用来生成密钥。
	 * @return 生成的密钥
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public static Key getDESKey(byte[] key) throws InvalidKeyException,
			NoSuchAlgorithmException, InvalidKeySpecException {
		DESKeySpec des = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory
				.getInstance(ALGORITHM_DES);
		return keyFactory.generateSecret(des);
	}

	/**
	 * 根据指定的密钥及算法,将字符串进行解密。
	 * 
	 * @param data
	 *            要进行解密的数据,它是由原来的byte[]数组转化为字符串的结果。
	 * @param key
	 *            密钥。
	 * @param algorithm
	 *            算法。
	 * @return 解密后的结果。它由解密后的byte[]重新创建为String对象。如果解密失败,将返回null。
	 */
	public static String decrypt(String data, Key key, String algorithm) {
		try {
			Cipher cipher = Cipher.getInstance(algorithm);
			cipher.init(Cipher.DECRYPT_MODE, key);
			String result = new String(cipher.doFinal(StringUtil
					.hexStringToBytes(data)), "utf8");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据指定的密钥及算法对指定字符串进行可逆加密。
	 * 
	 * @param data
	 *            要进行加密的字符串。
	 * @param key
	 *            密钥。
	 * @param algorithm
	 *            算法。
	 * @return 加密后的结果将由byte[]数组转换为16进制表示的数组。如果加密过程失败,将返回null。
	 */
	public static String encrypt(String data, Key key, String algorithm) {
		try {
			Cipher cipher = Cipher.getInstance(algorithm);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			return StringUtil.bytesToHexString(cipher.doFinal(data
					.getBytes("utf8")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}


关于其中 的StringUtil类参见http://maosidiaoxian.iteye.com/blog/1751441。
0
1
分享到:
评论

相关推荐

    C++ DES加密解密

    C++ DES加密解密 封装成类了 此代码是测试工具,实现字符串加密解密/文件加密解密。KEY可使用2个 支持中文。 VS2010运行代码

    vue DES加密解密工具类 des.js

    vue DES加密解密工具类 des.js,与博文中的匹配,为封装好的完整工具类

    DES加密算法实现的C++类

    DES加密算法实现的C++类DES加密算法实现的C++类

    Java中3DES加密解密示例(封装byte数组16进制互转)

    Java中3DES加密解密示例(封装byte数组16进制互转)

    des 加密解密,vc c++封装

    在网上找了很久都没有一个很方便的解密解密代码,很郁闷,自己花了点时间,进行封装,des,目标就是方便简单的调用,有源代码和例子

    DES加密封装

    DES加密的完美封装,一共一套加密和解密的方法,前提是使用自己的加密密钥

    封装好的MD5和DES的加密解密代码

    这个文件封装了MD5的加密函数和DES加密解密函数,并且经过测试成功运行的

    DES加密(封装成C++类)

    2009-11-01 07:47 45056 10080 des\DES.exe 2006-12-08 13:17 406528 170355 des\DES_0.exe 2009-11-08 06:42 53248 10524 des\DES_CLASS.exe 2009-11-08 06:39 17229 3099 des\encrypt.cpp.rtf 2009-11-08 06:40 ...

    CSharp_DES加密解密程序源码.zip

    此程序由Microsoft Visual Studio 2010...一种是DES,一种位MD5加密(此加密并非直接调用.net类库中的加密算法) 在加密时需要将生成的dll文件进行引用后方可使用。 没有什么技术含量,练习下dll封装和MD5算法的实现

    PHP实现的DES加密解密封装类完整实例

    本文实例讲述了PHP实现的DES加密解密封装类。分享给大家供大家参考,具体如下: <?php /** * PHP版DES加解密类 * 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 * */ class CryptDes { var $key; var $...

    3DEC加密解密c++封装(实现ECB和BCB模式)

    我在使用得比较多的开源程序d3des.c基础上封装了一个类,可以直接加密文件或者流数据,实现的加解密模式是ECB和BCB。资源中包含类的实现和vc的测试工程,这个类已经在windows,linux,vxworks下使用。

    银联3DES加密解密JAR

    不解释,所有过程本人封装成jar包,导入项目直接调用DES3encrypt(传明文)加密 DES3decrypt(传密文) 解密

    C#版DES加密解密算法实现

    没有使用C#自带的封装,所有功能都是自己实现,附详细代码注释,对于研究DES算法有极大的参考价值。本示例使用DES对数据库连接字符串进行加密,保存到XML文件中。开发工具:VS2008 C# 为了让更多人下载,把资源分...

    C#加密解密Demo.zip

    说到加密,可能大家最熟悉的就是MD5了,一般保存密码、查看文档是否更新都是用到MD5加密,MD5加密是不可逆的,不能解密出明文。...C#里的MD5、DES、AES、RSA加密解密详细方法已封装,大家安心下载使用。

    C#编写DES加密、解密类

    这个C#类封装的DES加密解密,可以使用默认秘钥进行加密、解密,也可以自定义秘钥进行加密、解密,调用简单方便。 示例一: using System; using System.Security.Cryptography; using System.Text; namespace ...

    c# 常用加密,解密类

    包含常用的加密解密,封装了5种,有MD5,DES,RC2,Rijndael,RSA,3DES

    des加密算法解析

    des加密 解密原理以及r4的加密解密原理 可以直接调用 封装在一个接口里面的、

    3des 加密解密框架 适配arc版本

    封装的3des 加解密的类 名字是 3desandbase64 ,详细内容在内里面

    封装好的3DES加解密DLL

    对称加密代码,有des,3des两种加密方式,已经封装好的c++动态库,附有调用说明,用起来绝对简单方便

Global site tag (gtag.js) - Google Analytics