tripletest.java
来自「通过java方式实现的DESEDE加密算法,其中包括选区择不同的模式与填充方式进」· Java 代码 · 共 311 行
JAVA
311 行
package com.des.test;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TripleTest
{
public static void main(String[] args)
{
TripleTest tripleTest = new TripleTest();
String key_string = "123400000000000000000000";
String src_string = "12345678";
String des_string = "";
String information = "DESEDE";// "DESEDE/ECB/PKCS5Padding";
String information2 = "DESEDE/CBC/PKCS5Padding";
try
{
SecretKey key = tripleTest.getDESKey(key_string);
SecureRandom random = new SecureRandom();
byte[] iv = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00 };
iv = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
(byte) 0, (byte) 0, (byte) 0 };
byte[] vi = new byte[8];
byte[] tt = new byte[8];
int COUNT = 0;
// while (!"Brsl0etq4fQ1fVmYgeeVdw==".equals(des_string))
// {
System.out.println("**********************" + (++COUNT)
+ "***********************");
random.nextBytes(vi);
IvParameterSpec iv_param_spec = new IvParameterSpec(iv);
// 加密
Cipher cipher1 = Cipher.getInstance(information);
cipher1.init(Cipher.ENCRYPT_MODE, key);
byte[] des_byte = cipher1.doFinal(src_string.getBytes());
System.out.println("src_string.getBytes()"
+ src_string.getBytes().length + "\n加密后数组长度des_byte:"
+ des_byte.length);
des_string = tripleTest.encodeBase64(des_byte);
System.out.println("des_string:" + des_string);
if ("Brsl0etq4fQ1fVmYgeeVdw==".equals(des_string))
{
System.out.println("找到正确答案了!!!!!!!!!!!!!!");
for (int i = 0; i < vi.length; i++)
{
System.out.println("vi[" + i + "]:" + vi[i]);
}
}
// CBC加密
Cipher cipher3 = Cipher.getInstance(information2);
cipher3.init(Cipher.ENCRYPT_MODE, key, iv_param_spec);
byte[] des_byte_cbc = cipher3.doFinal(src_string.getBytes());
/*
* String ttt = StringBytesTransformUtils
* .bytesToHexString(des_byte_cbc); System.out.println("tttttttt:" +
* ttt); byte[] ss =
* StringBytesTransformUtils.hexStringToBytes(ttt);
* System.out.println("ss" + new String(ss));
*/
System.out.println(des_byte_cbc.length);
String des_string_cbc = tripleTest.encodeBase64(des_byte_cbc);
System.out.println("des_string_cbc:" + des_string_cbc);
// 解密
cipher1.init(Cipher.DECRYPT_MODE, key);
des_byte = tripleTest.decodeBase64(des_string);
byte[] src_byte = cipher1.doFinal(des_byte);
src_string = new String(src_byte);
System.out.println("src_string:" + src_string);
String tripe = tripleTest.encryptString(src_string, key_string
+ "00000000000000000000");
System.out.println("tripe:::" + tripe);
// }
} catch (InvalidKeyException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String encodeBase64(byte[] src_byte)
{
BASE64Encoder base64Encoder = new BASE64Encoder();
try
{
// 经过BASE64加密后的密文
String base64String = base64Encoder.encodeBuffer(src_byte);
String a = base64Encoder.encodeBuffer(src_byte);
String b = base64Encoder.encode(src_byte);
System.out.println("a:" + a + "b:" + b);
return base64String;
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static byte[] decodeBase64(String base64_string)
{
BASE64Decoder base64Decoder = new BASE64Decoder();
try
{
// 将BASE64转码过的字符串进行解码,获取明文
byte[] src_byte = base64Decoder.decodeBuffer(base64_string);
return src_byte;
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public SecretKey getKey(String key_String) throws InvalidKeyException,
NoSuchAlgorithmException, InvalidKeySpecException
{
SecretKey key = null;
byte[] key_chars = key_String.getBytes();
int key_length = 0;
byte[] newchars = new byte[24];
if (key_chars.length < 24)
{
key_length = 24;
for (int i = 0; i < key_chars.length; i++)
{
newchars[i] = key_chars[i];
}
for (int j = key_chars.length; j < key_length; j++)
{
newchars[j] = '0';
}
}
DESedeKeySpec dks = new DESedeKeySpec(newchars);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
key = keyFactory.generateSecret(dks);
return key;
}
public static SecretKey getDESKey(String key_string)
throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException
{
SecretKey key = null;
byte[] key_byte = null;
// 判断密钥的长度,如果不是24位,则以"0"补齐
String zeros = "000000000000000000000000";
if (key_string != null)
{
int keylength = key_string.getBytes().length;
if (keylength < 24)
{
key_string += zeros.substring(keylength);
}
} else
{
return null;
}
char[] gg = key_string.toCharArray();
for (int i = 0; i < gg.length; i++)
{
// gg[i] -= 18;
System.out.println((byte) gg[i]);
}
System.out.println("gg[i]:" + new String(gg));
// key_string = encodeBase64(key_string.getBytes());
key_byte = key_string.getBytes();
for (int i = 0; i < key_byte.length; i++)
{
key_byte[i] -= 18;
System.out.println(key_byte[i]);
}
byte[] kkey_byte = new byte[] { (byte) 0x49, (byte) 0x50, (byte) 0x51,
(byte) 0x52, (byte) 0x48, (byte) 0x48, (byte) 0x48,
(byte) 0x48, (byte) 0x48, (byte) 0x48, (byte) 0x48,
(byte) 0x48, (byte) 0x48, (byte) 0x48, (byte) 0x48,
(byte) 0x48, (byte) 0x48, (byte) 0x48, (byte) 0x48,
(byte) 0x48, (byte) 0x48, (byte) 0x48, (byte) 0x48,
(byte) 0x48, (byte) 0x48, (byte) 0x48, (byte) 0x48 };
key_byte = new byte[] { (byte) 0x01, (byte) 0x02, (byte) 0x03,
(byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
key_byte = new byte[] { (byte) 0x31, (byte) 0x32, (byte) 0x33,
(byte) 0x34, (byte) 0x30, (byte) 0x30, (byte) 0x30,
(byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30,
(byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30,
(byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30,
(byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30,
(byte) 0x30, };
System.out.println("new key_string::" + new String(key_byte));
String base_keystring = encodeBase64(key_string.getBytes());
System.out.println("base_keystring:" + base_keystring);
byte[] base_keybyte = base_keystring.getBytes();
System.out.println("base_keybyte.length:" + base_keybyte.length);
for (int i = 0; i < base_keybyte.length; i++)
{
System.out.println("base_keybyte[" + i + "]" + base_keybyte[i]);
}
DESedeKeySpec dks = new DESedeKeySpec(key_string.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
key = keyFactory.generateSecret(dks);
return key;
}
public String encryptString(String rawStr, String rawKey)
throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException,
BadPaddingException, IllegalBlockSizeException,
IllegalStateException, IOException
{
byte rawStrData[] = rawStr.getBytes();
byte rawKeyData[] = rawKey.getBytes();
byte encryptData[] = encryptString(rawStrData, rawKeyData);
String encryptStr = (new BASE64Encoder()).encode(encryptData);
System.out.println("encryptStr:" + encryptStr);
byte c[] = encryptStr.getBytes();
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < c.length; i++)
{
int t = c[i];
String str = Integer.toHexString(t);
if (str.length() == 1)
strBuf.append(0);
strBuf.append(Integer.toHexString(t));
}
return strBuf.toString();
}
private byte[] encryptString(byte data[], byte rawKeyData[])
throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException,
BadPaddingException, IllegalBlockSizeException,
IllegalStateException
{
DESedeKeySpec dks = new DESedeKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESEDE");
javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte encryptedData[] = cipher.doFinal(data);
return encryptedData;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?