⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tripledeskeygenerator.java

📁 对称和非对称加密
💻 JAVA
字号:
package com.dmgc.security.cipher.symmetic.tripledes;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.生成一个24个字节长的随机BIT串作为一个对称密钥. * <p>2.构成一个将24个字节长的字节数组转化为16进制的字符串的函数,便于表示. * <p>3.构成一个将16进制表示的24个字节转化为字节形式.便于操作. * @author 陆荣幸  周渊   潘勇 * @version 1.0 */public class TripleDESKeyGenerator {  /**  *构造函数  */ public TripleDESKeyGenerator() {  } /**  * 生成一个DES密钥文件,  * 过程为随机生成一个24字节的字节数组,然后转化为16进制字符串,然后将48个字符以字节方式  * 写入文件  * @param outFileName 输出的文件名  * @throws InvalidDESKeyException DES无效时的抛异常  * @throws IOException IO异常  */ public void TripleDESKeyFileGenerator(String outFileName) throws     InvalidTripleDESKeyException, IOException {   //   FileOutputStream fOutput = null;   //   byte[] key = getTripleDESKey();   //   String skey = null;   try {     skey = ByteToHexString(key);   }   catch (InvalidTripleDESKeyException ex) {     throw new InvalidTripleDESKeyException("Invalid triple DES key");   }   //   byte[] stringKey = skey.getBytes();   //   try {     //  输出文件     fOutput = new FileOutputStream(outFileName);     //     fOutput.write(stringKey);     //     fOutput.close();   }   catch (IOException ex1) {     throw new IOException("IO Exception");   } } /**  * 生成一个24个字节的对称密钥  * @return 24个字节的字节数组  */ public byte[] getTripleDESKey() {   //   int k = 24;   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  必须是24个字节,否则抛异常  * @return 十六进制字符串  * @throws InvalidTripleDESKeyException 密钥格式不正确  */ static public String ByteToHexString(byte[] inbyte) throws     InvalidTripleDESKeyException {   //   String ret = "";   //   if (inbyte.length != 24) {     throw new InvalidTripleDESKeyException("Invalid Triple DES Key Exception");   }   //   for (int i = 0; i < 24; 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     InvalidTripleDESKeyException, HexStringToByteException {   //   byte[] ret = new byte[24];   //   if (inString.length() != 48) {     throw new InvalidTripleDESKeyException("Invalid Triple DES Key Exception");   }   //   for (int i = 0; i < 24; 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) {   //   TripleDESKeyGenerator deskey = new TripleDESKeyGenerator();   //   byte[] plain = deskey.getTripleDESKey();   //   System.out.println("生成密钥");   //   System.out.println("转化为字符串打印");   //   try {     System.out.println(ByteToHexString(plain));   }   catch (InvalidTripleDESKeyException 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 (InvalidTripleDESKeyException ex1) {     ex1.printStackTrace();   } } //////////////////////////////////////////////////////////////////////////////}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -