0

我进行了以下导入:

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.crypto.*;
import java.util.Scanner;
import java.security.spec.*;

我已经将变量 N​​ 声明为 largeBigInteger并且 done RSAPublicKeySpec pub=new RSAPublicKeySpec(N, new BigInteger("65537"));。我也有一个字符串数组ls。为了简单起见,ls 定义为String[] ls= {"Abc","abc","a"};.

我使用以下代码创建了一个密码

Cipher cipher= Cipher.getInstance("RSA");
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey publ=(RSAPublicKey) keyFactory.generatePublic(pub);
System.out.println(publ.getPublicExponent()); System.out.println(publ.getModulus()); System.out.println(N);
cipher.init(Cipher.ENCRYPT_MODE, publ);

然后我运行了这个循环:

System.out.println(cipher.doFinal());
int i;
for(i=0;i<3;i++){
    byte[] mb=cipher.doFinal(ls[i].getBytes());
    BigInteger m=new BigInteger(mb);
    System.out.println(ls[i]); System.out.println(mb); System.out.println(m);}
System.out.println(i);

当完整的代码运行时,它会输出随机结果,尽管我指定了一个特定的键并且没有额外的随机性。

我得到的一个示例结果:

65537
8876876877514683419701394775736891208848228258165890045890388820357265315302111630148566347729740003117740265768481144604003358339087090263495975493871517
8876876877514683419701394775736891208848228258165890045890388820357265315302111630148566347729740003117740265768481144604003358339087090263495975493871517
[B@4dcbadb4
Abc
[B@4e515669
-5462094712561744024560535444520088624777610175171976781234077640626971429079045151098460959562949209262068937820990160757259828914166157234608976597064350
abc
[B@17d10166
3107323907558141358810822022103673043984543035554223372765982648017786708913920092191193838772862316322034375980838715614109687358524365202642588530273930
a
[B@1b9e1916
6445163883197255205896338963911279787620368657249049376589703029248914873890907191971001889147477506682665011145469994788929208061633206661616367855217482
3

运行相同代码时的另一个示例:

65537
8876876877514683419701394775736891208848228258165890045890388820357265315302111630148566347729740003117740265768481144604003358339087090263495975493871517
8876876877514683419701394775736891208848228258165890045890388820357265315302111630148566347729740003117740265768481144604003358339087090263495975493871517
[B@61e4705b
Abc
[B@50134894
2417384835689897675160227748639975454110518566516301711167733068619357149939147252927780753402294448641286117754069483471011084747372041883630633605894839
abc
[B@2957fcb0
3879287039536019818188188335652449442503231260096157847006548762163832071824706188511894185312719300152349914092885760207457631282795049343998798324887051
a
[B@1376c05c
2592101622106234263448104107242829911318871603007110808578463097621588022744826051145004666578077234156235426617805240679356193746126406311247530120779148
3

我希望这两个结果都是一样的。请解释发生了什么以及我应该如何获得所需的结果。

完整代码:

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.crypto.*;
import java.util.Scanner;
import java.security.spec.*;

public class Test {
    public static void main(String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        String[] ls= {"Abc","abc","a"};
        
        BigInteger N=new BigInteger("8876876877514683419701394775736891208848228258165890045890388820357265315302111630148566347729740003117740265768481144604003358339087090263495975493871517",10);
        
        RSAPublicKeySpec pub=new RSAPublicKeySpec(N, new BigInteger("65537"));
        
        Cipher cipher= Cipher.getInstance("RSA");
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKey publ=(RSAPublicKey) keyFactory.generatePublic(pub);
        System.out.println(publ.getPublicExponent());System.out.println(publ.getModulus());System.out.println(N);
        cipher.init(Cipher.ENCRYPT_MODE, publ);
        
        System.out.println(cipher.doFinal());
        int i;
        for(i=0;i<3;i++){
            byte[] mb=cipher.doFinal(ls[i].getBytes());
            BigInteger m=new BigInteger(mb);//Another error that was found later was that mb was being interpreted as a signed value.
            System.out.println(ls[i]);System.out.println(mb);System.out.println(m);}
        System.out.println(i);
    }

}
4

0 回答 0