对长度为7的字节数组加密,解密输出结果:
对长度为8的字节数组加密,解密输出结果:
以下是源代码(也可以下载附件里的源码),代码复制即可执行.期待您的帮助
- 原明文byte[]长度:7 相应的16进制字符串值:0123456789abcd
- 加密后byte[]长度:8 相应的16进制字符串值:19dffce951d8c37d
- 解密后byte[]长度:7 相应的16进制字符串值:0123456789abcd
对长度为8的字节数组加密,解密输出结果:
- 原明文byte[]长度:8 相应的16进制字符串值:0123456789abcdef
- 加密后byte[]长度:16 相应的16进制字符串值:bb93c15e93aafe01c15629bc63a3c3c8
- 解密后byte[]长度:8 相应的16进制字符串值:0123456789abcdef
以下是源代码(也可以下载附件里的源码),代码复制即可执行.期待您的帮助
- import java.security.Key;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- public class DESedeCoderTest {
- public static void main(String[] args) throws Exception {
- String sHexPlainText = "0123456789abcdef";
- SecretKey skSecretkey=(SecretKey)TDESCoder.generateKey(112);
- byte[] byteaPlainText = hexStr2ByteArr(sHexPlainText);
- byte[] byteaCryptograph = TDESCoder.enc(byteaPlainText, skSecretkey);
- byte[] byteaPlainTextAftDec = TDESCoder.dec(byteaCryptograph, skSecretkey);
- System.out.println("原明文byte[]长度:"+byteaPlainText.length+"\t相应的16进制字符串值:"+byteArr2HexStr(byteaPlainText));
- System.out.println("加密后byte[]长度:"+byteaCryptograph.length+"\t相应的16进制字符串值:"+byteArr2HexStr(byteaCryptograph));
- System.out.println("解密后byte[]长度:"+byteaPlainTextAftDec.length+"\t相应的16进制字符串值:"+byteArr2HexStr(byteaPlainTextAftDec));
- }
- public static String byteArr2HexStr(byte[] bytea) throws Exception {
- String sHex = "";
- int iUnsigned = 0;
- StringBuffer sbHex = new StringBuffer();
- for (int i = 0; i < bytea.length; i++) {
- iUnsigned = bytea[i];
- if (iUnsigned < 0) {
- iUnsigned += 256;
- }
- if (iUnsigned < 16) {
- sbHex.append("0");
- }
- sbHex.append(Integer.toString(iUnsigned, 16));
- }
- sHex = sbHex.toString();
- return sHex;
- }
- public static byte[] hexStr2ByteArr(String sHex) throws Exception {
- if(sHex.length()%2!=0){
- sHex="0"+sHex;
- }
- byte[] bytea =bytea=new byte[sHex.length() / 2];
- String sHexSingle = "";
- for (int i = 0; i < bytea.length; i++) {
- sHexSingle = sHex.substring(i * 2, i * 2 + 2);
- bytea[i] = (byte) Integer.parseInt(sHexSingle, 16);
- }
- return bytea;
- }
- }
- class TDESCoder {
- private static final String S_KEY_ALGORITHM = "DESede";
- private static final String S_CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
- private static SecretKey skSecretkey;
- public static byte[] enc(byte[] byteaPlainText,SecretKey skSecretkey) throws Exception {
- Cipher cipher = Cipher.getInstance(S_CIPHER_ALGORITHM);
- cipher.init(Cipher.ENCRYPT_MODE, skSecretkey);
- byte[] byteaCryptograph=cipher.doFinal(byteaPlainText);
- return byteaCryptograph;
- }
- public static byte[] dec(byte[] byteaCryptograph,SecretKey skSecretkey) throws Exception {
- Cipher cCipher = Cipher.getInstance(S_CIPHER_ALGORITHM);
- cCipher.init(Cipher.DECRYPT_MODE, skSecretkey);
- byte[] byteaPlainText=cCipher.doFinal(byteaCryptograph);
- return byteaPlainText;
- }
- public static Key generateKey(int iBits) throws Exception {
- iBits=112;
- KeyGenerator kg = KeyGenerator.getInstance(S_KEY_ALGORITHM);
- kg.init(iBits);
- skSecretkey = kg.generateKey();
- return skSecretkey;
- }
- }
2011年2月27日 14:46