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

本文提供了一个使用DESEDE算法进行数据加密和解密的Java代码示例。展示了不同长度字节数组加密后的变化及如何通过相同的密钥进行解密还原。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对长度为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.   
  6. public class DESedeCoderTest {  
  7.     public static void main(String[] args) throws Exception {  
  8.         String sHexPlainText = "0123456789abcdef";  
  9.         SecretKey skSecretkey=(SecretKey)TDESCoder.generateKey(112);  
  10.         byte[] byteaPlainText = hexStr2ByteArr(sHexPlainText);  
  11.         byte[] byteaCryptograph = TDESCoder.enc(byteaPlainText, skSecretkey);  
  12.         byte[] byteaPlainTextAftDec = TDESCoder.dec(byteaCryptograph, skSecretkey);  
  13.         System.out.println("原明文byte[]长度:"+byteaPlainText.length+"\t相应的16进制字符串值:"+byteArr2HexStr(byteaPlainText));  
  14.         System.out.println("加密后byte[]长度:"+byteaCryptograph.length+"\t相应的16进制字符串值:"+byteArr2HexStr(byteaCryptograph));  
  15.         System.out.println("解密后byte[]长度:"+byteaPlainTextAftDec.length+"\t相应的16进制字符串值:"+byteArr2HexStr(byteaPlainTextAftDec));  
  16.     }  
  17.     public static String byteArr2HexStr(byte[] bytea) throws Exception {  
  18.         String sHex = "";  
  19.         int iUnsigned = 0;  
  20.         StringBuffer sbHex = new StringBuffer();  
  21.         for (int i = 0; i < bytea.length; i++) {  
  22.             iUnsigned = bytea[i];  
  23.             if (iUnsigned < 0) {  
  24.                 iUnsigned += 256;  
  25.             }  
  26.             if (iUnsigned < 16) {  
  27.                 sbHex.append("0");  
  28.             }  
  29.             sbHex.append(Integer.toString(iUnsigned, 16));  
  30.         }  
  31.         sHex = sbHex.toString();  
  32.         return sHex;  
  33.     }  
  34.   
  35.   
  36.     public static byte[] hexStr2ByteArr(String sHex) throws Exception {  
  37.           
  38.         if(sHex.length()%2!=0){  
  39.             sHex="0"+sHex;  
  40.         }  
  41.         byte[] bytea =bytea=new byte[sHex.length() / 2];  
  42.           
  43.         String sHexSingle = "";  
  44.         for (int i = 0; i < bytea.length; i++) {  
  45.             sHexSingle = sHex.substring(i * 2, i * 2 + 2);  
  46.             bytea[i] = (byte) Integer.parseInt(sHexSingle, 16);  
  47.         }  
  48.         return bytea;  
  49.     }  
  50.        
  51. }  
  52. class TDESCoder {  
  53.     private static final String S_KEY_ALGORITHM = "DESede";  
  54.     private static final String S_CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";  
  55.     private static SecretKey skSecretkey;  
  56.     public static byte[] enc(byte[] byteaPlainText,SecretKey skSecretkey) throws Exception {  
  57.         Cipher cipher = Cipher.getInstance(S_CIPHER_ALGORITHM);  
  58.         cipher.init(Cipher.ENCRYPT_MODE, skSecretkey);  
  59.         byte[] byteaCryptograph=cipher.doFinal(byteaPlainText);  
  60.         return byteaCryptograph;  
  61.     }  
  62.       
  63.     public static byte[] dec(byte[] byteaCryptograph,SecretKey skSecretkey) throws Exception {  
  64.         Cipher cCipher = Cipher.getInstance(S_CIPHER_ALGORITHM);  
  65.         cCipher.init(Cipher.DECRYPT_MODE, skSecretkey);  
  66.         byte[] byteaPlainText=cCipher.doFinal(byteaCryptograph);  
  67.         return byteaPlainText;  
  68.     }  
  69.   
  70.     public static Key generateKey(int iBits) throws Exception {  
  71.         iBits=112;  
  72.         KeyGenerator kg = KeyGenerator.getInstance(S_KEY_ALGORITHM);  
  73.         kg.init(iBits);  
  74.         skSecretkey = kg.generateKey();  
  75.         return   skSecretkey;  
  76.     }  
  77. }  
2011年2月27日 14:46

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值