Base64.java

package com.mstf.des;

import java.io.UnsupportedEncodingException;

/**
* base64编码/解码
* @author ceet
*
*/
public class Base64 { public static String encode(String data) {
return new String(encode(data.getBytes()));
} public static String decode(String data) {
try {
return new String(decode(data.toCharArray()),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
} private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
.toCharArray(); private static byte[] codes = new byte[256]; static {
for (int i = 0; i < 256; i++) {
codes[i] = -1;
}
for (int i = 'A'; i <= 'Z'; i++) {
codes[i] = (byte) (i - 'A');
} for (int i = 'a'; i <= 'z'; i++) {
codes[i] = (byte) (26 + i - 'a');
}
for (int i = '0'; i <= '9'; i++) {
codes[i] = (byte) (52 + i - '0');
}
codes['+'] = 62;
codes['/'] = 63;
} public static char[] encode(byte[] data) {
char[] out = new char[((data.length + 2) / 3) * 4];
for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
boolean quad = false;
boolean trip = false; int val = (0xFF & (int) data[i]);
val <<= 8;
if ((i + 1) < data.length) {
val |= (0xFF & (int) data[i + 1]);
trip = true;
}
val <<= 8;
if ((i + 2) < data.length) {
val |= (0xFF & (int) data[i + 2]);
quad = true;
}
out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 1] = alphabet[val & 0x3F];
val >>= 6;
out[index + 0] = alphabet[val & 0x3F];
}
return out;
} public static byte[] decode(char[] data) {
int tempLen = data.length;
for (int ix = 0; ix < data.length; ix++) {
if ((data[ix] > 255) || codes[data[ix]] < 0) {
--tempLen;
}
}
int len = (tempLen / 4) * 3;
if ((tempLen % 4) == 3) {
len += 2;
}
if ((tempLen % 4) == 2) {
len += 1; }
byte[] out = new byte[len]; int shift = 0;
int accum = 0;
int index = 0; for (int ix = 0; ix < data.length; ix++) {
int value = (data[ix] > 255) ? -1 : codes[data[ix]]; if (value >= 0) {
accum <<= 6;
shift += 6;
accum |= value;
if (shift >= 8) {
shift -= 8;
out[index++] = (byte) ((accum >> shift) & 0xff);
}
}
} if (index != out.length) {
throw new Error("Miscalculated data length (wrote " + index
+ " instead of " + out.length + ")");
} return out;
}
}

DESUtil.java

package com.mstf.des;

import java.security.Key;
import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; /**
* DES对称算法(加密/解密)
*
* @author ceet
*
*/
public class DESUtil { private Key key; public DESUtil(String strKey) {
setKey(strKey);
} public void setKey(String strKey) {
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(strKey.getBytes())); // 根据参数生成key
this.key = generator.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
} public String encrypt(String source) {
return encrypt(source, "utf-8");
} public String decrypt(String encryptedData) {
return decrypt(encryptedData, "utf-8");
} public String encrypt(String source, String charSet) {
String encrypt = null;
try {
byte[] ret = encrypt(source.getBytes(charSet));
encrypt = new String(Base64.encode(ret));
} catch (Exception e) {
e.printStackTrace();
encrypt = null;
}
return encrypt;
} public String decrypt(String encryptedData, String charSet) {
String descryptedData = null;
try {
byte[] ret = descrypt(Base64.decode(encryptedData.toCharArray()));
descryptedData = new String(ret, charSet);
} catch (Exception e) {
e.printStackTrace();
descryptedData = null;
}
return descryptedData;
} private byte[] encrypt(byte[] primaryData) {
try {
Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成加密操作
cipher.init(Cipher.ENCRYPT_MODE, this.key); // 用密钥初始化Cipher对象(加密) return cipher.doFinal(primaryData);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} private byte[] descrypt(byte[] encryptedData) {
try {
Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成解密操作
cipher.init(Cipher.DECRYPT_MODE, this.key); // 用密钥初始化Cipher对象(解密) return cipher.doFinal(encryptedData);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static void main(String[] args) {
String code = "ceet";
DESUtil desUtil = new DESUtil("key");
String encrypt = desUtil.encrypt(code);
String decrypt = desUtil.decrypt(encrypt);
System.out.println("原内容:" + code);
System.out.println("加密:" + encrypt);
System.out.println("解密:" + decrypt);
}
}

  

最简单的DES加密算法实现的更多相关文章

  1. 简单理解DES加密算法

    数据加密标准(Data Encryption Standard,DES)是当前使用最广泛的加密体制,对于任意的加密方案,总有两个输入:明文和密钥. 明文是64bits,密钥是56bits 加密过程就是 ...

  2. DES加密算法的C++实现

    <信息安全技术>这门课又在讲 DES 加密算法了,以前用纯C写过一次,这次我用 C++ 重新写了一个,写篇文章以备后用.本文介绍了 DES 算法加密的大致步骤和整体流程. 一.DES算法原 ...

  3. 在.NET Core 里使用 BouncyCastle 的DES加密算法

    .NET Core上面的DES等加密算法要等到1.2 才支持,我们可是急需这个算法的支持,文章<使用 JavaScriptService 在.NET Core 里实现DES加密算法>需要用 ...

  4. 浅谈DES加密算法

    一.DES加密算法介绍 1.要求密钥必须是8个字节,即64bit长度 2.因为密钥是byte[8] , 代表字符串也可以是非可见的字节,可以与Base64编码算法一起使用 3.加密.解密都需要通过字节 ...

  5. JAVA使用DES加密算法加密解密

    程序中使用了.properties文件作为参数配置文档,好处是灵活配置各项参数 一旦对数据库的一些参数进行了配置,势必涉及数据库的IP,端口,用户名和密码 properties文件全是unicode编 ...

  6. 对称密码——DES加密算法

    前言 本篇博文将介绍对称密码算法中的DES密码的算法原理与代码实现(Java) DES算法原理 DES加密算法是对称加密算法(加密和解密使用同一个密钥)中的一种,DES也是分组密码,以64位为分组对明 ...

  7. des加密算法java&c#

    项目中用到的数据加密方式是ECB模式的DES加密得到的十六进制字符串.技术支持让写一个.net版的加密算法.这里做一下记录. java版: 16进制使用的是bouncycastle. import c ...

  8. android和.net webservice中的DES加密算法

    也是看了一堆的例子,本身并不会写加密算法,好在只要会用就行了,我们把在app中使用的参数加密,然后在.net端的webservice中进行解密,本身并没有什么问题,但是android下和.net下的d ...

  9. .net中DES加密算法研究

    /// <summary> /// DES加密算法 /// </summary> /// <param name="toEncrypt">要加密 ...

随机推荐

  1. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  2. BZOJ 1007: [HNOI2008]水平可见直线 平面直线

    1007: [HNOI2008]水平可见直线 Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则 ...

  3. kentico version history and upgrade

    Version history Kentico 10: November 30, 2016 Kentico 9: November 24, 2015 Kentico 8.2: January 6, 2 ...

  4. 创建表空间及plsql查看远程表空间路径

    -新建表空间,登录名和密码 --请尽量把表空间和别的系统分离,这里以Search为例子,登录名和密码以test为例子 create tablespace Search logging datafile ...

  5. Linux下安装ipython与jupyter

    IPython从Python发展而来,更倾向于科学计算.互联网数据分析更喜欢用. 首先切换root用户: su - root pip3自动安装ipython [root@hear ~]# ipytho ...

  6. C#线程调用带参数的方法,给控件赋值

    System.Threading.Thread thread = new System.Threading.Thread(() => { //各种业务 //定义一个委托 public deleg ...

  7. Windows 下 Sketch 替代 APP(UWP)

    不得了,这个版本开始支持 Sketch 的打开了,还可以编辑. https://www.adobe.com/cn/products/xd/features.html Lunacy 支持 Sketch ...

  8. Windows 10 Mobile 演示:插入耳机自动执行 APP

    Windows Mobile 10 新特性:插入外部设备自动动作(如插入耳机执行 APP.打开小工具):另外可以找到最后一次使用设备地点和时间: http://www.tudou.com/progra ...

  9. 关于jsp web项目,jsp页面与servlet数据不同步的解决办法(报错404、405等)即访问.jsp和访问web.xml中注册的/servlet/的区别

    报错信息: Type Status Report Message HTTP method GET is not supported by this URL Description The method ...

  10. MyEclipse 启动之 java.lang.RuntimeException: No application id has been

    found. 今天公司刚买来一台服务器,配置安装 java 开发环境的时候,MyEclipse 无法启动,查看日志文件之后,具体错误信息 如下: [java] view plaincopyprint? ...