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;
}
}
本博客中涉及到的加密解密代码基本兼容 C/C#/JAVA三种平台,可以交叉使用。
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三种平台,可以交叉使用。
相关文章推荐
- Android平台和java平台 DES、3DES、RSA加密解密互通程序及其不能互通的原因
- 3DES在Android、Ios 和Java 平台的加密解密
- 选择C#还是Java归根结底是平台问题
- 四强联手推出开放标准嵌入式Java/Linux 平台
- 关于在linux平台上使用java.awt.*包的设置
- 实现 Java 平台的三种方式
- Windows平台下简单运行Java程序的方法
- BREW、JAVA、.NET三大平台之比较
- Java中,利用语言与平台特性改进RMI分布计算框架
- 论Java技术在因特网平台上的应用—论文1:ERP开发的应用
- 论Java技术在因特网平台上的应用—论文3:银行业的应用
- Java 平台透析
- 利用 Java 平台的特性建造一个令人瞩目的系统
- Merlin 给 Java 平台带来了非阻塞 I/O
- Java能够成为完美的技术平台吗?
- 利用Java平台的特性建造一个令人瞩目的系统
- Java平台调试架构简介
- 利用Axis在Java平台上部署WebService简明教程
- 实现 Java 平台的三种方式
- Java平台组件开发