📄 fileencryptor.java
字号:
package org.infosecurity.cryptography;
/**
* <p>Title: DES_CBC文件加密器 </p>
* <p>Description: DES_CBC文件加密器 </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: 中信信息安全组织(CISO)</p>
* @author 张荣华
* @version 1.0.2003.1028
*/
import org.infosecurity.cryptography.*;
import java.io.*;
public class FileEncryptor extends DES {
//=========================================================
// 定义错误类型,以有意义的常量代替数字
public final static int S_OK = 1; /* 成功 */
public final static int S_IV_ERROR = -1; /* 初始化向量出错 */
public final static int S_ENC_ERROR = -2; /* 加密错误 */
public final static int S_DEC_ERROR = -3; /* 解密错误 */
/**
* 构造函数
* @param pwd 密码 (8 个字节)
*/
public FileEncryptor(byte[] pwd) {
super(pwd);
}
/**
* CBC模式的加密
* @param iv 初始向量
* @param in 输入数据
* @param out 输出数据
* @return 输出数据的长度
*/
public int EncryptCBC(byte[] iv,InputStream in,OutputStream out)
throws IOException
{
// =========================================================
// 定义变量
int out_length = 0; /* 输出长度 */
int block_count = 0; /* 数据块数 */
int return_flag =-2; /* 返回字节数 */
byte remaider = 0; /* 剩余字节数 */
byte[] IV = new byte[8]; /* 初始向量 */
byte[] bTemp = new byte[8]; /* 临时缓冲区 */
byte[] out_block = new byte[8]; /* 输出内容 */
byte[] last_block = new byte[8]; /* 最后一个数据块 */
// =========================================================
// 合法性检查
if(iv == null||iv.length!=8)
return S_IV_ERROR;
// =========================================================
// 处理IV
out_block = encrypt(iv);
if(out_block == null || out_block.length == 0)
return S_ENC_ERROR;
out.write(out_block,0,8);
out.flush();
System.arraycopy(out_block,0,IV, 0,8);
out_length += 8;
// =========================================================
// 处理输入的数据
return_flag=in.read(bTemp,0,8);
while(return_flag==8)
{
for(int j = 0;j < 8;j++)
{
IV[j]^=bTemp[j];
//MyDebug.out("in["+j+"]="+bTemp[j]); /* 调试信息 */
}
out_length += 8;
out_block = encrypt(IV);
if(out_block == null || out_block.length==0)
return S_ENC_ERROR;
out.write(out_block,0,8);
out.flush();
System.arraycopy(out_block,0,IV,0,8);
out_block = null;
return_flag=in.read(bTemp,0,8);
}
// =========================================================
// 处理输入数据最后一个数据块
byte value=0;
if(return_flag>0)
{
value = (byte)(8 - return_flag);
System.arraycopy(bTemp,0,last_block,0,return_flag);/* 如果有数据,将它复制过来 */
for(int i=return_flag;i<8;i++)last_block[i]=value; /* 进行填充 */
}
else
{
value = (byte)8;
for(int i=0;i<8;i++)last_block[i]=(byte)8; /* 进行填充 */
}
for(int j = 0;j < 8;j++)
IV[j]^=last_block[j];
out_length += 8;
out_block = encrypt(IV);
if(out_block == null || out_block.length==0)
return S_ENC_ERROR;
out.write(out_block,0,8);
out.flush();
// out.close();
// in.close();
return out_length;
}
/**
* CBC模式的解密
* @param in 输入数据
* @param out 输出数据
* @return 输出数据的长度
*/
public int DecryptCBC(InputStream in,OutputStream out) throws IOException
{
// =========================================================
// 定义变量
int out_length = 0,cur_pos = 0; /* 输出长度与当前位置 */
int block_count = 0; /* 数据块数 */
byte remaider = 0; /* 剩余字节数 */
int return_flag =-2; /* 返回字节数 */
byte[] IV = new byte[8]; /* 初始化向量 */
byte[] bTemp = new byte[8]; /* 临时缓冲区 */
byte[] out_block = new byte[8]; /* 输出内容 */
byte[] last_block = new byte[8]; /* 最后一个数据块 */
// =========================================================
// 处理IV
block_count = in.available()/8;
return_flag = in.read(IV,0,8);
// =========================================================
// 处理输入的数据
return_flag = in.read(bTemp,0,8);
for(int i = 0 ; i < block_count-1&&return_flag>0;i++) /* -1的原因是要去除第一块 */
{
out_block = decrypt(bTemp);
if(out_block == null || out_block.length==0)
return S_DEC_ERROR;
for(int j = 0;j < 8;j++)
{
out_block[j]^=IV[j];
//MyDebug.out("out_block["+j+"]="+out_block[j]);
/* 调试信息 */
}
if(i==block_count-2)
{
byte real_count = (byte)(8 - out_block[7]);
out_length += real_count;
out.write(out_block,0,real_count);
}
else
{
out_length += 8;
out.write(out_block,0,8);
}
out.flush();
System.arraycopy(bTemp,0,IV,0,8);
out_block = null;
return_flag = in.read(bTemp,0,8);
}
return out_length;
}
public static void main(String args[])
{
FileInputStream fis = null;
FileOutputStream fos = null;
try {
FileEncryptor fe = new FileEncryptor("12345678".getBytes());
fis = new FileInputStream("E:\\JavaProj\\temp\\ciphertext.txt");
fos = new FileOutputStream("E:\\JavaProj\\temp\\result.txt");
fe.DecryptCBC(fis,fos);
fis.close();
fos.close();
}
catch (IOException ex) {
MyDebug.out("Dec 出错了!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -