當前位置:首頁 » 密碼管理 » java3des加密解密

java3des加密解密

發布時間: 2024-12-17 20:00:25

① 如何用java進行3DES加密

有專門方法的,可以參考:
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);

熱點內容
編譯系統自動生成函數條件 發布:2025-03-05 22:45:20 瀏覽:364
存儲鏡像雙活 發布:2025-03-05 22:44:42 瀏覽:185
sql怎麼建資料庫 發布:2025-03-05 22:39:18 瀏覽:889
javawindows服務 發布:2025-03-05 22:39:16 瀏覽:243
安卓手機如何調和平精英幀數 發布:2025-03-05 22:38:30 瀏覽:909
dnf多玩腳本 發布:2025-03-05 22:31:19 瀏覽:876
如何解鎖sim卡pin密碼 發布:2025-03-05 22:24:47 瀏覽:730
怎麼查看網頁的伺服器 發布:2025-03-05 22:13:05 瀏覽:371
sql解決方案 發布:2025-03-05 22:09:25 瀏覽:563
網吧電腦配置被盜怎麼處理 發布:2025-03-05 21:54:14 瀏覽:500