DES算法java实现
发布时间:2020-05-28 21:56:56 所属栏目:Java 来源:互联网
导读:DES算法java实现
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 解密的数据,mode为其工作模式。当模式为加密模式时,明文按照64位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性。 package com.gary.test.ws.test;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class SymmetricAlgorithm {
private String strKey = "&^%$*#@~";
private String info;
public SymmetricAlgorithm(String info) {
this.info = info;
}
public SymmetricAlgorithm(String info,String strKey) {
this.info = info;
this.strKey = strKey;
}
private Key getKey() {
byte[] keyBtye = this.strKey.getBytes();
byte[] _keyByte = new byte[8];
for (int i = 0; (i < keyBtye.length) && (i < _keyByte.length); i++) {
_keyByte[i] = keyBtye[i];
}
return new SecretKeySpec(_keyByte,"DES");
}
public String desEncrypt() {
return desEncrypt(this.info,"UTF-8");
}
public String desEncrypt(String origin,String encoding) {
if ((origin == null) || (encoding == null))
return null;
try {
return encrypt(origin.getBytes(encoding),"DES");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public String desDecrypt() {
return desDecrypt(this.info,"UTF-8");
}
public String desDecrypt(String ciperData,String encoding) {
if ((ciperData == null) || (encoding == null)) {
return null;
}
byte[] b = decrypt(EncryptHelper.hex2byte(ciperData),"DES");
try {
return new String(b,encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
private String encrypt(byte[] data,String algorithm) {
try {
Key key = getKey();
Cipher c1 = Cipher.getInstance(algorithm);
c1.init(1,key);
byte[] cipherByte = c1.doFinal(data);
return EncryptHelper.byte2hex(cipherByte);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
private byte[] decrypt(byte[] data,String algorithm) {
try {
Key key = getKey();
Cipher c1 = Cipher.getInstance(algorithm);
c1.init(2,key);
return c1.doFinal(data);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
SymmetricAlgorithm s =new SymmetricAlgorithm("gp3adm","&^%$*#@~");//加密
String sa_pwd = s.desEncrypt("gp3adm","UTF-8");
System.out.println("加密后:"+sa_pwd);
String pwd = "84DFA223A4521331";
String password = (new SymmetricAlgorithm(pwd)).desDecrypt();// 解密
System.out.println("解密后:"+password);
}
}
package com.gary.test.ws.test;
public class EncryptHelper
{
public static final String DEFAULT_ENCODING = "UTF-8";
public static String byte2hex(byte[] bytes)
{
StringBuffer retString = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
retString.append(Integer.toHexString(256 + (bytes[i] & 0xFF)).substring(1));
}
return retString.toString().toUpperCase();
}
public static byte[] hex2byte(String hex)
{
byte[] bts = new byte[hex.length() / 2];
for (int i = 0; i < bts.length; i++) {
bts[i] = (byte)Integer.parseInt(hex.substring(2 * i,2 * i + 2),16);
}
return bts;
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
