(2)java⾃带软件包pto的使⽤⽅法,保存generator
中⽣成的key
关于pto的使⽤,这⾥不再介绍,参考上⼀篇blog。
这⾥说⼀下另⼀个问题。
在使⽤这个密码包的时候,以上⼀篇中代码所⽰,通过接⼝ateKey()⽣成的密钥每次都会重新⽣成,导致上⼀篇中代码只能够作为demo演⽰⽽⽤,在实际使⽤中则出现很多问题。
以Blowfish为例,如果能够按照下⾯的形式封装和使⽤,则⾮常好了。
String sk = atorKey();
BlowfishUtil u = new BlowfishUtil(sk);
String cipher = u.encrypt(msg);
String plain = u.decrypt(cipher);
好了,废话不多说,直接上code。
package encryption_schema;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.KeyGenerator;
pto.NoSuchPaddingException;
pto.SecretKey;
pto.spec.SecretKeySpec;
import utils.Base64Utils;
public class BlowFishUtil {
// SecretKey 负责保存对称密钥
private SecretKey deskey;
// Cipher负责完成加密或解密⼯作
private Cipher c;
// 该字节数组负责保存加密的结果
private byte[] cipherByte;
public BlowFishUtil(String key) throws NoSuchAlgorithmException, NoSuchPaddingException {
byte[] bKey = Base64Utils.base64Decode(key);
deskey = new SecretKeySpec(bKey, "Blowfish");
c = Instance("Blowfish");
}
/**
* 对字符串加密
*
* @param str
* @return
*/
private byte[] Encrytor(String str) {
// 根据密钥,对Cipher对象进⾏初始化,ENCRYPT_MODE表⽰加密模式
try {
c.init(Cipher.ENCRYPT_MODE, deskey);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
// TODO Auto-generated catch block
e.printStackTrace();java用什么软件运行
}
byte[] src = Bytes();
// 加密,结果保存进cipherByte
try {
cipherByte = c.doFinal(src);
} catch (IllegalBlockSizeException | BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cipherByte;
}
/**
* 对字符串解密
*
* @param buff
* @return
*/
private byte[] Decryptor(byte[] buff) {
/
/ 根据密钥,对Cipher对象进⾏初始化,DECRYPT_MODE表⽰加密模式
try {
c.init(Cipher.DECRYPT_MODE, deskey);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
cipherByte = c.doFinal(buff);
} catch (IllegalBlockSizeException | BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cipherByte;
}
/**
* 密⽂cipher,通过base64util进⾏转换成byte[],然后解密成byte形式的明⽂,在转换成String  *
* @param base64str
* @return
*/
public String decrypt(String base64str_cipher) {
// ⽤base64util对String进⾏转换,转换成byte[]
byte[] encontent = Base64Utils.base64Decode(base64str_cipher);
// 解密byte形式的密⽂,转换成了String形式的密⽂
byte[] decontent = Decryptor(encontent);
String plain = new String(decontent);
return plain;
}
/**
* 明⽂plain,加密成byte形式的密⽂,然后⽤base64util进⾏转换成String
*
* @param plain
* @return
*/
public String encrypt(String plain) {
// 加密String形式的明⽂,成了byte形式的密⽂
byte[] encontent = Encrytor(plain);
// ⽤base64util对byte进⾏转换,转换成String
String base64str_cipher = Base64Utils.base64Encode(encontent);
return base64str_cipher;
}
/**
* ⽣成符合Blowfish的key,String类型,可⽤于该类的构造函数
*
* @return
* @throws NoSuchAlgorithmException
*/
static public String GenerateKey() throws NoSuchAlgorithmException {
// 实例化⽀持DES算法的密钥⽣成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = Instance("Blowfish");
// ⽣成密钥
SecretKey seckey = ateKey();
String tmp_sk = Base64Utils.Encoded());
return tmp_sk;
}
public static void main(String[] args) throws Exception {
String tmp_seckey = BlowFishUtil.GenerateKey();
System.out.println("SecretKet: " + tmp_seckey);
BlowFishUtil u = new BlowFishUtil(tmp_seckey);
String msg = "hello world";
String cipher = u.encrypt(msg);
System.out.println("cipher: " + cipher);
String plain = u.decrypt(cipher);
System.out.println("plain: " + plain);
}
}
可以看到,保存SecretKey的语句主要就在于Encoded()返回的byte[]数组。通过base64util,把这个byte[]数组转换成String 保存起来。并可以直接⽤于构造函数。
其中base64util这个类⽤到了库commons-codec-1.10.jar。⼤家可以去官⽹下。附上封装的这个类。
package utils;
import dec.binary.Base64;
public class Base64Utils {
public static String base64Encode(String data) {
Bytes());
}
public static String base64Encode(byte [] data) {
deBase64String(data);
}
public static byte[] base64Decode(String data) {
return Base64.Bytes());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Base64 b = new Base64();
String str = "1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijk  System.out.println(str);
String base64str = base64Encode(str);
System.out.println(base64str);
byte[] buf = base64Decode(base64str);
System.out.println(new String(buf));
}
}