📄 tripledesprobabilityalgorithm.java
字号:
package com.dmgc.security.cipher.symmetic.tripledes;import com.dmgc.security.cipher.util.*;import java.security.*;import java.io.*;/** * <p>Title: DMGC SECURITY CIPHER LIB</p> * <p>Description: 上海信宁科技有限公司 安全密码库(JAVA version)</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: 上海信宁科技有限公司</p> * 本类主要实现TripleDES算法对文件的加解密,文件扩张为8:7.但是可以防制选择密文攻击,实现了概率加密 * @author 陆荣幸 周渊 潘勇 * @version 1.0 */public class TripleDESProbabilityAlgorithm extends TripleDESCore { /*** 生成一个非零随机字节* @return 一个非零随机字节*/private byte getOneNonZeroByte(){ byte[] c = new byte[1]; byte ret; SecureRandom random = null; try { random = SecureRandom.getInstance("SHA1PRNG"); } catch (Exception ex) { ex.printStackTrace(); } random.nextBytes(c); ret = c[0]; if (ret == 0x00) { ret =(byte)( ret | (byte)0x01); } return ret;}/** * 构造函数 */ public TripleDESProbabilityAlgorithm() { super(); } /** * tripleDES概率加密 * @param inFile 输入文件 * @param outFile 输出文件 * @param keys 密钥 字符串 十六进制形式 * @throws FileNotFoundException 文件没有找到 * @throws InvalidKeyException 密钥不正确 * @throws IOException 输入输出异常 * @throws java.lang.Exception 其它异常 */ public void Encrypt(String inFile, String outFile, String keys) throws FileNotFoundException, InvalidKeyException, IOException, Exception { FileInputStream fInput = null; FileOutputStream fOutput = null; // byte[] keyBytes = ByteConvertor.CommonHexStringToByte(keys); try { // 输入文件 fInput = new FileInputStream(inFile); // 输出文件 fOutput = new FileOutputStream(outFile); // 输入文件的字节总长度 int byteLength = fInput.available(); // 剩余字节 int padLength = 7 - byteLength % 7; // byte[] c = new byte[8]; // byte[] oo = new byte[8]; // byte[] first7 = DmgcMpInteger.getRandomByteArray(7); // System.arraycopy(first7, 0, c, 0, 7); // byte PadCount = (byte) padLength; // c[7] = PadCount; // 文件头生成 TripleDESSecretKey key0 = new TripleDESSecretKey(keyBytes); //加密初始化 coreInit(key0, false); //加密 coreCrypt(c, 0, oo, 0); //写文件 fOutput.write(oo, 0, 8); // if (padLength != 0) { byte[] pad = DmgcMpInteger.getRandomByteArray(padLength); // System.arraycopy(pad, 0, c, 0, padLength); } // // 用于填充后面的随机位,由于随机算法太费时间 // byte myc = c[3]; byte[] myc = new byte[1]; SecureRandom random = null; try { random = SecureRandom.getInstance("SHA1PRNG"); } catch (Exception ex) { ex.printStackTrace(); } random.nextBytes(myc); // byte[] firstbuffer = new byte[7 - padLength]; // int bufferLength = fInput.read(firstbuffer); // if (bufferLength != (7 - padLength)) { throw new Exception("Error"); } // System.arraycopy(firstbuffer, 0, c, padLength + 1, 7 - padLength); //加密 coreCrypt(c, 0, oo, 0); //写文件 fOutput.write(oo, 0, 8); // byte[] buffer = new byte[7168]; // while ( (bufferLength = fInput.read(buffer)) != -1) { for (int i = 0; i < bufferLength; i = i + 7) { System.arraycopy(buffer, i, c, 1, 7);// myc = (byte)((myc+1) % 255);// c[0] = myc; c[0] = myc[0]; random.nextBytes(myc); coreCrypt(c, 0, oo, 0); fOutput.write(oo, 0, 8); } } // fInput.close(); fOutput.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); throw new FileNotFoundException("File not found exception"); } catch (InvalidKeyException ex) { ex.printStackTrace(); throw new InvalidKeyException("Invalid key exception"); } catch (IOException ex) { ex.printStackTrace(); throw new IOException("IO Exception"); } catch (Exception ex) { ex.printStackTrace(); throw new Exception("Encryption Exception"); } } /** * tripleDES概率解密 * @param inFile 输入文件 * @param outFile 输出文件 * @param keys 密钥 字符串 十六进制形式 * @throws FileNotFoundException 文件没有找到 * @throws InvalidKeyException 密钥不正确 * @throws IOException 输入输出异常 * @throws java.lang.Exception 其它异常 */ public void Decrypt(String inFile, String outFile, String keys) throws FileNotFoundException, InvalidKeyException, IOException, Exception { FileInputStream fInput = null; FileOutputStream fOutput = null; // byte[] keyBytes = keys.getBytes(); byte[] keyBytes = ByteConvertor.CommonHexStringToByte(keys); try { // 输入文件 fInput = new FileInputStream(inFile); // 输出文件 fOutput = new FileOutputStream(outFile); // 剩余字节 int padLength = 0; // byte[] c = new byte[8]; // byte[] oo = new byte[8]; // 文件头生成 TripleDESSecretKey key0 = new TripleDESSecretKey(keyBytes); //加密初始化 coreInit(key0, true); // int bufferLength = 0; // byte[] buffer = new byte[8192]; // boolean token = false; // int offset = 0; // while ( (bufferLength = fInput.read(buffer)) != -1) { for (int i = 0; i < bufferLength; i = i + 8) { System.arraycopy(buffer, i, c, 0, 8); coreCrypt(c, 0, oo, 0); if (token == true) { fOutput.write(oo, offset + 1 , 7 - offset); if (offset != 0) { offset = 0; } } else { offset = oo[7]; token = true; } } } // fInput.close(); fOutput.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); throw new FileNotFoundException("File not found exception"); } catch (InvalidKeyException ex) { ex.printStackTrace(); throw new InvalidKeyException("Invalid key exception"); } catch (IOException ex) { ex.printStackTrace(); throw new IOException("IO Exception"); } catch (Exception ex) { ex.printStackTrace(); throw new Exception("Encryption Exception"); } }////////////////////////////////////////////////////////////////////////////////// /** * 用于测试 * @param args */ public static void main(String[] args) { TripleDESProbabilityAlgorithm tripleDESProbabilityAlgorithm1 = new TripleDESProbabilityAlgorithm(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -