您的位置:首页 > 编程语言 > Java开发

(2)java自带软件包javax.crypto的使用方法,保存generator中生成的key

2016-04-14 21:40 821 查看
关于javax.crypto的使用,这里不再介绍,参考上一篇blog。

这里说一下另一个问题。

在使用这个密码包的时候,以上一篇中代码所示,通过接口keygen.generateKey()生成的密钥每次都会重新生成,导致上一篇中代码只能够作为demo演示而用,在实际使用中则出现很多问题。

以Blowfish为例,如果能够按照下面的形式封装和使用,则非常好了。

String sk = BlowfishUtil.generatorKey();

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;

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 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 = Cipher.getInstance("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
e.printStackTrace();
}
byte[] src = str.getBytes();
// 加密,结果保存进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 = KeyGenerator.getInstance("Blowfish");
// 生成密钥
SecretKey seckey = keygen.generateKey();
String tmp_sk = Base64Utils.base64Encode(seckey.getEncoded());
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的语句主要就在于seckey.getEncoded()返回的byte[]数组。通过base64util,把这个byte[]数组转换成String保存起来。并可以直接用于构造函数。

其中base64util这个类用到了库commons-codec-1.10.jar。大家可以去官网下。附上封装的这个类。

package utils;
import org.apache.commons.codec.binary.Base64;

public class Base64Utils {
public static String base64Encode(String data) {
return Base64.encodeBase64String(data.getBytes());
}

public static String base64Encode(byte [] data) {
return Base64.encodeBase64String(data);
}

public static byte[] base64Decode(String data) {
return Base64.decodeBase64(data.getBytes());
}

public static void main(String[] args) {
// TODO Auto-generated method stub
// Base64 b = new Base64();
String str = "1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz";
System.out.println(str);
String base64str = base64Encode(str);
System.out.println(base64str);
byte[] buf = base64Decode(base64str);
System.out.println(new String(buf));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: