1. 程式人生 > >java:三重des加密中明文、密文長度

java:三重des加密中明文、密文長度

對長度為7的位元組陣列加密,解密輸出結果: 
Java程式碼  收藏程式碼
  1. 原明文byte[]長度:7   相應的16進位制字串值:0123456789abcd  
  2. 加密後byte[]長度:8   相應的16進位制字串值:19dffce951d8c37d  
  3. 解密後byte[]長度:7   相應的16進位制字串值:0123456789abcd  


對長度為8的位元組陣列加密,解密輸出結果: 
Java程式碼  收藏程式碼
  1. 原明文byte[]長度:8   相應的16進位制字串值:0123456789abcdef  
  2. 加密後byte[]長度:16  相應的16進位制字串值:bb93c15e93aafe01c15629bc63a3c3c8  
  3. 解密後byte
    []長度:8   相應的16進位制字串值:0123456789abcdef  


以下是原始碼(也可以下載附件裡的原始碼),程式碼複製即可執行.期待您的幫助 
Java程式碼  收藏程式碼
  1. import java.security.Key;  
  2. import javax.crypto.Cipher;  
  3. import javax.crypto.KeyGenerator;  
  4. import javax.crypto.SecretKey;  
  5. public class DESedeCoderTest {  
  6.     public static void main(String[] args) throws
     Exception {  
  7.         String sHexPlainText = "0123456789abcdef";  
  8.         SecretKey skSecretkey=(SecretKey)TDESCoder.generateKey(112);  
  9.         byte[] byteaPlainText = hexStr2ByteArr(sHexPlainText);  
  10.         byte[] byteaCryptograph = TDESCoder.enc(byteaPlainText, skSecretkey);  
  11.         byte[] byteaPlainTextAftDec = TDESCoder.dec(byteaCryptograph, skSecretkey);  
  12.         System.out.println("原明文byte[]長度:"+byteaPlainText.length+"\t相應的16進位制字串值:"+byteArr2HexStr(byteaPlainText));  
  13.         System.out.println("加密後byte[]長度:"+byteaCryptograph.length+"\t相應的16進位制字串值:"+byteArr2HexStr(byteaCryptograph));  
  14.         System.out.println("解密後byte[]長度:"+byteaPlainTextAftDec.length+"\t相應的16進位制字串值:"+byteArr2HexStr(byteaPlainTextAftDec));  
  15.     }  
  16.     public static String byteArr2HexStr(byte[] bytea) throws Exception {  
  17.         String sHex = "";  
  18.         int iUnsigned = 0;  
  19.         StringBuffer sbHex = new StringBuffer();  
  20.         for (int i = 0; i < bytea.length; i++) {  
  21.             iUnsigned = bytea[i];  
  22.             if (iUnsigned < 0) {  
  23.                 iUnsigned += 256;  
  24.             }  
  25.             if (iUnsigned < 16) {  
  26.                 sbHex.append("0");  
  27.             }  
  28.             sbHex.append(Integer.toString(iUnsigned, 16));  
  29.         }  
  30.         sHex = sbHex.toString();  
  31.         return sHex;  
  32.     }  
  33.     public static byte[] hexStr2ByteArr(String sHex) throws Exception {  
  34.         if(sHex.length()%2!=0){  
  35.             sHex="0"+sHex;  
  36.         }  
  37.         byte[] bytea =bytea=new byte[sHex.length() / 2];  
  38.         String sHexSingle = "";  
  39.         for (int i = 0; i < bytea.length; i++) {  
  40.             sHexSingle = sHex.substring(i * 2, i * 2 + 2);  
  41.             bytea[i] = (byte) Integer.parseInt(sHexSingle, 16);  
  42.         }  
  43.         return bytea;  
  44.     }  
  45. }  
  46. class TDESCoder {  
  47.     private static final String S_KEY_ALGORITHM = "DESede";  
  48.     private static final String S_CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";  
  49.     private static SecretKey skSecretkey;  
  50.     public static byte[] enc(byte[] byteaPlainText,SecretKey skSecretkey) throws Exception {  
  51.         Cipher cipher = Cipher.getInstance(S_CIPHER_ALGORITHM);  
  52.         cipher.init(Cipher.ENCRYPT_MODE, skSecretkey);  
  53.         byte[] byteaCryptograph=cipher.doFinal(byteaPlainText);  
  54.         return byteaCryptograph;  
  55.     }  
  56.     public static byte[] dec(byte[] byteaCryptograph,SecretKey skSecretkey) throws Exception {  
  57.         Cipher cCipher = Cipher.getInstance(S_CIPHER_ALGORITHM);  
  58.         cCipher.init(Cipher.DECRYPT_MODE, skSecretkey);  
  59.         byte[] byteaPlainText=cCipher.doFinal(byteaCryptograph);  
  60.         return byteaPlainText;  
  61.     }  
  62.     public static Key generateKey(int iBits) throws Exception {  
  63.         iBits=112;  
  64.         KeyGenerator kg = KeyGenerator.getInstance(S_KEY_ALGORITHM);  
  65.         kg.init(iBits);  
  66.         skSecretkey = kg.generateKey();  
  67.         return   skSecretkey;  
  68.     }  
  69. }