public class Encryption {
/**
* DES Data Encryption Standard 数据加密标准
*/
/**
* AES方式加、解密 Advances Encryption Standard 高级加密标准
*/
/**
* RSA DSA算法相同
*/
private KeyGenerator keygen;// 提供对称密钥生成器的功能,支持各种算法
private SecretKey deskey;// 负责保存对称密钥
private Cipher cipher;
private KeyPairGenerator keyPairGen;
private KeyPair keyPair;
private RSAPrivateKey privateKey;
private RSAPublicKey publicKey;
private byte[] cipherByte;
public Encryption(String encrypType) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
if(encrypType!=null && ("AES".equals(encrypType)|| "DES".equals(encrypType)|| "DESede".equals(encrypType)))
{
keygen = KeyGenerator.getInstance(encrypType);
deskey = keygen.generateKey();
cipher = Cipher.getInstance(encrypType);
}
else if(encrypType!=null && ("RSA").equals(encrypType) || "DSA".equals(encrypType))
{
keyPairGen = KeyPairGenerator.getInstance(encrypType);
keyPairGen.initialize(1024);
keyPair = keyPairGen.generateKeyPair();
if("RSA".equals(encrypType))
{
privateKey = (RSAPrivateKey) keyPair.getPrivate();
publicKey = (RSAPublicKey) keyPair.getPublic();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* DES AES DESede加密
*
* @param str
* @return
*/
public byte[] createEncryptor(String str) {
try {
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] src = str.getBytes();
cipherByte = cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return cipherByte;
}
/**
* DES AES DESede 方式解密
*
* @param buff
* @return
*/
public byte[] createDecryptor(byte[] buff) {
try {
cipher.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = cipher.doFinal(buff);
} catch (Exception e) {
e.printStackTrace();
}
return cipherByte;
}
private byte[] encryptRSA(byte[] srcBytes) {
byte[] resultByte = null;
try {
if (publicKey != null) {
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
resultByte = cipher.doFinal(srcBytes);
}
} catch (Exception e) {
e.printStackTrace();
}
return resultByte;
}
private byte[]decryptRSA(byte[] encBytes)
{
byte[] resultByte = null;
try {
if(privateKey!=null)
{
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
resultByte=cipher.doFinal(encBytes);
}
} catch (Exception e) {
}
return resultByte;
}
/**
* MD5的加密 SHA一样
*/
public byte[] encryptMD5 (String msg)
{
try {
MessageDigest md5=MessageDigest.getInstance("MD5");
byte[]srcBytes=msg.getBytes();
md5.update(srcBytes);
byte[]resultBytes=md5.digest();
return resultBytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* DES Data Encryption Standard 数据加密标准
*/
/**
* AES方式加、解密 Advances Encryption Standard 高级加密标准
*/
/**
* RSA DSA算法相同
*/
private KeyGenerator keygen;// 提供对称密钥生成器的功能,支持各种算法
private SecretKey deskey;// 负责保存对称密钥
private Cipher cipher;
private KeyPairGenerator keyPairGen;
private KeyPair keyPair;
private RSAPrivateKey privateKey;
private RSAPublicKey publicKey;
private byte[] cipherByte;
public Encryption(String encrypType) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
if(encrypType!=null && ("AES".equals(encrypType)|| "DES".equals(encrypType)|| "DESede".equals(encrypType)))
{
keygen = KeyGenerator.getInstance(encrypType);
deskey = keygen.generateKey();
cipher = Cipher.getInstance(encrypType);
}
else if(encrypType!=null && ("RSA").equals(encrypType) || "DSA".equals(encrypType))
{
keyPairGen = KeyPairGenerator.getInstance(encrypType);
keyPairGen.initialize(1024);
keyPair = keyPairGen.generateKeyPair();
if("RSA".equals(encrypType))
{
privateKey = (RSAPrivateKey) keyPair.getPrivate();
publicKey = (RSAPublicKey) keyPair.getPublic();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* DES AES DESede加密
*
* @param str
* @return
*/
public byte[] createEncryptor(String str) {
try {
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] src = str.getBytes();
cipherByte = cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return cipherByte;
}
/**
* DES AES DESede 方式解密
*
* @param buff
* @return
*/
public byte[] createDecryptor(byte[] buff) {
try {
cipher.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = cipher.doFinal(buff);
} catch (Exception e) {
e.printStackTrace();
}
return cipherByte;
}
private byte[] encryptRSA(byte[] srcBytes) {
byte[] resultByte = null;
try {
if (publicKey != null) {
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
resultByte = cipher.doFinal(srcBytes);
}
} catch (Exception e) {
e.printStackTrace();
}
return resultByte;
}
private byte[]decryptRSA(byte[] encBytes)
{
byte[] resultByte = null;
try {
if(privateKey!=null)
{
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
resultByte=cipher.doFinal(encBytes);
}
} catch (Exception e) {
}
return resultByte;
}
/**
* MD5的加密 SHA一样
*/
public byte[] encryptMD5 (String msg)
{
try {
MessageDigest md5=MessageDigest.getInstance("MD5");
byte[]srcBytes=msg.getBytes();
md5.update(srcBytes);
byte[]resultBytes=md5.digest();
return resultBytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}