您当前的位置:首页 > 互联网教程

高分求java的RSA 和IDEA 加密解密算法

发布时间:2025-05-24 05:00:56    发布人:远客网络

高分求java的RSA 和IDEA 加密解密算法

一、高分求java的RSA 和IDEA 加密解密算法

取任何一个数e,要求满足e<t并且e与t互素(就是最大公因数为1)

设c=(M**d)%n就得到了加密后的消息c

设m=(c**e)%n则 m== M,从而完成对c的解密。

注:**表示次方,上面两式中的d和e可以互换。

n d两个数构成公钥,可以告诉别人;

n e两个数构成私钥,e自己保留,不让任何人知道。

给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。

别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。

rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解

从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法

接下来我们来一个实践,看看实际的操作:

取e=63,满足e<t并且e和t互素

用perl简单穷举可以获得满主 e*d%t==1的数d:

C:\Temp>perl-e"foreach$i(1..9999){ print($i),last if$i*63%2668==1}"

C:\Temp>perl-Mbigint-e"print 244**847%2773"

即用d对M加密后获得加密信息c=465

我们可以用e来对加密后的c进行解密,还原M:

C:\Temp>perl-Mbigint-e"print 465**63%2773"

即用e对c解密后获得m=244,该值和原始信息M相等。

把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了。

每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制

的数的字符串形式,按3字节表示,如01F

#RSA计算过程学习程序编写的测试程序

my%RSA_CORE=(n=>2773,e=>63,d=>847);#p=47,q=59

my$N=new Math::BigInt($RSA_CORE{n});

my$E=new Math::BigInt($RSA_CORE{e});

my$D=new Math::BigInt($RSA_CORE{d});

for($i=0;$i< length($$r_mess);$i++)

$c=ord(substr($$r_mess,$i,1));

$C=$M->copy();$C->bmodpow($D,$N);

for($i=0;$i< length($$r_mess);$i+=3)

$C=$M->copy();$C->bmodpow($E,$N);

$mess=$ARGV[0] if@ARGV>= 1;

print"原始串:",$mess,"\n";

my$r_cmess= RSA_ENCRYPT(\$mess);

print"加密串:",$$r_cmess,"\n";

my$r_dmess= RSA_DECRYPT($r_cmess);

print"解密串:",$$r_dmess,"\n";

C:\Temp>perl rsa-test.pl

加密串:5CB6CD6BC58A7709470AA74A0AA74A0AA74A6C70A46C70A46C70A4

C:\Temp>perl rsa-test.pl安全焦点(xfocus)

加密串:3393EC12F0A466E0AA9510D025D7BA0712DC3379F47D51C325D67B

前面已经提到,rsa的安全来源于n足够大,我们测试中使用的n是非常小的,根本不能保障安全性,

我们可以通过RSAKit、RSATool之类的工具获得足够大的N及D E。

通过工具,我们获得1024位的N及D E来测试一下:

n=0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90B66EC3A85F5005D

BDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF1538D4C2013433B383B

47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941D2ED173CCA50E114705D7E2

e=0xE760A3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C2995

4C5D86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF2

C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A592D2B

M=0x11111111111122222222222233333333333

完成这么大数字的计算依赖于大数运算库,用perl来运算非常简单:

C:\Temp>perl-Mbigint-e"$x=Math::BigInt->bmodpow(0x11111111111122222222222233

333333333, 0x10001, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5F

CD15F90B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F0

17F9CCF1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD6

0438941D2ED173CCA50E114705D7E2BC511951);print$x->as_hex"

0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd

45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b

3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91

c=0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd

45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b

3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91

C:\Temp>perl-Mbigint-e"$x=Math::BigInt->bmodpow(0x17b287be418c69ecd7c39227ab

681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd45692b007f3a2f7c5f5aa1d99ef3

866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b3028f9461a3b1533ec0cb4764414

65f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91f1834580c3f6d90898, 0xE760A

3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C29954C5D

86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF

2C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A

592D2B1965, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90

B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF

1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941

D2ED173CCA50E114705D7E2BC511951);print$x->as_hex"

0x11111111111122222222222233333333333

(我的P4 1.6G的机器上计算了约5秒钟)

得到用e解密后的m=0x11111111111122222222222233333333333== M

RSA简洁幽雅,但计算速度比较慢,通常加密中并不是直接使用RSA来对所有的信息进行加密,

最常见的情况是随机产生一个对称加密的密钥,然后使用对称加密算法对信息加密,之后用

最后需要说明的是,当前小于1024位的N已经被证明是不安全的

自己使用中不要使用小于1024位的RSA,最好使用2048位的。

----------------------------------------------------------

一个简单的RSA算法实现JAVA源代码:

* TODO To change the template for this generated file go to

* Window- Preferences- Java- Code Style- Code Templates

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileNotFoundException;

import java.io.BufferedReader;

import java.util.StringTokenizer;

* TODO To change the template for this generated type comment go to

* Window- Preferences- Java- Code Style- Code Templates

private static final BigInteger ZERO= BigInteger.ZERO;

private static final BigInteger ONE= BigInteger.ONE;

private static final BigInteger TWO= new BigInteger("2");

public RSA(BigInteger key, BigInteger n, int b){

public void encodeFile(String filename){

byte[] bytes= new byte[blockSize/ 8+ 1];

is= new FileInputStream(filename);

writer= new FileWriter(filename+".enc");

catch(FileNotFoundException e1){

System.out.println("File not found:"+ filename);

System.out.println("File not found:"+ filename+".enc");

* Write encoded message to'filename'.enc

while((tempLen= is.read(bytes, 1, blockSize/ 8))> 0){

for(int i= tempLen+ 1; i< bytes.length;++i){

writer.write(encodeDecode(new BigInteger(bytes))+"");

System.out.println("error writing to file");

* Close input stream and file writer

System.out.println("Error closing file.");

public void decodeFile(String filename){

reader= new FileReader(filename);

os= new FileOutputStream(filename.replaceAll(".enc",".dec"));

catch(FileNotFoundException e1){

System.out.println("File not found:"+ filename);

System.out.println("File not found:"+ filename.replaceAll(".enc","dec"));

BufferedReader br= new BufferedReader(reader);

st= new StringTokenizer(br.readLine());

toFile= encodeDecode(new BigInteger(st.nextToken())).toByteArray();

System.out.println(toFile.length+" x"+(blockSize/ 8));

if(toFile[0]== 0&& toFile.length!=(blockSize/ 8)){

temp= new byte[blockSize/ 8];

offset= temp.length- toFile.length;

for(int i= toFile.length- 1;(i<= 0)&&((i+ offset)<= 0);--i){

/*if(toFile.length!=((blockSize/ 8)+ 1)){

temp= new byte[(blockSize/ 8)+ 1];

System.out.println(toFile.length+" x"+ temp.length);

for(int i= 1; i< temp.length; i++){

System.out.println(toFile.length+""+((blockSize/ 8)+ 1));*/

System.out.println("Something went wrong");

System.out.println("Error closing file.");

* Performs<tt>base</tt>^<sup><tt>pow</tt></sup> within the modular

* domain of<tt>mod</tt>.

*@param base the base to be raised

*@param pow the power to which the base will be raisded

*@param mod the modular domain over which to perform this operation

*@return<tt>base</tt>^<sup><tt>pow</tt></sup> within the modular

* domain of<tt>mod</tt>.

public BigInteger encodeDecode(BigInteger base){

在这里提供两个版本的RSA算法JAVA实现的代码下载:

1.来自于 的RSA算法实现源代码包:

2.来自于 的实现:

源代码包

编译好的jar包

另外关于RSA算法的php实现请参见文章:

关于使用VB实现RSA算法的源代码下载(此程序采用了psc1算法来实现快速的RSA加密):

RSA加密的JavaScript实现:

二、如何用JAVA实现字符串简单加密解密

1、Java中可以使用多种加密算法来加密字符串,例如DES算法。下面通过一个示例来展示如何使用Java实现字符串的加密和解密。

2、首先,我们需要创建一个密钥。这可以通过调用`KeyGenerator`类的`getInstance`方法来实现。这里我们以DES算法为例:

3、KeyGenerator keygen= KeyGenerator.getInstance("DES");

4、接下来,我们需要初始化`KeyGenerator`对象,并生成一个密钥:

5、使用生成的密钥进行加密操作。这里我们定义一个方法`encryptToDES`,接受一个密钥和要加密的信息,返回加密后的信息:

6、public String encryptToDES(SecretKey key, String info){...}

7、解密操作则通过另一个方法`decryptByDES`来实现,该方法接受密钥和要解密的密文,返回解密后的信息:

8、public String decryptByDES(SecretKey key, String sInfo){...}

9、此外,还可以使用其他加密算法,如MD5和SHA-1。这里提供一个MD5加密方法`encryptToMD5`:

10、public String encryptToMD5(String info){...}

11、以及一个SHA-1加密方法`encryptToSHA`:

12、public String encryptToSHA(String info){...}

13、通过这些方法,我们可以方便地对字符串进行加密和解密操作。同时,还可以使用公钥和私钥进行数字签名和验证,确保信息的完整性和安全性。

14、示例代码中还包含了一些辅助方法,如将二进制转化为16进制字符串`byte2hex`,以及将十六进制字符串转化为二进制`hex2byte`。这些方法在加密和解密过程中起到了关键作用。

15、通过上述方法,我们可以灵活地在Java中实现字符串的加密和解密,确保数据的安全传输和存储。

三、请问用java如何对文件进行加密解密

packagecom.palic.pss.afcs.worldthrough.common.util;

importjavax.crypto.spec.SecretKeySpec;

importrepack.com.thoughtworks.xstream.core.util.Base64Encoder;

publicstaticfinalStringcKey="assistant7654321";

*加密--把加密后的byte数组先进行二进制转16进制在进行base64编码

publicstaticStringencrypt(StringsSrc,StringsKey)throwsException{

thrownewIllegalArgumentException("ArgumentsKeyisnull.");

thrownewIllegalArgumentException(

"ArgumentsKey'lengthisnot16.");

byte[]raw=sKey.getBytes("ASCII");

SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");

Ciphercipher=Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE,skeySpec);

byte[]encrypted=cipher.doFinal(sSrc.getBytes("UTF-8"));

StringtempStr=parseByte2HexStr(encrypted);

Base64Encoderencoder=newBase64Encoder();

returnencoder.encode(tempStr.getBytes("UTF-8"));

*解密--先进行base64解码,在进行16进制转为2进制然后再解码

publicstaticStringdecrypt(StringsSrc,StringsKey)throwsException{

thrownewIllegalArgumentException("499");

thrownewIllegalArgumentException("498");

byte[]raw=sKey.getBytes("ASCII");

SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");

Ciphercipher=Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE,skeySpec);

Base64Encoderencoder=newBase64Encoder();

byte[]encrypted1=encoder.decode(sSrc);

StringtempStr=newString(encrypted1,"utf-8");

encrypted1=parseHexStr2Byte(tempStr);

byte[]original=cipher.doFinal(encrypted1);

StringoriginalString=newString(original,"utf-8");

publicstaticStringparseByte2HexStr(bytebuf[]){

StringBuffersb=newStringBuffer();

for(inti=0;i<buf.length;i++){

Stringhex=Integer.toHexString(buf[i]&0xFF);

sb.append(hex.toUpperCase());

publicstaticbyte[]parseHexStr2Byte(StringhexStr){

byte[]result=newbyte[hexStr.length()/2];

for(inti=0;i<hexStr.length()/2;i++){

inthigh=Integer.parseInt(hexStr.substring(i*2,i*2+1),16);

intlow=Integer.parseInt(hexStr.substring(i*2+1,i*2+2),

result[i]=(byte)(high*16+low);

publicstaticvoidmain(String[]args)throwsException{

*加密用的Key可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定

StringcKey="assistant7654321";

longlStart=System.currentTimeMillis();

StringenString=encrypt(cSrc,cKey);

System.out.println("加密后的字串是:"+enString);

longlUseTime=System.currentTimeMillis()-lStart;

System.out.println("加密耗时:"+lUseTime+"毫秒");

lStart=System.currentTimeMillis();

StringDeString=decrypt(enString,cKey);

System.out.println("解密后的字串是:"+DeString);

lUseTime=System.currentTimeMillis()-lStart;

System.out.println("解密耗时:"+lUseTime+"毫秒");