Java生成3DES密钥 3DES加解密示例 update和doFinal行为 Cipher重复使用

2016-10-01 21:26:00
admin
原创 6293
摘要:Java生成3DES密钥 3DES加解密示例 update和doFinal行为 Cipher重复使用

一、Java生成3DES密钥

1、密钥生成器都有长度和随机源的概念;

2、KeyGenerator.init(int keysize),指定生成密钥的长度;
3、KeyGenerator.init(int keysize, SecureRandom random),指定生成密钥的长度和随机源;


public static void desKeyGen() throws Exception {
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(112);//must be equal to 112 or 168
SecretKey secretKey = kg.generateKey();
System.out.println("format: " + secretKey.getFormat());
byte[] bytes = secretKey.getEncoded();
System.out.println("length: " + bytes.length);
System.out.println("key: " + Hex.encodeHexString(bytes));
}


输出:

format: RAW
length: 24
key: 5eb6fd1afb0ea8a2b9e916c1704fd0295eb6fd1afb0ea8a2


二、3DES加解密示例

1、SecretKeySpec和IvParameterSpec初始化时深度复制数组;

2、代码下载:DesTest.java


import javax.crypto.*;
import javax.crypto.spec.*;
import org.apache.commons.codec.binary.*;

public class DesTest {
private static final String key = "ae3712e83f0831ee8277f795bfed995e";
private static final String iv = "1234567812345678";

private static byte[] build3DesKey(String keyStr) throws Exception {
byte[] key = new byte[24];
byte[] keyBytes = Hex.decodeHex(keyStr.toCharArray());
if(keyBytes.length < key.length) {
System.arraycopy(keyBytes, 0, key, 0, keyBytes.length);
System.arraycopy(keyBytes, 0, key, 16, 8);
} else
System.arraycopy(keyBytes, 0, key, 0, key.length);
return key;
}

private static Cipher getCipher(int mode) throws Exception {
SecretKey deskey = new SecretKeySpec(build3DesKey(key), "DESede");
IvParameterSpec ivObj = new IvParameterSpec(Hex.decodeHex(iv.toCharArray()));
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(mode, deskey, ivObj);
return cipher;
}

public static void main(String[] args) throws Exception {
Cipher encrypt = getCipher(Cipher.ENCRYPT_MODE);
Cipher decrypt = getCipher(Cipher.DECRYPT_MODE);
String content = "secret";
String ciphertext = String.valueOf(Hex.encodeHex(encrypt.doFinal(content.getBytes()))); 
System.out.println(ciphertext);
String plaintext = new String(decrypt.doFinal(Hex.decodeHex(ciphertext.toCharArray())));
System.out.println(plaintext);
}
}


输出:

260a42ebdbde07a5
secret


三、update和doFinal行为

public static void main(String[] args) throws Exception {
Cipher encrypt = getCipher(Cipher.ENCRYPT_MODE);
Cipher decrypt = getCipher(Cipher.DECRYPT_MODE);
SecureRandom random = new SecureRandom();
byte[] bytes = new byte[16];
random.nextBytes(bytes);
System.out.println(Arrays.toString(bytes));

byte[] r1 = encrypt.update(bytes, 0, 0);//没有数据返回null,bytes为null抛出异常。
System.out.println(Arrays.toString(r1));
byte[] r2 = encrypt.update(bytes, 0, 4);//有数据但无法分组,返回空数组。
System.out.println(Arrays.toString(r2));
byte[] r3 = encrypt.update(bytes, 4, 12);
System.out.println(Arrays.toString(r3));
byte[] r4 = encrypt.doFinal();
System.out.println(Arrays.toString(r4));
}


输出:

[68, -68, 121, 91, -74, 118, -80, 112, -99, 18, 107, 84, -113, -63, 5, 21]
null
[]
[-105, -103, -55, 47, 105, -95, 58, 124, 120, -54, 0, -127, -35, 121, 41, 5]
[126, 66, 40, 34, 119, 54, 102, -64]


四、Cipher重复使用

private static Cipher getCipher(int mode) throws Exception {
SecretKey deskey = new SecretKeySpec(build3DesKey(key), Algorithm);
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(mode, deskey, new IvParameterSpec(deskey.getEncoded(), 0, 8));
return cipher;
}

public static void main(String[] args) throws Exception {
Cipher encrypt = getCipher(Cipher.ENCRYPT_MODE);
SecureRandom random = new SecureRandom();
byte[] bytes = new byte[16];
random.nextBytes(bytes);
System.out.println(Arrays.toString(bytes));

System.out.println(Arrays.toString(encrypt.getIV()));
byte[] r1 = encrypt.update(bytes, 0, 16);
System.out.println(Arrays.toString(r1));
byte[] r2 = encrypt.doFinal();
System.out.println(Arrays.toString(r2));

System.out.println(Arrays.toString(encrypt.getIV()));
byte[] r3 = encrypt.update(bytes, 0, 16);
System.out.println(Arrays.toString(r3));
byte[] r4 = encrypt.doFinal();
System.out.println(Arrays.toString(r4));
}


输出:

[109, 107, 115, 60, 17, -84, 23, 18, -33, 71, 106, -5, -113, 41, 54, -41]
[-1, -1, -1, -1, -1, -1, -1, -1]
[-113, 10, -66, -35, 10, -93, -60, 91, -113, -45, -115, 123, 99, 117, 94, -36]
[-46, 58, 9, -92, 9, -57, -75, 68]
[-1, -1, -1, -1, -1, -1, -1, -1]
[-113, 10, -66, -35, 10, -93, -60, 91, -113, -45, -115, 123, 99, 117, 94, -36]
[-46, 58, 9, -92, 9, -57, -75, 68]

发表评论
评论通过审核之后才会显示。