13,097
社区成员




package com.infoservice.dms.chana.common;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @author oubocheng
*
*/
public class EncryptionAndDecryption {
/********************** 对称加密 *****************************/
/**将字符串加密成可解密但不可视的密文
* 对字符串加密
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public String Encrytor(String str, String secretKey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, keygen.generateKey());
byte[] src = str.getBytes();
// 加密,结果保存进cipherByte
byte[] cipherByte = c.doFinal(src);
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(cipherByte);
return str_;
}
/**
* 对字符串解密
*
* @param buff
* @return
* @throws Exception
*/
public String Decryptor(String str, String secretKey) throws Exception {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, keygen.generateKey());
//base64将不可见字符串转成可见字符串
BASE64Decoder decoder = new BASE64Decoder();
byte[] buff = decoder.decodeBuffer(str);
byte[] cipherByte = c.doFinal(buff);
String str_ = new String(cipherByte);
return str_;
}
/**
* 将字符串加密成可视但不可解密的无空格字符串
* MD5加密
*
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public String encrypMD5(String str) throws NoSuchAlgorithmException {
// 根据MD5算法生成MessageDigest对象
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes();
// 使用bytes更新摘要
md5.update(bytes);
// 完成哈希运算,生成不可解密的密文
byte[] result = md5.digest();
//用base64再次加密
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(result);
return str_;
}
/**
* 生成UUID
*
* @return
*/
public String uuid() {
String str = UUID.randomUUID().toString().toUpperCase();
str = str.replaceAll("-", "");
return str;
}
/**
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws Exception {
EncryptionAndDecryption de1 = new EncryptionAndDecryption();
String secret= de1.uuid();
String msg= "secret";
// DES加密
String enStr = de1.Encrytor(msg,secret);
// DES解密
String deStr = de1.Decryptor(enStr,secret);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + enStr);
System.out.println("解密后:" + deStr);
if (msg.equals(deStr))
System.out.println(true);
else
System.out.println(false);
System.out.println("MD5加密后:" + de1.encrypMD5(msg));
// BASE64Encoder encoder = new BASE64Encoder();
// System.out.println(encoder.encode("32010000000003".getBytes()));
byte[] bytes = compress("32010000000003");
String str = decompress(bytes);
System.out.println(bytes.toString().length());
System.out.println(str);
String strLong = Long.toString(32010000000003l, 36);
System.out.println("strLong==="+strLong);
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
}
public static final byte[] compress(String str) {
if (str == null)
return null;
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch (IOException e) {
compressed = null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return compressed;
}
public static final String decompress(byte[] compressed) {
if (compressed == null)
return null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
// ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}
}
package com.infoservice.dms.chana.common;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @author oubocheng
*
*/
public class EncryptionAndDecryption {
/********************** 对称加密 *****************************/
/**将字符串加密成可解密但不可视的密文
* 对字符串加密
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public String Encrytor(String str, String secretKey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, keygen.generateKey());
byte[] src = str.getBytes();
// 加密,结果保存进cipherByte
byte[] cipherByte = c.doFinal(src);
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(cipherByte);
return str_;
}
/**
* 对字符串解密
*
* @param buff
* @return
* @throws Exception
*/
public String Decryptor(String str, String secretKey) throws Exception {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, keygen.generateKey());
//base64将不可见字符串转成可见字符串
BASE64Decoder decoder = new BASE64Decoder();
byte[] buff = decoder.decodeBuffer(str);
byte[] cipherByte = c.doFinal(buff);
String str_ = new String(cipherByte);
return str_;
}
/**
* 将字符串加密成可视但不可解密的无空格字符串
* MD5加密
*
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public String encrypMD5(String str) throws NoSuchAlgorithmException {
// 根据MD5算法生成MessageDigest对象
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes();
// 使用bytes更新摘要
md5.update(bytes);
// 完成哈希运算,生成不可解密的密文
byte[] result = md5.digest();
//用base64再次加密
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(result);
return str_;
}
/**
* 生成UUID
*
* @return
*/
public String uuid() {
String str = UUID.randomUUID().toString().toUpperCase();
str = str.replaceAll("-", "");
return str;
}
/**
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws Exception {
EncryptionAndDecryption de1 = new EncryptionAndDecryption();
String secret= de1.uuid();
String msg= "secret";
// DES加密
String enStr = de1.Encrytor(msg,secret);
// DES解密
String deStr = de1.Decryptor(enStr,secret);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + enStr);
System.out.println("解密后:" + deStr);
if (msg.equals(deStr))
System.out.println(true);
else
System.out.println(false);
System.out.println("MD5加密后:" + de1.encrypMD5(msg));
// BASE64Encoder encoder = new BASE64Encoder();
// System.out.println(encoder.encode("32010000000003".getBytes()));
byte[] bytes = compress("32010000000003");
String str = decompress(bytes);
System.out.println(bytes.toString().length());
System.out.println(str);
String strLong = Long.toString(32010000000003l, 36);
System.out.println("strLong==="+strLong);
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
}
public static final byte[] compress(String str) {
if (str == null)
return null;
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch (IOException e) {
compressed = null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return compressed;
}
public static final String decompress(byte[] compressed) {
if (compressed == null)
return null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
// ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}
}