📄 hldecrsa.java
字号:
package HLDecRSA;import java.math.BigInteger;import java.io.*;import java.math.*;import java.security.*;import java.security.interfaces.*;public class HLDecRSA{//RSA解密类 public BigInteger dprk,nprk;//,encm; private String EncText="";//传入的密文 private String DecText="";//解得的明文 private byte[] DecBytes;//明文字节数组 private int SBStartPos;//DecBytes中可用空间的起始位置 private int ByteTextLength;//DecBytes长度 private String sectText;//EncText中的一段密文 private int TextPos;//EncText中的位置指针 public String getEncText(){ return new String(EncText); }//读取原始密文 public String getSectText(){ return new String(sectText); } public void clearDecText(){ DecText=""; TextPos=0; //reset TextPos } public String getDecText(){ return new String(DecText); }//读取解密后的明文 public String getSectDecText() throws UnsupportedEncodingException{ return new String(DecBytes,"UTF-8"); }//读取当前分段的明文 public Boolean ReadEncRsa(String encFilePath)throws Exception{//读取密文方法 File Encfile=new File(encFilePath); if(Encfile.exists()){//如果包含密文的文件存在,则读取密文 BufferedReader in=new BufferedReader(new InputStreamReader( new FileInputStream(encFilePath))); this.EncText=in.readLine(); in.close(); return true; } else return false; } public boolean nextSectEncText(){//取得下一分段密文 if(TextPos<this.EncText.length()){ int endPos=this.EncText.indexOf("|",TextPos); this.sectText=EncText.substring(TextPos,endPos); TextPos=endPos+1;//next TextPos return true; //not decode all yet } else return false; //finish } public int preDecRsa(){//提取当前分段密文所需的字节数组长度 int firstSeperatorPos=this.sectText.indexOf(">"); //读取存放明文字节数组所需的长度 this.ByteTextLength=Integer.parseInt( this.sectText.substring(0,firstSeperatorPos)); DecBytes=new byte[this.ByteTextLength]; //设置SBStartPos SBStartPos=0; //返回密文的真正起始位置 return firstSeperatorPos+1; } public void froDecRsa()//把分段解得的明文拼起来 throws UnsupportedEncodingException{ DecText+=new String(DecBytes,"UTF-8"); } public int DecRsa(int SegmentStartPos)throws Exception{//RSA解密方法 String EncTextSegment="";//密文段 int SegmentEndPos=0;//密文段结束字符的位置(在EncText中) int EncTextLength=sectText.length();//密文的长度(字符数) //确定分隔符的位置,并读取EncText从SegmentStartPos到分隔字符间的所有字符 SegmentEndPos=sectText.indexOf(">",SegmentStartPos); EncTextSegment=new String(sectText.substring(SegmentStartPos,SegmentEndPos)); SegmentStartPos=SegmentEndPos+1;//跳过'>'字符 //对当前这小段密文进行解密,添加到DecText中 BigInteger bigint=new BigInteger(EncTextSegment); byte[] tempBA=bigint.modPow(dprk,nprk).toByteArray(); System.arraycopy(tempBA,1,DecBytes,SBStartPos,tempBA.length-1); SBStartPos+=tempBA.length-1; return SegmentStartPos; } public Boolean ReadRsaKey(String rsaKeyPath)throws Exception{//读取解密密钥方法 File keyfile=new File(rsaKeyPath); if(keyfile.exists()){//如果包含解密密钥的文件存在,则读取密钥 FileInputStream f=new FileInputStream(rsaKeyPath); ObjectInputStream b=new ObjectInputStream(f); RSAPrivateKey prk=(RSAPrivateKey)b.readObject( ); BigInteger d=prk.getPrivateExponent(); BigInteger n=prk.getModulus(); this.dprk=d; this.nprk=n; return true; } else return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -