RSA算法的实现实验报告

RSA算法的实现

一、实验目的

1. 熟悉公钥密码体制;

2.掌握产生密钥对的程序设计方法;

3.掌握产生加密/解密的程序设计方法。

二、实验内容和要求

1.进行RSA加密/解密算法的设计;

2.对RSA程序进行编译和调试;

3.使用编写的程序进行加密和解密。

三、实验环境

运行Windows操作系统的PC机,可以利用具有VC++语言环境;如果所运用的语言是JAVA,那么也可以利用JAVA语言环境来实现RSA算法的加密和解密。

四、实验步骤

1.采用C++语言进行本次实验的编写,实验的代码如下:

#include <stdio.h>

#include<conio.h>

int candp(int a,int b,int c)

{ int r=1;

b=b+1;

while(b!=1)

{

    r=r*a;

    r=r%c;

    b--;

}

printf("%d\n",r);

return r;

}

void main()

{

int p,q,e,d,m,n,t,c,r;

char s;

printf("please input the p,q: ");

scanf("%d%d",&p,&q);

n=p*q;

printf("the n is %3d\n",n);

t=(p-1)*(q-1);

printf("the t is %3d\n",t);

printf("please input the e: ");

scanf("%d",&e);

if(e<1||e>t)

{

     printf("e is error,please input again: ");

     scanf("%d",&e);

}

d=1;

while(((e*d)%t)!=1)   d++;

printf("then caculate out that the d is %d\n",d);

printf("the cipher please input 1\n");

printf("the plain please input 2\n");

scanf("%d",&r);

switch(r)

{

    case 1: printf("input the m: "); /*输入要加密的明文数字*/

            scanf("%d",&m);

            c=candp(m,e,n);

            printf("the cipher is %d\n",c);break;

    case 2: printf("input the c: "); /*输入要解密的密文数字*/

            scanf("%d",&c);

            m=candp(c,d,n);

            printf("the cipher is %d\n",m);break;

}

getch();

}

2、代码的思想:首先随意输入两个素数p和q,然后利用算法计算出p*q即n,再算出(p-1)*(q-1)即t,并且同时输出计算的结果n和t,接下来输入e,经过算法可以计算出d,由此可以知道RSA算法的公钥和私钥;接下来可以有两个选择:一选择输入明文,有明文经过算法可以计算出密文;二输入密文,有密文经过算法可以计算出明文。

3、运行以上代码就可以得到实验的结果。

五、实验结果

    实验结果如下图所示:

六、实验心得:

通过这次的实验,了解了非对称密码算法RSA,会运用一些现成的算法进行编程,对一些比较复杂的算法开始基本认识并深刻的掌握。在以后所涉及这方面的知识将会有全新的了解和掌握。

实验报告

                                

 

第二篇:JAVA实现RSA算法

数据结构:JAVA实现RSA算法

实现一对密钥对整个项目所有加密解密文件都适用的方法,采用先生成一对密钥.保存到xml文件中,以后获得私匙和公钥只需要从xml文件中取得就可以了.

/**

* 把成生的一对密钥保存到RSAKey.xml文件中

*/

public void saveRSAKey() {

try {

SecureRandom sr = new SecureRandom();

KeyPairGenerator kg = KeyPairGenerator.getInstance(\"RSA\",

new org.bouncycastle.jce.provider.BouncyCastleProvider());

//注意密钥大小最好为1024,否则解密会有乱码情况.

kg.initialize(1024, sr);

FileOutputStream fos = new FileOutputStream(\"C:/RSAKey.xml\");

ObjectOutputStream oos = new ObjectOutputStream(fos);

//生成密钥

oos.writeObject(kg.generateKeyPair());

oos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

注意:需要从下载bcprov-jdk14-137.jar包.

获取密钥方法如下:

/**

* 获得RSA加密的密钥。

* @return KeyPair返回对称密钥

*/

public static KeyPair getKeyPair() {

//产生新密钥对

KeyPair kp;

try {

String fileName = \"conf/RASKey.xml\";

InputStream is = FileUtils.class.getClassLoader()

.getResourceAsStream(fileName);

ObjectInputStream oos = new ObjectInputStream(is);

kp = (KeyPair) oos.readObject();

oos.close();

} catch (Exception e) {

throw new EprasRuntimeException(\"读取加密文件出错.\", e);

}

return kp;

}

文件采用RSA算法加密文件

/** * 文件file进行加密并保存目标文件destFile中 * @param srcFileName * 要加密的文件 如c:/test/srcFile.txt * @param destFileName * 加密后存放的文件名 如c:/加密后文件.txt */ public static void encryptFile(String srcFileName, String destFileName) throws Exception { OutputStream outputWriter = null; InputStream inputReader = null; try { Cipher cipher = Cipher.getInstance(\"RSA/ECB/PKCS1Padding\", new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] buf = new byte[100]; int bufl; cipher.init(Cipher.ENCRYPT_MODE, getKeyPair().getPublic()); outputWriter = new FileOutputStream(destFileName); inputReader = new FileInputStream(srcFileName); while ((bufl = inputReader.read(buf)) != -1) { byte[] encText = null; byte[] newArr = null; if (buf.length == bufl) { newArr = buf; } else { newArr = new byte[bufl]; for (int i = 0; i < bufl; i++) { newArr = (byte) buf; } } encText = cipher.doFinal(newArr); outputWriter.write(encText); } outputWriter.flush(); } catch (Exception e) { throw e; } finally { try { if (outputWriter != null) { outputWriter.close(); } if (inputReader != null) { inputReader.close(); }

} catch (Exception e) { } } } 文件采用RSA算法解密文件 /** * 文件file进行加密并保存目标文件destFile中 * @param srcFileName * 已加密的文件 如c:/加密后文件.txt * @param destFileName * 解密后存放的文件名 如c:/ test/解密后文件.txt */ public static void decryptFile(String srcFileName, String destFileName) throws Exception { OutputStream outputWriter = null; InputStream inputReader = null; try { Cipher cipher = Cipher.getInstance(\"RSA/ECB/PKCS1Padding\", new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] buf = new byte[128]; int bufl; cipher.init(Cipher.DECRYPT_MODE, getKeyPair().getPrivate()); outputWriter = new FileOutputStream(destFileName); inputReader = new FileInputStream(srcFileName); while ((bufl = inputReader.read(buf)) != -1) { byte[] encText = null; byte[] newArr = null; if (buf.length == bufl) { newArr = buf; } else { newArr = new byte[bufl]; for (int i = 0; i < bufl; i++) { newArr = (byte) buf; } } encText = cipher.doFinal(newArr); outputWriter.write(encText); } outputWriter.flush(); } catch (Exception e) { throw e; } finally { try { if (outputWriter != null) {

outputWriter.close(); } if (inputReader != null) { inputReader.close(); } } catch (Exception e) { } } } 如果对于大文件加密采用RSA算法执行速度要非常非常慢。

相关推荐