请教一个RSA算法中的问题

今天看了CSDN上一篇RSA算法的文章。发现当文本中有多行文字时,解密会无法体现。我初步认为是public void dataEncryption(String source,String destination)这个函数中

long filelength = rafr.length()这个地方有问题,因为回车符号长度为2,所以在加密是无法正确加密。大家讨论看看是否可以解决这个问题呢?

 

代码如下

 

 

import java.io.*;

import java.util.Random;

 

/** *//**

*Rsa非对称加密的实现

*@author Shaojun Huang

*@version 1.0

*/

public class RSA1

{

    private int keyn;            //公私钥相同部分

    private int publickeye;        //公钥

    private int privatekeyd;    //私钥

 

    /** *//**

    *Rsa构造器,初始化对象

    */

    public RSA1()

    {

        int randomprimep = 0;    //随机产生素数

        int randomprimeq = 0;    //随机产生素数

        int eulern = 0;    //公私钥相同部分的欧拉数

        Random rand = new Random();

        randomprimep = 10 + rand.nextInt(200);

        while (ifPrime(randomprimep))    //判断是否为素数

        {

            randomprimep = 10 + rand.nextInt(200);

        }

        randomprimeq = 10 + rand.nextInt(200);

        while (ifPrime(randomprimeq))    //判断是否为素数

        {

            randomprimeq = 10 + rand.nextInt(200);

        }

        keyn = randomprimep * randomprimeq;

        eulern = (randomprimep - 1) * (randomprimeq - 1);

        randomprimep = randomprimeq = 0;    //销毁两个随机素数

        publickeye = 10 + rand.nextInt(200);        //随机产生公钥

        while (relativePrime(publickeye,eulern))    //判断随机公钥与公私钥相同部分的欧拉数是否为相对素数

        {

            publickeye = 10 + rand.nextInt(200);

        }

        boolean mark = true;    //用简捷方法求私钥,从模逆运算原始定义出发

        int k = 0;

        while (mark)

        {

            k++;

            if((k * eulern + 1)%publickeye == 0)

                mark = false;

        }

        privatekeyd = (k * eulern + 1)/publickeye;        //计算私钥

        System.out.println("公钥为:(" + publickeye + "," + keyn + ")");

        System.out.println("私钥为:(" + privatekeyd + "," + keyn + ")");

    }

 

    /** *//**

    *判断一个整型数据是否为素数

    *@param number 待判断数据

    *@return true --不是素数,false -- 是素数

    */

    public boolean ifPrime(int number)

    {

        boolean mark = false;

        for (int i = 2;i < number/2 ;i++ )

        {

            if (number%i == 0)

            {

                mark = true;

                break;

            }

        }

        return mark;

    }

 

    /** *//**

    *判断两个整数数据是否为素数

    *@param e 整型数据

    *@param n 整型数据

    *@return true -- 不是否为素数,fasle -- 是互为素数

    */

    public boolean relativePrime(int e,int n)

    {

        boolean mark = false;

        for (int i = 2;i <= n ;i++ )

       {

            if (n%i == 0)

                if (e%i == 0)

               {

                    mark = true;

                    break;

                }

        }

        return mark;

    }

 

    /** *//**

    *模n的大数幂乘快速运算

    *@param a 底数

    *@param b 指数

    *@param n 模数

    *@return 大数幂乘结果

    */

    public int fastPowerMultiplication(int a,int b,int n)

    {

        int c = 1;

        while (b != 0)

        {

            while (b%2 == 0)

            {

                b = b/2;

                a = ( a * a )%n;

            }

            b = b - 1;

            c = ( c * a )%n;

        }

        return c;

    }

 

 

    /** *//**

    *求一个整型数据的欧拉数

    *@param number 待求欧拉数的数据

    *@return 欧拉数

    */

    public int getPrivateKeyexp(int number)

    {

        int eulernumber = 0;

        for (int i = 2;i < number ;i++ )

        {

            if(relativePrime(i,number) == false)

           {

                System.out.println("相互素数:" + i + "," + number);

                eulernumber++;

            }

        }

        System.out.println(eulernumber);

        return eulernumber + 1;

    }

 

    /** *//**

    *对明文加密

    *@param source 明文源地址

    *@param destination 密文目的地

    */

    public void dataEncryption(String source,String destination)

   {

        try

        {

            RandomAccessFile rafr = new RandomAccessFile(source,"r");

            RandomAccessFile rafw = new RandomAccessFile(destination,"rw");

            String understandtxts = "";

            String passwordtxts = "";

            char [] eachchar;

            int charint = 0;

            boolean mark = false;

            try

            {

                long filelength = rafr.length();

                long pos = 0;

                while (pos != filelength)

                {

                    understandtxts = rafr.readLine();

                    eachchar = understandtxts.toCharArray();

                    for (int i = 0 ;i < eachchar.length ;i++ )

                    {

                        charint = (int)eachchar[i];

                        if (charint > 127 || charint < 10)

                        {

                            System.out.println("加密失败!原文中包含不能加密的字符。");

                            mark = true;

                            break;

                        }

                    }

                    if (mark)

                    {

                        break;

                    }

                    pos = rafr.getFilePointer();

                }

                rafr.seek(0);

                pos = 0;

                if (!mark)

                {

                    System.out.println("从文件读入明文:");

                    while (pos != filelength)

                    {

                        System.out.println(rafr.readLine());

                        pos = rafr.getFilePointer();

                    }

                    rafr.seek(0);

                    pos = 0;

                    System.out.println("明文加密后密文:");

                    while (true)

                    {

                        understandtxts = rafr.readLine();

                        eachchar = understandtxts.toCharArray();

                        for (int i = 0;i < eachchar.length ;i++ )

                        {

                            passwordtxts = passwordtxts + fastPowerMultiplication((int)eachchar[i],publickeye,keyn) + getRandomString();

                        }

                        System.out.println(passwordtxts);

                        rafw.writeBytes(passwordtxts);

                        pos = rafr.getFilePointer();

                        if (pos == filelength)

                        {

                            break;

                        }

                        passwordtxts = "";

                        rafw.writeChar(' ');

                    }

                }

            }

            catch (Exception e)

            {

                e.printStackTrace(System.out);

            }

            finally

            {

                rafr.close();

                rafw.close();

            }

        }

        catch (IOException ex)

        {

            ex.printStackTrace(System.out);

        }

    }

 

    /** *//**

    *随机生成1~5个随机大写字母

    */

    public String getRandomString()

    {

        Random rand = new Random();

        String randstr = "";

        int number = 1 + rand.nextInt(5);

        int charint = 65 + rand.nextInt(26);

        for (int i = 0;i < number ;i++ )

        {

            randstr = randstr + (char)charint;

            charint = 65 + rand.nextInt(26);

        }

        return randstr;

    }

 

    /** *//**

    *对密文解密

    *@param source 密文源地址

    *@param destination 明文目的址

    */

    public void dataDeciphering(String source,String destination)

   {

        try

        {

            RandomAccessFile rafr = new RandomAccessFile(source,"r");

            RandomAccessFile rafw = new RandomAccessFile(destination,"rw");

            String understandtxts = "";

            String passwordtxts = "";

            char [] eachchar;

            int intchar = 0;

            boolean mark = false;

            try

            {

                long filelength = rafr.length();

                long pos = 0;

                int j = 0;

                System.out.println("密文解密后明文:");

                while (true)

                {

                    passwordtxts = rafr.readLine();

                    eachchar = passwordtxts.toCharArray();

                    for (int i = 0;i < eachchar.length ;i++ )

                    {

                        if (Character.isDigit(eachchar[i]))

                       {

                            intchar = 10*intchar + Integer.valueOf(String.valueOf(eachchar[i]));

                        }

                        else

                        {

                            j = i - 1;

                            if (Character.isDigit(eachchar[j]) && !Character.isDigit(eachchar[i]))

                            {

                                understandtxts = understandtxts + (char)fastPowerMultiplication(intchar,privatekeyd,keyn);

                                intchar = 0;

                            }

                        }

                    }

                    System.out.println(understandtxts);

                    rafw.writeBytes(understandtxts);

                    pos = rafr.getFilePointer();

                    if (pos == filelength)

                    {

                        break;

                    }

                    System.out.println(' ');

                    understandtxts = "";

                }

            }

            catch (Exception e)

            {

                e.printStackTrace(System.out);

            }

            finally

            {

                rafr.close();

                rafw.close();

            }

        }

        catch (IOException ex)

        {

            ex.printStackTrace(System.out);

        }

    }

    public static void main(String[] args)

    {

        RSA1 rsa = new RSA1();

        System.out.println();

        rsa.dataEncryption("D:/1.txt","D:/2.txt");

        rsa.dataDeciphering("D:/2.txt","D:/3.txt");

    }

}

 

 

### 回答1: 下面是一个简单的Java代码实现RSA算法: ``` import java.math.BigInteger; import java.security.SecureRandom; public class RSA { private final static BigInteger one = new BigInteger("1"); private BigInteger privateKey; private BigInteger publicKey; private BigInteger modulus; RSA(int keySize) { SecureRandom rnd = new SecureRandom(); BigInteger p = new BigInteger(keySize / 2, 100, rnd); BigInteger q = new BigInteger(keySize / 2, 100, rnd); BigInteger phi = (p.subtract(one)).multiply(q.subtract(one)); modulus = p.multiply(q); publicKey = new BigInteger("65537"); privateKey = publicKey.modInverse(phi); } BigInteger encrypt(BigInteger message) { return message.modPow(publicKey, modulus); } BigInteger decrypt(BigInteger encrypted) { return encrypted.modPow(privateKey, modulus); } public static void main(String[] args) { RSA rsa = new RSA(2048); BigInteger plainText = new BigInteger(2048, new SecureRandom()); BigInteger cipherText = rsa.encrypt(plainText); BigInteger decipheredText = rsa.decrypt(cipherText); System.out.println("Plain Text : " + plainText); System.out.println("Cipher Text : " + cipherText); System.out.println("Deciphered Text : " + decipheredText); } } ``` 这是一个简单的RSA算法实现,用于加密解密。请注意,这不是用于生产环境的完整实现,因为它没有实现关键部分的安全性。 ### 回答2: RSA算法是一种非对称加密算法,可以用于数据加密和数字签名。下面是一个用Java编写的简化版RSA算法示例: ```java import java.util.Random; import java.math.BigInteger; public class RSAAlgorithm { private BigInteger p; // 素数p private BigInteger q; // 素数q private BigInteger n; // n = p * q private BigInteger phi; // φ(n) = (p - 1) * (q - 1) private BigInteger e; // 公钥e private BigInteger d; // 私钥d public RSAAlgorithm() { // 生成两个随机素数p和q p = generatePrimeNumber(); q = generatePrimeNumber(); // 计算n和phi n = p.multiply(q); phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); // 选择公钥e e = generatePublicKey(); // 计算私钥d d = e.modInverse(phi); } // 生成一个随机素数 private BigInteger generatePrimeNumber() { Random random = new Random(); return BigInteger.probablePrime(1024, random); } // 选择公钥 private BigInteger generatePublicKey() { Random random = new Random(); BigInteger publicKey; do { publicKey = BigInteger.probablePrime(1024, random); } while (publicKey.compareTo(phi) >= 0 || !publicKey.gcd(phi).equals(BigInteger.ONE)); return publicKey; } // 加密 public BigInteger encrypt(BigInteger message) { return message.modPow(e, n); } // 解密 public BigInteger decrypt(BigInteger cipher) { return cipher.modPow(d, n); } public static void main(String[] args) { RSAAlgorithm rsa = new RSAAlgorithm(); BigInteger message = new BigInteger("1234567890"); BigInteger cipher = rsa.encrypt(message); BigInteger decryptedMessage = rsa.decrypt(cipher); System.out.println("原始消息: " + message); System.out.println("加密后的密文: " + cipher); System.out.println("解密后的消息: " + decryptedMessage); } } ``` 该示例实现了RSA算法的基本功能,包括生成公钥和私钥、加密解密操作。在main方法,我们生成了一个随机消息,使用公钥进行加密,然后使用私钥进行解密,最后打印出原始消息和解密后的消息供验证。请注意,该示例为简化版本,仅供演示,实际使用时需要考虑更多的安全性和性能问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值