Javax crypto seems to generate a random RSA public key despite me providing a modular and exponent

183 Views Asked by At

I've made the following imports:

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.*;

I have declared a variable N as a large BigInteger and done RSAPublicKeySpec pub=new RSAPublicKeySpec(N, new BigInteger("65537"));. I also have a string array ls. To keep the question simple, ls is defined with String[] ls= {"Abc","abc","a"};.

I have used the following code to create a cipher

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);

I then ran this loop:

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);

When the full code is run, it outputs random results, despite me specifying a specific key and no extra randomness.

An example result I get:

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

Another example when running the same code:

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

I would expect both of these results to be the same. Please explain what is happening and how I should obtain the desired result.

Full code:

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);
    }

}
0

There are 0 best solutions below