本文目錄一覽:
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);
參考了:
如何用Java進行3DES加密解
public static String encryptKey(String mainKey,String plainKey){ String encryptKey = “”; try{ Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); String Algorithm = “DESede/ECB/NoPadding”; byte[] hb = hex2byte(mainKey.getBytes()); byte[] k = new byte[24]; System.arraycopy(hb,0,k,0,16); System.arraycopy(hb,0,k,16,8); SecretKey deskey = new SecretKeySpec(k, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, deskey); encryptKey = byte2hex(c1.doFinal(hex2byte(plainKey.getBytes()))); }catch(Exception e){ e.printStackTrace(); } return encryptKey; }public static String byte2hex(byte[] b) { String hs = “”; String stmp = “”; for (int n = 0; n b.length; n++) { stmp = Integer.toHexString(b[n] 0xFF); if (stmp.length() == 1) hs += (“0” + stmp); else hs += stmp; } return hs.toUpperCase(); } 3DES的加密密鑰長度要求是24個位元組,本例中因為給定的密鑰只有16個位元組,所以需要填補至24個位元組。
java 3des 密鑰是多少位
3DES演算法是指使用雙長度(16位元組)密鑰K=(KL||KR)將8位元組明文數據塊進行3次DES加密/解密。如下所示: Y = DES(KL)[DES-1(KR)[DES(KL[X])]] 解密方式為: X = DES-1 (KL)[DES (KR)[ DES-1 (KL[Y])]] 其中,DES(KL[X])表示用密鑰K對數據X進行DES加密,DES-1 (KL[Y])表示用密鑰K對數據Y進行解密。 SessionKey的計算採用3DES演算法,計算出單倍長度的密鑰。表示法為:SK = Session(DK,DATA) 3DES加密演算法為: VOID 3DES(BYTE DoubleKeyStr[16], BYTE Data[8], BYTE Out[8]) { BYTE Buf1[8], Buf2[8]; DES (DoubleKeyStr[0], Data, Buf1); UDES(DoubleKeyStr[8], Buf1, Buf2); DES (DoubleKeyStr[0], Buf2, Out); }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/290929.html