java3des加密解密
有专门方法的,可以参考:
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class TripleDESTest {
public static void main(String[] args) throws Exception {
String text = "kyle boon";
byte[] codedtext = new TripleDESTest().encrypt(text);
String decodedtext = new TripleDESTest().decrypt(codedtext);
System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array
System.out.println(decodedtext); // This correctly shows "kyle boon"
}
public byte[] encrypt(String message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
.getBytes("utf-8"));
final byte[] keyBytes = Arrays.Of(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
final byte[] plainTextBytes = message.getBytes("utf-8");
final byte[] cipherText = cipher.doFinal(plainTextBytes);
// final String encodedCipherText = new sun.misc.BASE64Encoder()
// .encode(cipherText);
return cipherText;
}
public String decrypt(byte[] message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
.getBytes("utf-8"));
final byte[] keyBytes = Arrays.Of(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
// final byte[] encData = new
// sun.misc.BASE64Decoder().decodeBuffer(message);
final byte[] plainText = decipher.doFinal(message);
return new String(plainText, "UTF-8");
}
}
② 如何用Java进行3DES加密解密
Java进行3DES加密解密代码如下:
<preclass="java"name="code">publicstaticStringbyte2hex(byte[]b){
Stringhs="";
Stringstmp="";
for(intn=0;n<b.length;n++){
stmp=Integer.toHexString(b[n]&0xFF);
if(stmp.length()==1)
hs+=("0"+stmp);
else
hs+=stmp;
}
returnhs.toUpperCase();
}</pre><br>
<pre></pre>
<p><br>
3DES的加密密钥长度要求是24个字节,本例中因为给定的密钥只有16个字节,所以需要填补至24个字节。</p>
<p>其中"DESede/ECB/NoPadding",除此之外,3DES还支持"<spanstyle="color:#0000ff">DESede/CBC/PKCS5Padding</span>"模式。</p>
③ java 进行3DES 加密解密
【Java使用3DES加密解密的流程】
①传入共同约定的密钥(keyBytes)以及算法(Algorithm),来构建SecretKey密钥对象
SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
②根据算法实例化Cipher对象。它负责加密/解密
Cipher c1 = Cipher.getInstance(Algorithm);
③传入加密/解密模式以及SecretKey密钥对象,实例化Cipher对象
c1.init(Cipher.ENCRYPT_MODE, deskey);
④传入字节数组,调用Cipher.doFinal()方法,实现加密/解密,并返回一个byte字节数组
c1.doFinal(src);
参考了:http://www.cnblogs.com/lianghuilin/archive/2013/04/15/3des.html
④ java 3des 解密
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。
3DES是三重数据加密,且可以逆推的一种算法方案。但由于3DES的算法是公开的,所以算法本身没什么秘密可言,主要依靠唯一密钥来确保数据加密解密的安全。有人可能会问,那3DES到底安不安全呢?!目前为止,还没有人能破解3DES,所以你要是能破解它,都足以震惊整个信息安全界了……
[Java使用3DES加解密流程]
①传入共同约定的密钥(keyBytes)以及算法(Algorithm),来构建SecretKey密钥对象
SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
②根据算法实例化Cipher对象。它负责加密/解密
Cipher c1 = Cipher.getInstance(Algorithm);
③传入加密/解密模式以及SecretKey密钥对象,实例化Cipher对象
c1.init(Cipher.ENCRYPT_MODE, deskey);
④传入字节数组,调用Cipher.doFinal()方法,实现加密/解密,并返回一个byte字节数组
c1.doFinal(src);