1.Base64
加密:org.apache.commons.codec.binary.Base64.encodeBase64(byte[] binaryData)
解密:org.apache.commons.codec.binary.Base64.decodeBase64(byte[] base64Data)
2.Md5
加密:org.apache.commons.codec.digest.md5Hex(byte[] data)
解密:无
3.DES(des-ecb,3des,des-cbc,cbc-mac)
view plaincopy to clipboardprint?
import java.io.ByteArrayOutputStream;
import java.security.SecureRandom;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.engines.DESEngine;
import org.bouncycastle.crypto.macs.CBCBlockCipherMac;
import org.bouncycastle.crypto.params.KeyParameter;
import com.alibaba.common.lang.StringUtil;
import com.huateng.commons.lang.convert.HexUtils;
public class ShfftDes {
//验证用密钥
private byte[] key = "000000000000000000000000".getBytes();
// private byte[] key = Hex.decode("00000000");
private byte[] ivs = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
private static final String DES_EDE = "DESede/ECB/NoPadding"; //定义 加密算法,可用 DES,DESede,Blowfish //keybyte为加密密钥,长度为24字节 //src为被加密的数据缓冲区(源)
private static final String DES_EDE_CBC = "DESede/CBC/NoPadding"; //定义 加密算法,可用 DES,DESede,Blowfish //keybyte为加密密钥,长度为24字节 //src为被加密的数据缓冲区(源)
private static final String DES_CBC = "DES/CBC/NoPadding";
private static final String DES_ECB = "DES/ECB/PKCS5Padding";
public byte[] CryptByDes(byte[] content, int mode) throws Exception {
Cipher cipher = Cipher.getInstance(DES_ECB);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(new DESKeySpec(key));
cipher.init(mode, secretKey);
return cipher.doFinal(content);
}
public byte[] CryptBy3Des(byte[] content, int mode) throws Exception {
Cipher cipher = Cipher.getInstance(DES_EDE);
SecretKey secretKey = new SecretKeySpec(key, "DESede");
cipher.init(mode, secretKey);
return cipher.doFinal(content);
}
public byte[] CryptByDesCbc(byte[] content, int mode) throws Exception {
Cipher cipher = Cipher.getInstance(DES_CBC);
SecretKey secureKey = new SecretKeySpec(key, "DES");
IvParameterSpec iv = new IvParameterSpec(ivs);
cipher.init(mode, secureKey, iv);
return cipher.doFinal(HexUtils.fromHex(new String(content)));
}
public byte[] CryptBy3DesCbc(byte[] content, int mode) throws Exception {
Cipher cipher = Cipher.getInstance(DES_EDE_CBC);
SecretKey secureKey = new SecretKeySpec(key, "DESede");
IvParameterSpec iv = new IvParameterSpec(ivs);
cipher.init(mode, secureKey, iv);
return cipher.doFinal(content);
}
public byte[] CryptByDesCbcMac(byte[] content) throws Exception {
BlockCipher engine = new DESEngine();
Mac mac = new CBCBlockCipherMac(engine, 64);
byte[] macText = new byte[engine.getBlockSize()];
mac.init(new KeyParameter(key));
mac.update(Padding(content, 64), 0, content.length);
mac.update(content, 0, content.length);
mac.doFinal(macText, 0);
return macText;
}
public byte[] ShFftCryptByDessdsCbc(byte[] content, int mode) throws Exception {
byte[] ks1 = HexUtils.fromHex(new String(key));
byte[] ks = new byte[24];
System.arraycopy(ks1, 0, ks, 0, ks1.length);
System.arraycopy(ks1, 0, ks, ks1.length, 8);
Cipher cipher = Cipher.getInstance(DES_EDE_CBC);
SecretKeyFactory keyFactory = null;
keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey secretKey = null;
secretKey = keyFactory.generateSecret(new DESedeKeySpec(ks));
IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 });
cipher.init(mode, secretKey, iv);
return cipher.doFinal(HexUtils.fromHex(new String(content)));
}
public byte[] mac(byte[] content) throws Exception {
int len;
byte plainData[];
byte encryptedData[];
len = (content.length / 8 + (content.length % 8 != 0 ? 1 : 0)) * 8;
plainData = new byte[len];
encryptedData = new byte[8];
Arrays.fill(plainData, (byte) 32);
System.arraycopy(content, 0, plainData, 0, content.length);
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = null;
keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");
IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 });
cipher.init(1, secretKey, iv, sr);
System.arraycopy(cipher.doFinal(plainData), len - 8, encryptedData, 0, 8);
return encryptedData;
}
public byte[] Padding(byte[] content, int block) {
int contentLength = content.length;
int mod = contentLength % block;
if (mod != 0) {
int size = contentLength + block - mod;
// String s = new String(content);
// StringUtil.alignLeft(s, size, " ");
byte[] s = new byte[size];
System.arraycopy(content, 0, s, 0, content.length);
for (int i = content.length; i < size; i++) {
s[i] = 32;
}
return s;
}
return content;
}
public String Padding(String content, int block) {
int contentLength = content.length();
int mod = contentLength % block;
if (mod != 0) {
int size = contentLength + block - mod;
String s = new String(content);
StringUtil.alignLeft(s, size, " ");
return s;
}
return content;
}
public void println(byte[] bs) {
for (byte b : bs) {
System.out.print(b + " ");
}
System.out.println();
}
public void printlnByte(byte[] bs) {
for (byte b : bs) {
if (b < 0) {
System.out.print((int) b + 256 + " ");
} else {
System.out.print(b + " ");
}
}
System.out.println();
}
public void printlnByteInt16(byte[] bs) {
for (byte b : bs) {
System.out.print(Integer.toHexString((int) b) + " ");
}
System.out.println();
}
public String dumpBytes(byte[] bytes) {
int i;
StringBuffer sb = new StringBuffer();
for (i = 0; i < bytes.length; i++) {
int n = bytes[i] >= 0 ? bytes[i] : 256 + bytes[i];
String s = Integer.toHexString(n);
if (s.length() < 2) {
s = "0" + s;
}
if (s.length() > 2) {
s = s.substring(s.length() - 2);
}
sb.append(s);
}
return sb.toString().toUpperCase();
//return new BASE64Encoder().encode(bytes);
}
// 一下程序将每2位16进制整数组装成一个字节
private String hexString = "0123456789ABCDEF";
public byte[] decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes
.charAt(i + 1))));
return baos.toByteArray();
}
public byte[] getKey() {
return key;
}
public void setKey(byte[] key) {
this.key = key;
}
public byte[] getIvs() {
return ivs;
}
public void setIvs(byte[] ivs) {
this.ivs = ivs;
}
}
相关推荐
本文主要介绍了Java中常用加密/解密方法。具有很好的参考价值,下面跟着小编一起来看下吧
本次使用JAVA语言,采用3DES加密算法进行对文本文件的加密,通过二进制和十六进制的转换来实现,产生加密文件,再对加密文件进行解密。 关键词:DES加密算法、加密、解密。摘要 随着现代网络技术的飞速发展,人们...
Java中常用的加密算法应用,MD5!
详细描述了java常用几种加密算法以及例子
根据密钥类型不同将现代密码技术分为两类:对称加密算法(秘密钥匙加密)和非对称加密算法(公开密钥加密)。 对称钥匙加密系统是加密和解密均采用同一把秘密钥匙,而且通信双方都必须获得这把钥匙,并保持钥匙的...
java常用加密方式.pdf
博文链接:https://llying.iteye.com/blog/171256
一、常用的加密/解密算法 1.Base64 严格来说Base64并不是一种加密/解密算法,而是一种编码方式。Base64不生成密钥,通过Base64编码后的密文就可以直接“翻译”为明文,但是可以通过向明文中添加混淆字符来达到...
Java中常用的加密方法(JDK),对于一些不会使用加密方法的童鞋来说,本文档可能对你有一些启发哦。
1.CLASSLOADER在JAVA加密解密方面的一些研究 2.加密算法 3.java加密技术
常用的包含MD5等的12个加密和冒泡排序工具类以及log4j配置文件
java中常用的工具类,用于MD5的加密,里面还有切割了MD5加密的长度,有DES加密与解密,并且配有main函数可测试,可以根据自己的实际情况使用main函数测试使用
用java编程制作加密的java程序,分有密钥和无秘要,两种方法,很好用,照着写一遍,你也会掌握此2种写法。
Java中常用的加密算法MD5,SHA,RSA
java加密 解密 MD5加密 常用java加密解密算法
java加密算法AES\DES\RSA示例及常用知识学习杂记
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
Java Jct 加密解密工具包源码,WDSsoft免费源代码,java加密解密编程常用工具包 JCT 1.0,作者吴东升 常蕴秋,BlowfishTool.java封装同Blowfish对称加密算法有关的方法,包括了使用创建Blowfish密码,使用Blowfish加密...
常用加密算法的java实现总结,常用加密算法的java实现总结,常用加密算法的java实现总结,常用加密算法的java实现总结,常用加密算法的java实现总结
实现了java中所有常用的加密算法的代码示例,在jdk1.8上测试过,都可以直接运行