📄 rsadecryption.java
字号:
package com.dmgc.security.cipher.unsymmetic.rsa;
import com.dmgc.security.cipher.util.*;
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>RSA解密算法:对应于四种加密算法,主要有四种方式加密算法
* <p>对短信息的解密:这里短信息可以是会话双方的交换密钥,对于1024比特的RSA解密
* 算法短信息长度一定等于128个字节,如果不是,则停止。恢复后,去掉填充位,所得明文信息。
* 开头二位0x00 0x02 然后判断下一个0x00,之后即为明文消息。
* <p>方法一:公钥N,E是字符串(DMGCMPINTEGER)形式,输入消息是字节数组,输出消息是字节数组
* <p>方法二:公钥N,E是文件(二进制文件)形式,输入消息是字节数组,输出消息是字节数组
* <p>对文件的加密:
* <p>1.首先判断文件的字节数是否是128的倍数,是,则继续。否则,停止。
* <p>2.每次读取文件(8192)字节到缓冲区
* <p>3.对128个字节进行处理
* <p>4.解密,如果不足128字节,则前面补0
* <p>5.判断前二位0x00 0x02,从下面出现的0x00之后获取明文
* <p>6 这里的加密方案,明文:密文 = 91.4%
* <p>方法三:公钥N,E是字符串(DMGCMPINTEGER)形式,输入是文件,输出是文件
* <p>方法四:公钥N,E是文件(二进制文件)形式,输入是文件,输出是文件
* @author 陆荣幸 周渊 潘勇
* @version 1.0
*/
public class RSADecryption {
/**
* 默认构造函数
*/
public RSADecryption() {
}
/**
* 用于测试
* @param args
*/
public static void main(String[] args) {
RSADecryption RSADecryption1 = new RSADecryption();
}
/**
* 使用二进制文件作为密钥进短消息进行解密
* @param nfilename RSA算法大整数模的文件
* @param dfilename RSA算法的解密指数文件
* @param cipherText 密文 128字节
* @return 明文消息
* @throws InvalidCryptographException 密文不是有效密文
* @throws FileNotFoundException 密钥文件没有找到
* @throws IOException 其它IO错误
*/
public byte[] MessageDecryptionF(String nfilename, String dfilename,
byte[] cipherText) throws
InvalidCryptographException, FileNotFoundException, IOException {
// 密文长度
int length = cipherText.length;
if (length != 128) {
throw new InvalidCryptographException("Invalid Cryptograph Exception");
}
// 解密指数 模
DmgcMpInteger dd = null;
DmgcMpInteger nn = null;
FileInputStream fInputN = null;
FileInputStream fInputD = null;
// 密钥文件
try {
fInputD = new FileInputStream(dfilename);
fInputN = new FileInputStream(nfilename);
int lengthN = fInputN.available();
int lengthD = fInputD.available();
byte[] BufferN = new byte[lengthN];
byte[] BufferD = new byte[lengthD];
lengthN = fInputN.read(BufferN);
lengthD = fInputD.read(BufferD);
byte[] N = new byte[lengthN];
System.arraycopy(BufferN, 0, N, 0, lengthN);
byte[] D = new byte[lengthD];
System.arraycopy(BufferD, 0, D, 0, lengthD);
dd = new DmgcMpInteger(D);
nn = new DmgcMpInteger(N);
fInputN.close();
fInputD.close();
}
catch (FileNotFoundException ex) {
throw new FileNotFoundException("file not found");
}
catch (IOException ex) {
throw new IOException("IO Exception");
} //
// 解密
DmgcMpInteger ccc = new DmgcMpInteger(cipherText);
DmgcMpInteger pp = ccc.modPow(dd, nn);
//
byte[] plainTemp = pp.toByteArray();
byte[] plainText = new byte[128];
//
length = plainTemp.length;
int padCount = 128 - length;
if (padCount > 0) {
byte[] paddZero = new byte[padCount];
// for (int i = 0; i < padCount; i++) {
// paddZero[i] = 0x00;
// }
System.arraycopy(paddZero, 0, plainText, 0, padCount);
System.arraycopy(plainTemp, 0, plainText, padCount, length);
}
else {
System.arraycopy(plainTemp, 0, plainText, padCount, length);
}
if (plainText[0] != 0x00 || plainText[1] != 0x02) {
throw new InvalidCryptographException("Invalid Cryptograph Exception");
}
int start = 0;
for (int i = 2; i < plainText.length; i++) {
if (plainText[i] == 0x00) {
start = i + 1;
break;
}
}
length = 128 - start;
byte[] ret = new byte[length];
System.arraycopy(plainText, start, ret, 0, length);
return ret;
}
/**
* 解密短消息
* @param n RSA算法大整数模
* @param d RSA算法解密指数
* @param cipherText 密文,一定是128个字节
* @return 明文字节
* @throws InvalidCryptographException
*/
public byte[] MessageDecryption(String n, String d, byte[] cipherText) throws
InvalidCryptographException {
// 密文长度
int length = cipherText.length;
if (length != 128) {
throw new InvalidCryptographException("Invalid Cryptograph Exception");
}
// 解密
DmgcMpInteger dd = new DmgcMpInteger(d);
DmgcMpInteger nn = new DmgcMpInteger(n);
DmgcMpInteger ccc = new DmgcMpInteger(cipherText);
DmgcMpInteger pp = ccc.modPow(dd, nn);
//
byte[] plainTemp = pp.toByteArray();
byte[] plainText = new byte[128];
//
length = plainTemp.length;
int padCount = 128 - length;
if (padCount > 0) {
byte[] paddZero = new byte[padCount];
// for (int i = 0; i < padCount; i++) {
// paddZero[i] = 0x00;
// }
System.arraycopy(paddZero, 0, plainText, 0, padCount);
System.arraycopy(plainTemp, 0, plainText, padCount, length);
}
else {
System.arraycopy(plainTemp, 0, plainText, padCount, length);
}
if (plainText[0] != 0x00 || plainText[1] != 0x02) {
throw new InvalidCryptographException("Invalid Cryptograph Exception");
}
int start = 0;
for (int i = 2; i < plainText.length; i++) {
if (plainText[i] == 0x00) {
start = i + 1;
break;
}
}
length = 128 - start;
byte[] ret = new byte[length];
System.arraycopy(plainText, start, ret, 0, length);
return ret;
}
/**
* 返回指定个数的zero
* @param k 指定长度
* @return zero 字节的数组
*/
private byte[] getPaddingZero(int k) {
byte[] ret = new byte[k];
return ret;
}
/**
* 解密文件
* @param n RSA算法大整数模的文件
* @param d RSA算法的解密指数文件
* @param inFile 密文文件
* @param outFile 明文文件
* @return 如果成功,返回true,否则 false
* @throws InvalidCryptographException 如果密文文件格式错误,抛异常
* @throws FileNotFoundException 文件没有找到
* @throws IOException IO 异常
*/
public boolean FileDecryption(String n, String d, String inFile,
String outFile) throws
InvalidCryptographException,
FileNotFoundException, IOException {
boolean ret = false;
// 解密密钥
DmgcMpInteger dd = new DmgcMpInteger(d);
DmgcMpInteger nn = new DmgcMpInteger(n);
//
FileInputStream fInput = null;
FileOutputStream fOutput = null;
//
try {
// 输入文件
fInput = new FileInputStream(inFile);
// 输出文件
fOutput = new FileOutputStream(outFile);
}
catch (FileNotFoundException ex) {
throw new FileNotFoundException("file not found");
}
//
int inLength = fInput.available();
if (inLength % 128 != 0) {
fInput.close();
fOutput.close();
throw new InvalidCryptographException("Invalid cryptograph exception");
}
//
byte[] buffer = new byte[8192];
int bufferLength = 0;
//
while ( (bufferLength = fInput.read(buffer)) != -1) {
//
for (int i = 0; i < bufferLength; i = i + 128) {
byte[] temp = new byte[128];
//
System.arraycopy(buffer, i, temp, 0, 128);
//
DmgcMpInteger ccc = new DmgcMpInteger(temp);
DmgcMpInteger pp = ccc.modPow(dd, nn);
//
//
byte[] plainTemp = pp.toByteArray();
byte[] plainText = new byte[128];
//
int length = plainTemp.length;
int padCount = 128 - length;
if (padCount > 0) {
byte[] paddZero = new byte[padCount];
System.arraycopy(paddZero, 0, plainText, 0, padCount);
System.arraycopy(plainTemp, 0, plainText, padCount, length);
}
else {
System.arraycopy(plainTemp, 0, plainText, padCount, length);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -