import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
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 com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;
public
class
util {
public
static
String rsaEncoding(String src,PublicKey pubkey){
try
{
Cipher cip = Cipher.getInstance(
"RSA"
);
cip.init(cip.ENCRYPT_MODE, pubkey);
byte[] by = cip.doFinal(src.getBytes());
return
new
String(BASE64EncoderStream.encode(by));
}
catch
(NoSuchAlgorithmException e) {
throw
new
RuntimeException(e);
}
catch
(NoSuchPaddingException e) {
throw
new
RuntimeException(e);
}
catch
(InvalidKeyException e) {
throw
new
RuntimeException(e);
}
catch
(IllegalBlockSizeException e) {
throw
new
RuntimeException(e);
}
catch
(BadPaddingException e) {
throw
new
RuntimeException(e);
}
}
public
static
String rsaDeEncoding(String sec,PrivateKey privkey){
try
{
Cipher cip = Cipher.getInstance(
"RSA"
);
cip.init(cip.DECRYPT_MODE, privkey);
byte[] by = BASE64DecoderStream.decode(sec.getBytes());
return
new
String(cip.doFinal(by));
}
catch
(NoSuchAlgorithmException e) {
throw
new
RuntimeException(e);
}
catch
(NoSuchPaddingException e) {
throw
new
RuntimeException(e);
}
catch
(InvalidKeyException e) {
throw
new
RuntimeException(e);
}
catch
(IllegalBlockSizeException e) {
throw
new
RuntimeException(e);
}
catch
(BadPaddingException e) {
throw
new
RuntimeException(e);
}
}
public
static
String doubKeyEncoding(String src,String keysrc,String method) {
SecretKey key;
try
{
KeyGenerator kg = KeyGenerator.getInstance(method);
kg.init(
new
SecureRandom(keysrc.getBytes(
"utf-8"
)));
key = kg.generateKey();
Cipher ciph = Cipher.getInstance(method);
ciph.init(Cipher.ENCRYPT_MODE, key);
ciph.update(src.getBytes(
"utf-8"
));
byte[] by = BASE64EncoderStream.encode(ciph.doFinal());
return
new
String(by);
}
catch
(NoSuchAlgorithmException e) {
throw
new
RuntimeException(e);
}
catch
(NoSuchPaddingException e) {
throw
new
RuntimeException(e);
}
catch
(InvalidKeyException e) {
throw
new
RuntimeException(e);
}
catch
(IllegalBlockSizeException e) {
throw
new
RuntimeException(e);
}
catch
(BadPaddingException e) {
throw
new
RuntimeException(e);
}
catch
(UnsupportedEncodingException e) {
throw
new
RuntimeException(e);
}
}
public
static
String doubKeyDencoding(String sec,String keysrc,String method) {
SecretKey key;
try
{
KeyGenerator kg = KeyGenerator.getInstance(method);
kg.init(
new
SecureRandom(keysrc.getBytes(
"utf-8"
)));
key = kg.generateKey();
Cipher ciph = Cipher.getInstance(method);
ciph.init(ciph.DECRYPT_MODE, key);
byte[] by = BASE64DecoderStream.decode(sec.getBytes());
ciph.update(by);
return
new
String(ciph.doFinal());
}
catch
(NoSuchAlgorithmException e) {
throw
new
RuntimeException(e);
}
catch
(NoSuchPaddingException e) {
throw
new
RuntimeException(e);
}
catch
(InvalidKeyException e) {
throw
new
RuntimeException(e);
}
catch
(IllegalBlockSizeException e) {
throw
new
RuntimeException(e);
}
catch
(BadPaddingException e) {
throw
new
RuntimeException(e);
}
catch
(UnsupportedEncodingException e) {
throw
new
RuntimeException(e);
}
}
public
static
String ecodingPasswd(String src,String method){
try
{
MessageDigest md5 = MessageDigest.getInstance(method);
md5.update(src.getBytes());
byte[] encoding = md5.digest();
return
new
String(BASE64EncoderStream.encode(encoding));
}
catch
(NoSuchAlgorithmException e) {
throw
new
RuntimeException(e+
"加密失败!!"
);
}
}
}