📄 rsaencryption.java
字号:
System.arraycopy(paddZero, 0, dest, 0, padCount);
System.arraycopy(tempc, 0, dest, padCount, length);
}
else {
System.arraycopy(tempc, 0, dest, padCount, length);
}
//
fOutput.write(dest, 0, 128);
} // if
}
//
ret = true;
fInput.close();
fOutput.close();
}
catch (FileNotFoundException ex) {
throw new FileNotFoundException("file not found");
}
catch (IOException ex) {
throw new IOException("IO Exception");
}
return ret;
}
/**
* 第四种方式:加密文件
* @param nfilename RSA大整数模的文件
* @param efilename RSA加密指数的文件
* @param inFile 要加密的文件
* @param outFile 输出的密文文件
* @return 真,操作成功,假,操作失败
* @throws FileNotFoundException 文件异常
* @throws IOException 输入输出异常
*/
public boolean FileEncryptionF(String nfilename, String efilename,
String inFile, String outFile) throws
FileNotFoundException, IOException {
boolean ret = false;
// 加密指数 模
DmgcMpInteger ee = null;
DmgcMpInteger nn = null;
FileInputStream fInputN = null;
FileInputStream fInputE = null;
// 密钥文件
try {
fInputE = new FileInputStream(efilename);
fInputN = new FileInputStream(nfilename);
int lengthN = fInputN.available();
int lengthE = fInputE.available();
byte[] BufferN = new byte[lengthN];
byte[] BufferE = new byte[lengthE];
lengthN = fInputN.read(BufferN);
lengthE = fInputE.read(BufferE);
byte[] N = new byte[lengthN];
System.arraycopy(BufferN, 0, N, 0, lengthN);
byte[] E = new byte[lengthE];
System.arraycopy(BufferE, 0, E, 0, lengthE);
ee = new DmgcMpInteger(E);
nn = new DmgcMpInteger(N);
fInputN.close();
fInputE.close();
}
catch (FileNotFoundException ex) {
throw new FileNotFoundException("file not found");
}
catch (IOException ex) {
throw new IOException("IO Exception");
}
// 文件加密
//
byte[] dest = new byte[128];
byte[] org = new byte[128];
byte[] temp = new byte[117];
//
byte[] buffer = new byte[7488];
int bufferLength = 0;
//
FileInputStream fInput = null;
FileOutputStream fOutput = null;
//
try {
// 输入文件
fInput = new FileInputStream(inFile);
// 输出文件
fOutput = new FileOutputStream(outFile);
//
while ( (bufferLength = fInput.read(buffer)) != -1) {
int i = 0;
for (i = 0; i < bufferLength; i = i + 117) {
// 每次读取117个字节
// 如果取到最后一次,则跳出
if (i + 117 >bufferLength) {
break;
}
// 每次117字节
System.arraycopy(buffer, i, temp, 0, 117);
// 开头两位标志位
org[0] = 0x00;
org[1] = 0x02;
int padCount = 8; //128 -length - 3;
// 填空位
byte[] padding = getPadding(padCount);
System.arraycopy(padding, 0, org, 2, padCount);
//标志位
org[padCount + 2] = 0x00;
//明文位
System.arraycopy(temp, 0, org, padCount + 3, 117);
// 加密
DmgcMpInteger ppp = new DmgcMpInteger(org);
DmgcMpInteger cc = ppp.modPow(ee, nn);
// 如果密文不是128字节也要填充0x00
byte[] tempc = cc.toByteArray();
int length = tempc.length;
padCount = 128 - length;
if (padCount > 0) {
byte[] paddZero = new byte[padCount];
// for (int j = 0; j < padCount; j++) {
// paddZero[j] = 0x00;
// }
System.arraycopy(paddZero, 0, dest, 0, padCount);
System.arraycopy(tempc, 0, dest, padCount, length);
}
else {
System.arraycopy(tempc, 0, dest, padCount, length);
}
//
fOutput.write(dest, 0, 128);
} // end for
// 如果有多余字节则进行下面操作
if ( (i !=0)&&(i < bufferLength) && ( (bufferLength - i) > 0)) {
int length = bufferLength - i;
System.arraycopy(buffer, i, temp, 0, length);
byte[] temp2 = new byte[length];
System.arraycopy(temp, 0, temp2, 0, length);
// 开头两位标志位
org[0] = 0x00;
org[1] = 0x02;
int padCount = 128 - length - 3;
// 填空位
byte[] padding = getPadding(padCount);
System.arraycopy(padding, 0, org, 2, padCount);
//标志位
org[padCount + 2] = 0x00;
//明文位
System.arraycopy(temp2, 0, org, padCount + 3, length);
//加密
DmgcMpInteger ppp = new DmgcMpInteger(org);
DmgcMpInteger cc = ppp.modPow(ee, nn);
// 如果密文不是128字节也要填充0x00
byte[] tempc = cc.toByteArray();
length = tempc.length;
padCount = 128 - length;
if (padCount > 0) {
byte[] paddZero = new byte[padCount];
// for (int j = 0; j < padCount; j++) {
// paddZero[j] = 0x00;
// }
System.arraycopy(paddZero, 0, dest, 0, padCount);
System.arraycopy(tempc, 0, dest, padCount, length);
}
else {
System.arraycopy(tempc, 0, dest, padCount, length);
}
//
fOutput.write(dest, 0, 128);
} // if
}
//
fInput.close();
fOutput.close();
ret = true;
}
catch (FileNotFoundException ex) {
throw new FileNotFoundException("file not found");
}
catch (IOException ex) {
throw new IOException("IO Exception");
}
return ret;
}
//----------------------------------------------------------------------------
/**
* 加密字符串
* @param keyfilename 公钥文件 n e
* @param plainText 明文,小于等于117字节
* @return 密文字节,128位
* @throws MessageTooLongException 消息太长
* @throws FileNotFoundException 文件没有找到
* @throws IOException IO 异常
* @throws InvalidKeyFileException 无效的公钥文件
*/
public byte[] NewMessageEncryptionF(String keyfilename,
byte[] plainText) throws
MessageTooLongException, FileNotFoundException,
InvalidKeyFileException, IOException {
//--------------------------------------------------------------------
// byte[] ret = new byte[128];
//
FileInputStream fInputKey = new FileInputStream(keyfilename);
int length = fInputKey.available();
if (length <= 128) {
throw new InvalidKeyFileException("Invalid Key file Exception");
}
//
byte[] Buffer = new byte[128];
//
int lengthN = fInputKey.read(Buffer);
byte[] N = new byte[lengthN];
System.arraycopy(Buffer, 0, N, 0, lengthN);
lengthN = fInputKey.read(Buffer);
byte[] E = new byte[lengthN];
System.arraycopy(Buffer, 0, E, 0, lengthN);
fInputKey.close();
DmgcMpInteger NN = new DmgcMpInteger(N);
DmgcMpInteger EE = new DmgcMpInteger(E);
// 以上为读取公钥
// ------------------------------------------------------------------------
byte[] ret = MessageEncryption(NN.toString(),EE.toString(),plainText);
// System.out.println(NN.toString());
// System.out.println(EE.toString());
return ret;
}
/**
* 加密文件
* @param keyfilename 密钥文件 n e
* @param inFile 输入文件
* @param outFile 输出文件
* @return 真,操作成功,假,操作失败
* @throws FileNotFoundException 文件异常
* @throws InvalidKeyFileException 无效的密钥异常
* @throws IOException IO 异常
*/
public boolean NewFileEncryptionF(String keyfilename,
String inFile, String outFile) throws
FileNotFoundException,InvalidKeyFileException, IOException {
//--------------------------------------------------------------------
// byte[] ret = new byte[128];
//
FileInputStream fInputKey = null;
try {
fInputKey = new FileInputStream(keyfilename);
}
catch (FileNotFoundException ex) {
throw new FileNotFoundException("File not found!");
}
int length = fInputKey.available();
if (length <= 128) {
throw new InvalidKeyFileException("Invalid Key file Exception");
}
//
byte[] Buffer = new byte[128];
//
int lengthN = fInputKey.read(Buffer);
byte[] N = new byte[lengthN];
System.arraycopy(Buffer, 0, N, 0, lengthN);
lengthN = fInputKey.read(Buffer);
byte[] E = new byte[lengthN];
System.arraycopy(Buffer, 0, E, 0, lengthN);
fInputKey.close();
DmgcMpInteger NN = new DmgcMpInteger(N);
DmgcMpInteger EE = new DmgcMpInteger(E);
// 以上为读取公钥
// ------------------------------------------------------------------------
boolean ret = FileEncryption(NN.toString(),EE.toString(),inFile,outFile);
return ret;
}
//----------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -