📄 deskeygenerator.java
字号:
package com.dmgc.security.cipher.symmetic.des;
import java.security.SecureRandom;
import com.dmgc.security.cipher.util.HexStringToByteException;
import java.io.*;
/**
* <p>Title: DMGC SECURITY CIPHER LIB</p>
* <p>Description: 上海信宁科技有限公司 安全密码库(JAVA version)</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: 上海信宁科技有限公司</p>
* <p>1.生成一个8个字节长的随机BIT串作为一个对称密钥.
* <p>2.构成一个将8个字节长的字节数组转化为16进制的字符串的函数,便于表示.
* <p>3.构成一个将16进制表示的8个字节转化为字节形式.便于操作.
*
* @author 陆荣幸 周渊 潘勇
* @version 1.0
*/
public class DESKeyGenerator {
/**
*构造函数
*/
public DESKeyGenerator() {
}
/**
* 生成一个DES密钥文件,
* 过程为随机生成一个8字节的字节数组,然后转化为16进制字符串,然后将16个字符以字节方式
* 写入文件
* @param outFileName 输出的文件名
* @throws InvalidDESKeyException DES无效时的抛异常
* @throws IOException IO异常
*/
public void DESKeyFileGenerator(String outFileName) throws
InvalidDESKeyException, IOException {
//
FileOutputStream fOutput = null;
//
byte[] key = getDESKey();
//
String skey = null;
try {
skey = ByteToHexString(key);
}
catch (InvalidDESKeyException ex) {
throw new InvalidDESKeyException("Invalid DES key");
}
//
byte[] stringKey = skey.getBytes();
//
try {
// 输出文件
fOutput = new FileOutputStream(outFileName);
//
fOutput.write(stringKey);
//
fOutput.close();
}
catch (IOException ex1) {
throw new IOException("IO Exception");
}
}
/**
* 生成一个8个字节的对称密钥
* @return 8个字节的字节数组
*/
public byte[] getDESKey() {
//
int k = 8;
byte[] ret = new byte[k];
//
SecureRandom random = null;
//
try {
random = SecureRandom.getInstance("SHA1PRNG");
}
catch (Exception ex) {
ex.printStackTrace();
}
//
random.nextBytes(ret);
//
return ret;
}
/**
* 将字节数组转化为16字进字符串,
* @param inbyte 必须是8个字节,否则抛异常
* @return 十六进制字符串
* @throws InvalidDESKeyException 密钥格式不正确
*/
static public String ByteToHexString(byte[] inbyte) throws
InvalidDESKeyException {
//
String ret = "";
//
if (inbyte.length != 8) {
throw new InvalidDESKeyException("Invalid DES Key Exception");
}
//
for (int i = 0; i < 8; i++) {
String temp = Integer.toHexString( (int) inbyte[i]);
//
if (temp.length() == 1) {
temp = "0" + temp;
}
//
if (temp.length() > 2) {
temp = temp.substring(temp.length() - 2);
}
ret = ret + temp;
}
//
return ret;
}
/**
* 将十六进制形式的字符串转化为字节数组,长度为8个字节
* @param inString 长度为16
* @return 长度为8的字节数组
* @throws InvalidDESKeyException 如果输入长度不是16的字符串
*/
static public byte[] HexStringToByte(String inString) throws
InvalidDESKeyException, HexStringToByteException {
//
byte[] ret = new byte[8];
//
if (inString.length() != 16) {
throw new InvalidDESKeyException("Invalid DES Key Exception");
}
//
for (int i = 0; i < 8; i++) {
String temp = inString.substring(2 * i, 2 * (i + 1));
byte c = (byte) 0;
try {
c = (byte) Integer.parseInt(temp, 16);
}
catch (NumberFormatException ex) {
throw new HexStringToByteException("Hexstring to byte exception");
}
ret[i] = c;
}
//
return ret;
}
////////////////////////////////////////////////////////////////////////////////
/**
* 用于测试
* @param args
*/
public static void main(String[] args) {
//
DESKeyGenerator deskey = new DESKeyGenerator();
//
byte[] plain = deskey.getDESKey();
//
System.out.println("生成密钥");
//
System.out.println("转化为字符串打印");
//
try {
System.out.println(ByteToHexString(plain));
}
catch (InvalidDESKeyException ex) {
ex.printStackTrace();
}
//
System.out.println("还原");
try {
byte[] plain2 = HexStringToByte(ByteToHexString(plain));
System.out.println("转化");
System.out.println(ByteToHexString(plain2));
}
catch (HexStringToByteException ex1) {
ex1.printStackTrace();
}
catch (InvalidDESKeyException ex1) {
ex1.printStackTrace();
}
}
//////////////////////////////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -