您的位置:首页 > 编程语言 > Java开发

3DES JAVA平台版

2015-03-11 14:36 92 查看
对数据安全方面的介绍在本博客也很多了,这里就不再累赘,直接上代码:

package base_crypt;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class base_crypt {

/*********************************************
* ecb_base_encrypt("AES/ECB/PKCS5Padding","AES",s_buf,p_pass);
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*********************************************/
protected static byte[] ecb_base_encrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass) {
try{
Cipher cipher = Cipher.getInstance(p_formation);
SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
cipher.init(Cipher.ENCRYPT_MODE,keyspec);
byte[] d_buf = cipher.doFinal(s_buf);
return d_buf;
} catch (InvalidKeyException | NoSuchAlgorithmException
| NoSuchPaddingException | IllegalBlockSizeException
| BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/*********************************************
* ecb_base_decrypt("AES/ECB/PKCS5Padding","AES",s_buf,p_pass);
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*********************************************/
protected static byte[] ecb_base_decrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass) {

try{
Cipher cipher = Cipher.getInstance(p_formation);
SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
cipher.init(Cipher.DECRYPT_MODE,keyspec);
byte[] d_buf = cipher.doFinal(s_buf);
return d_buf;
} catch (InvalidKeyException | NoSuchAlgorithmException
| NoSuchPaddingException | IllegalBlockSizeException
| BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/*********************************************
* base_encrypt("AES/CBC/PKCS5Padding","AES",s_buf,p_pass,p_iv);
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*********************************************/
protected static byte[] base_encrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass,byte[] p_iv) {

try{
Cipher cipher = Cipher.getInstance(p_formation);
SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
IvParameterSpec iv = new IvParameterSpec(p_iv);
cipher.init(Cipher.ENCRYPT_MODE,keyspec,iv);
byte[] d_buf = cipher.doFinal(s_buf);
return d_buf;
} catch (InvalidKeyException | NoSuchAlgorithmException
| NoSuchPaddingException | IllegalBlockSizeException
| BadPaddingException | InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/*********************************************
* base_decrypt("AES/CBC/PKCS5Padding","AES",s_buf,p_pass,p_iv);
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*********************************************/
protected static byte[] base_decrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass,byte[] p_iv){
try{
Cipher cipher = Cipher.getInstance(p_formation);
SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
IvParameterSpec iv = new IvParameterSpec(p_iv);
cipher.init(Cipher.DECRYPT_MODE,keyspec,iv);
byte[] d_buf = cipher.doFinal(s_buf);
return d_buf;
} catch (InvalidKeyException | NoSuchAlgorithmException
| NoSuchPaddingException | IllegalBlockSizeException
| BadPaddingException | InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

package base_crypt;

public class base_triple_des extends base_crypt{

/* this class just provide the interface that crypt algorithm which paddinged with PKCS5Padding.*/

private static String triple_des_mode_ecb_pkcs5 = "DESede/ECB/PKCS5Padding";	// mode ECB/CBC/OFB/CFB/CTR
private static String triple_des_mode_cbc_pkcs5 = "DESede/CBC/PKCS5Padding";
//private static String triple_des_mode_cfb_pkcs5 = "DESede/CFB/PKCS5Padding";	// java jdk is not surport
//private static String triple_des_mode_ofb_pkcs5 = "DESede/OFB/PKCS5Padding";	// java jdk is not surport
//private static String triple_des_mode_ctr_pkcs5 = "DESede/CTR/PKCS5Padding";	// java jdk is not surport

private static String triple_des_algorithm = "DESede";

private static String triple_des_key_padding_str = "1234567890ABCDEFASDFGHJK";
private static String triple_des_iv_padding_str = "ABCDEFGH12345678";

private static byte[] default_iv = { 0x12, 0x34, 0x56, 0x78,
(byte) Integer.parseInt("90", 16),
(byte) Integer.parseInt("AB", 16),
(byte) Integer.parseInt("CD", 16),
(byte) Integer.parseInt("EF", 16)};

private static byte[] key_generator(String p_key){
byte[] key = (p_key +triple_des_key_padding_str).substring(0, 24).getBytes();
return key;
}
private static byte[] iv_generator(String p_iv){
byte[] iv = (p_iv + triple_des_iv_padding_str).substring(0,8).getBytes();
return iv;
}

public static byte[] ecb_triple_des_encrypt(byte[] s_buf,byte[] p_pass){
return ecb_base_encrypt(triple_des_mode_ecb_pkcs5, triple_des_algorithm,s_buf, p_pass);
}
public static byte[] ecb_triple_des_decrypt(byte[] s_buf,byte[] p_pass){
return ecb_base_decrypt(triple_des_mode_ecb_pkcs5, triple_des_algorithm, s_buf, p_pass);
}
public static byte[] ecb_triple_des_encrypt(byte[] s_buf,String p_pass) {
byte[] p_key = key_generator(p_pass);
return ecb_base_encrypt(triple_des_mode_ecb_pkcs5, triple_des_algorithm, s_buf, p_key);
}
public static byte[] ecb_triple_des_decrypt(byte[] s_buf,String p_pass){

byte[] p_key = key_generator(p_pass);
return ecb_base_decrypt(triple_des_mode_ecb_pkcs5, triple_des_algorithm, s_buf, p_key);
}

public static byte[] triple_des_cbc_encrypt(byte[] s_buf,byte[] p_pass,byte[] p_iv){
return base_encrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, p_pass, p_iv);
}
public static byte[] triple_des_cbc_decrypt(byte[] s_buf,byte[] p_pass,byte[] p_iv) {
return base_decrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, p_pass, p_iv);
}
public static byte[] triple_des_cbc_encrypt(byte[] s_buf,byte[] p_pass){
return base_encrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, p_pass, default_iv);
}
public static byte[] triple_des_cbc_decrypt(byte[] s_buf,byte[] p_pass){
return base_decrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, p_pass, default_iv);
}
public static byte[] triple_des_cbc_encrypt(byte[] s_buf,String p_pass,String p_iv){
byte[] key = key_generator(p_pass);
byte[] iv = iv_generator(p_iv);
return base_encrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, key, iv);
}
public static byte[] triple_des_cbc_decrypt(byte[] s_buf,String p_pass,String p_iv){
byte[] key = key_generator(p_pass);
byte[] iv = iv_generator(p_iv);
return base_decrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, key, iv);
}
public static byte[] triple_des_cbc_encrypt(byte[] s_buf,String p_pass){
byte[] p_key = key_generator(p_pass);
return base_encrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, p_key,default_iv);
}
public static byte[] triple_des_cbc_decrypt(byte[] s_buf,String p_pass){
byte[] p_key = key_generator(p_pass);
return base_decrypt(triple_des_mode_cbc_pkcs5, triple_des_algorithm, s_buf, p_key,default_iv);
}

}


本博客中涉及到的加密解密代码基本兼容 C/C#/JAVA三种平台,可以交叉使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息