📄 decrydata2function.java
字号:
package mircoenorde.grade2;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.SecretKey;
import java.security.Security;
import javax.crypto.spec.SecretKeySpec;
import java.io.ObjectInputStream;
import javax.crypto.Cipher;
import java.io.FileOutputStream;
import java.security.MessageDigest;
import java.io.File;
import mircoenorde.common.exception.MircoenordeException;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DecryData2Function {
public void decryData2Funtion(String fileIN, String fileOUT,
String userpassword,
int grade, int delFile) throws
MircoenordeException {
try {
if (grade == 2) { //首先匹配加密级别;
//第一步,从指定目录读入加密文件;
ObjectInputStream objIN = new ObjectInputStream(new
BufferedInputStream(new FileInputStream(fileIN)));
EncryData2 degrade2 = (EncryData2) objIN.readObject();
objIN.close();
String filename = degrade2.getfilename();
//第二步,得到userpassword的摘要值,并取其前面128位用来构造AES对称密钥;
Security.addProvider(new BouncyCastleProvider()); //加入算法提供者;
MessageDigest passwordhash = MessageDigest.getInstance("SHA1",
"BC");
//passwordhash.update(userpassword.getBytes());
byte[] passwordhashbyte = passwordhash.digest(userpassword.
getBytes());
byte[] mesdigest = degrade2.getmesdigest();
//第三步,比较Hash值,如果一致就解密,否则就报出错信息;
boolean hashboolean = true; //注意口令匹配方式;
for (int i = 0; i < 20; i++) {
if (passwordhashbyte[i] == mesdigest[i]) {
} else {
hashboolean = false;
throw new MircoenordeException(1, "密码错误,请重新输入!!!");
}
}
System.out.println(hashboolean);
if (hashboolean) {
//第四步,得到userpassword的摘要值,并取其前面128位用来构造AES对称密钥;
byte[] passwordhash128 = new byte[16];
for (int i = 0; i < 16; i++) {
passwordhash128[i] = passwordhashbyte[i];
}
SecretKey passwordhash128AES = new SecretKeySpec(
passwordhash128,
"DES"); //将passwordhash64变幻成DES密钥;
//第五步,解密aeskey1
Cipher userCipher = Cipher.getInstance(
"AES/ECB/PKCS5Padding",
"BC");
userCipher.init(Cipher.DECRYPT_MODE, passwordhash128AES);
//userCipher.update(degrade1.getenaeskeybyte());
SecretKey aeskey1 = new SecretKeySpec(userCipher.doFinal(
degrade2.getenaeskeybyte()),
"AES"); //至此已将aeskey1解密;下面就用它把content解密;
//第六步,使用刚解密的aeskey1来解密content;
//这里不必重新定义加密机;
// Cipher secondCipher = Cipher.getInstance("DES","BC");
userCipher.init(Cipher.DECRYPT_MODE, aeskey1); //重新初始化;
// userCipher.update(degrade1.getcontent());
byte[] content = userCipher.doFinal(degrade2.getcontent());
//第七步,将文件写到指定目录;
FileOutputStream fout = new FileOutputStream(fileOUT +
filename);
fout.write(content);
fout.close();
if (delFile == 1) {
File file = new File(fileIN);
file.delete();
}
} else {
System.out.println("口令出错!!!");
System.out.println("你最多只有三次机会!!!");
}
} else {
System.out.println("***********出错***********");
}
} catch (MircoenordeException ex) {
throw new MircoenordeException(1, "密码错误,请重新输入!!!");
} catch (Exception ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -