DES加密
2016-07-14 12:59
218 查看
简单的DES加密解密方法
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Base64;
public class DESCodecTest
{
public static final String ALGORITHM_DES = "DES/ECB/PKCS5Padding";
public static String encode(String key,String data) throws Exception
{
return encode(key, data.getBytes());
}
public 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);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(data);
return bytes == null ? null : new String(Base64.encodeBase64(bytes));
// return bytes;
} catch (Exception e){
throw new Exception(e);
}
}
public static String decode(String key,String data) throws Exception
{
return decode(key, data.getBytes());
}
public static String 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);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte [] bytes = cipher.doFinal(Base64.decodeBase64(data));
return bytes == null ? null : new String(bytes);
//return bytes;
} catch (Exception e)
{
throw new Exception(e);
}
}
public static void main(String [] args) throws Exception{
String en = DESCodecTest.encode("20160113SS000020", "PASSWORD");
System.out.println(en);
String result = DESCodecTest.decode("20160113SS000020", en);
System.out.println(result);
}
}
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Base64;
public class DESCodecTest
{
public static final String ALGORITHM_DES = "DES/ECB/PKCS5Padding";
public static String encode(String key,String data) throws Exception
{
return encode(key, data.getBytes());
}
public 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);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(data);
return bytes == null ? null : new String(Base64.encodeBase64(bytes));
// return bytes;
} catch (Exception e){
throw new Exception(e);
}
}
public static String decode(String key,String data) throws Exception
{
return decode(key, data.getBytes());
}
public static String 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);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte [] bytes = cipher.doFinal(Base64.decodeBase64(data));
return bytes == null ? null : new String(bytes);
//return bytes;
} catch (Exception e)
{
throw new Exception(e);
}
}
public static void main(String [] args) throws Exception{
String en = DESCodecTest.encode("20160113SS000020", "PASSWORD");
System.out.println(en);
String result = DESCodecTest.decode("20160113SS000020", en);
System.out.println(result);
}
}
相关文章推荐
- java的动态代理到底生成了什么
- ToolProvider.getSystemJavaCompiler() Return NULL!
- Git23--配置别名
- LeetCode-179.Largest Number
- Enumeration, Iterator and ListIterator in Java
- JQuery 摘抄
- Git22--忽略特殊文件
- 使用spring boot构建微服务
- 改善用户体验之wordpress添加图片弹出层效果 (插件 FancyBox)
- 通过leakCanary找出程序内存泄露点
- 【设计模式】单例模式(一)
- 目录属性为drwxrwxrwt
- JVM参数与内存模型
- C++ 用libcurl库进行http通讯网络编程
- PyNLPIR简介和下载安装
- 【个人喜好诗词之一】蜀道难
- Git21--自定义Git
- jquery在线扫雷
- 代理者模式-结构型
- SuperQQ小项目遇到的问题(1)