java加密

package com.changhong.invitebid.encrypt;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;

/**
* 加解密 ,2008.06.20, by zeng-sy
*/
public class Encrypt {

/**
* 生成密钥
* 返回byte[]型密钥
*
* by zeng-sy
*
* @throws NoSuchAlgorithmException
* @return byte[]
*/
public byte[] createKey() throws NoSuchAlgorithmException {

KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
keygenerator.init(56);
// 获得密钥
SecretKey secretkey = keygenerator.generateKey();
// 获得密钥编码格式,返回二进制字节数组,以便将密钥显示
byte[] key = secretkey.getEncoded();

return key;
}

/**
* 类型转换器 按照BASE64转换byte[]为String提交给用户
* 参数:byte[]型 bte
* 返回转换成String型的密钥
*
* by zeng-sy
*
* @return String
*/
public String byteToStringByBASE64(byte[] bte) {
// 将byte数据转换为BASE64,再转换为String
String str = new sun.misc.BASE64Encoder().encode(bte);

return str;
}

/**
* 类型转换器 BASE64转换String 返回转换后的byte[]
*
* by zeng-sy
*
* @throws IOException
* @return byte[]
*/
public byte[] stringToByteByBASE64(String str) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
byte[] bt = decoder.decodeBuffer(str);
return bt;
}

/**
* 加密文本数据 参数:(byte[]密钥,String加密数据) 返回加密后 密文 byte[]
*
* by zeng-sy
*
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @return byte[]
*/
public byte[] doEncryptText(byte[] key, String str)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, UnsupportedEncodingException,
IllegalBlockSizeException, BadPaddingException {
SecretKey secretkey = new SecretKeySpec(key, "DES");
// 创建要求的加密对象:加密算法DES
Cipher cipher = Cipher.getInstance("DES");
// 加密对象初始化
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
// 密文存放在字节数组encrypted 中
byte[] strbyte = str.getBytes("UTF8");
byte[] encrypted = cipher.doFinal(strbyte);
return encrypted;
}

/**
* 加密附件 参数:(byte[]密钥,byte[]加密附件数据) 返回加密后 密文 byte[]
*
* by zeng-sy
*
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @return byte[]
*/
public byte[] doEncryptAttachment(byte[] key, byte[] str)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, UnsupportedEncodingException,
IllegalBlockSizeException, BadPaddingException {
SecretKey secretkey = new SecretKeySpec(key, "DES");
// 创建要求的加密对象:加密算法DES
Cipher cipher = Cipher.getInstance("DES");
// 加密对象初始化
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
// 密文存放在字节数组encrypted 中
byte[] encrypted = cipher.doFinal(str);
return encrypted;
}

/**
* 解密文本数据 参数:(byte[]密文,byte[]密钥)
* 返回加密后的byte[]型 密文
*
* by zeng-sy
*
* @return byte[]
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public String unDoEncryptText(byte[] encrypted, byte[] key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
SecretKey secretkey = new SecretKeySpec(key, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretkey);
// 解密后的原文存放在字节数组decrypted 中
byte[] decrypted = cipher.doFinal(encrypted);
// 解密后的原文转换为String
String str = new String(decrypted);
return str;
}

/**
* 解密附件 参数:(byte[]密文,byte[]密钥)
* 返回加密后的byte[]型 密文
*
* by zeng-sy
*
* @return byte[]
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public byte[] unDoEncryptAttachment(byte[] encrypted, byte[] key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
SecretKey secretkey = new SecretKeySpec(key, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretkey);
// 解密后的原文存放在字节数组decrypted 中
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

}

你可能感兴趣的:(java,算法,Security,sun)