📄 tripledesalgorithm.java
字号:
package com.dmgc.security.cipher.symmetic.tripledes;
import com.dmgc.security.cipher.util.*;
import java.security.InvalidKeyException;
import java.io.*;
/**
* <p>Title: DMGC SECURITY CIPHER LIB</p>
* <p>Description: 上海信宁科技有限公司 安全密码库(JAVA version)</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: 上海信宁科技有限公司</p>
* 本类主要实现三重DES算法对文件的加解密,文件基本没有扩张
* @author 陆荣幸 周渊 潘勇
* @version 1.0
*/
public class TripleDESAlgorithm
extends TripleDESCore {
/**
* 默认的构造函数
*/
public TripleDESAlgorithm() {
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 java.
io.FileNotFoundException, java.security.InvalidKeyException,
java.io.IOException, java.lang.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 = 8 - byteLength % 8;
//
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[] firstbuffer = new byte[8 - padLength];
//
int bufferLength = fInput.read(firstbuffer);
//
if (bufferLength != (8 - padLength)) {
throw new Exception("Error");
}
//
System.arraycopy(firstbuffer, 0, c, padLength, 8 - padLength);
//加密
coreCrypt(c, 0, oo, 0);
//写文件
fOutput.write(oo, 0, 8);
//
byte[] buffer = new byte[8192];
//
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);
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 java.
io.FileNotFoundException, java.security.InvalidKeyException,
java.io.IOException, java.lang.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, 8 - 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) {
TripleDESAlgorithm tripleDESAlgorithm1 = new TripleDESAlgorithm();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -