CDesCBC.java
资源名称:security.rar [点击查看]
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:7k
源码类别:
CA认证
开发平台:
Java
- package org.infosecurity.cryptography;
- /**
- * <p>Title: CBC模式的加密算法 </p>
- * <p>Description: CBC模式的加密算法 </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: 中信信息安全组织(CISO) </p>
- * @author 张荣华 (Eric Zhang)
- * @version 1.0.2003.1022
- */
- import org.infosecurity.cryptography.*;
- public class CDesCBC 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 CDesCBC(byte[] pwd) {
- super(pwd);
- }
- /**
- * CBC模式的加密
- * @param iv 初始向量
- * @param in 输入数据
- * @param out 输出数据
- * @return 输出数据的长度
- */
- public int EncryptCBC(byte[] iv,byte[] in,int in_len,byte[] out)
- {
- // =========================================================
- // 定义变量
- int out_length = 0; /* 输出长度 */
- int cur_pos = 0; /* 当前位置 */
- int block_count = 0; /* 数据块数 */
- 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;
- System.arraycopy(out_block,0,out,0,8);
- System.arraycopy(out_block,0,IV, 0,8);
- out_length += 8;
- cur_pos += 8;
- // =========================================================
- // 处理输入的数据
- block_count = in_len/8;
- remaider = (byte)(in_len % 8);
- MyDebug.out("输入数据的块数:"+block_count+"t最后一块的字节数:"+remaider);
- /* 调试信息 */
- for(int i = 0 ; i < block_count;i++)
- {
- for(int j = 0;j < 8;j++)
- {
- IV[j]^=in[8*i+j];
- MyDebug.out("in["+j+"]="+in[8*i+j]); /* 调试信息 */
- }
- out_length += 8;
- out_block = encrypt(IV);
- if(out_block == null || out_block.length==0)
- return S_ENC_ERROR;
- System.arraycopy(out_block,0,out,cur_pos,8);
- System.arraycopy(out_block,0,IV,0,8);
- cur_pos += 8;
- out_block = null;
- }
- // =========================================================
- // 处理输入数据最后一个数据块
- byte value = (byte)(8 - remaider);
- if(remaider > 0) /* 如果有数据,将它复制过来 */
- System.arraycopy(in,block_count*8,last_block,0,remaider);
- for(int i = remaider ;i < 8;i++) /* 进行填充 */
- last_block[i] = value;
- 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;
- System.arraycopy(out_block,0,out,cur_pos,8);
- System.arraycopy(out_block,0,IV,0,8);
- return out_length;
- }
- /**
- * CBC模式的解密
- * @param in 输入数据
- * @param out 输出数据
- * @return 输出数据的长度
- */
- public int DecryptCBC(byte[] in,int in_len,byte[] out)
- {
- // =========================================================
- // 定义变量
- int out_length = 0,cur_pos = 0; /* 输出长度与当前位置 */
- int block_count = 0; /* 数据块数 */
- 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]; /* 最后一个数据块 */
- // =========================================================
- // 处理IV
- System.arraycopy(in,0,IV,0,8);
- // =========================================================
- // 处理输入的数据
- block_count = in_len/8;
- MyDebug.out("输入要解密数据的块数:"+block_count);
- for(int i = 0 ; i < block_count-2;i++) /* -2的原因是要去除第 */
- /* 一块和最后一块 */
- {
- System.arraycopy(in,(i+1)*8,bTemp,0,8);
- 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]);
- /* 调试信息 */
- }
- out_length += 8;
- System.arraycopy(out_block,0,out,cur_pos,8);
- System.arraycopy(in,(i+1)*8,IV,0,8);
- cur_pos += 8;
- out_block = null;
- }
- // =========================================================
- // 处理输入数据最后一个数据块
- System.arraycopy(in,(block_count-1)*8,last_block,0,8);
- out_block = decrypt(last_block);
- 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("last_block["+j+"]="+out_block[j]);
- /* 调试信息 */
- }
- byte real_count = (byte)(8 - out_block[7]);
- out_length += real_count;
- if(real_count>0)
- System.arraycopy(out_block,0,out,cur_pos,real_count);
- return out_length;
- }
- /**
- * CBC算法的测试程序
- * @author 张荣华
- * @version 1.0.2003.1027
- */
- public static void main(String[] args) {
- CDesCBC cbc = new CDesCBC("12345678".getBytes());
- byte[] out = new byte[128];
- byte[] plaintext = new byte[128];
- /* 输入的数据 */
- byte[] in="这一个测试jkdfjkldfjkldfskjlasjkreio978789846r494r9/,.,.m".getBytes();
- byte[] iv="12345678".getBytes();
- System.out.println("明文数据为:"+new String(in));
- /* 加密 */
- int clen = cbc.EncryptCBC(iv,in,in.length,out);
- System.out.println("cipher text len="+clen);
- /* 解密 */
- int plen = cbc.DecryptCBC(out,clen,plaintext);
- byte[] result = new byte[plen];
- System.arraycopy(plaintext,0,result,0,plen);
- System.out.println(new String(result));
- }
- }