0

im trying to encrypt a String with a given PublicKey (Modulus and Exponent) and each time I get a diffrent CipherText. How is this possible?

Below my Code. I couldnt find any help by googling the problem :(

I always get the same variables for byte[] SHA-256, String h, byte[] sbyte and Key pubKey. But the byte[] cipherByte and String cipherText are each time diffrent.

Atiq


class ModuleInfo{

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

        String s="Test";
        System.out.println("String s: " + s);

        byte[] hash = getSHA(s);
        System.out.println("SHA256: " + hash);

        String h = getBase(hash);
        System.out.println("base64: " + h);

        String s2 = s + ";" + h;
        byte [] sbyte = getByte(s2);
        System.out.println("sbyte: " + sbyte);


        String modulus= "00da8b39f428a4c2ebe5e0b75a0d1286a6f2e9e0ec56c4e3afea864aad646bd6893c24db10f212ed5a24ec2ac9e8cede164eecf1dd79dc945e93bc6f46c1f3cdfe51e79c68e2bd8030779d4071859f6b8e6feb1dcbef3c2047e934959f33f43553b20a97fcfdc326a41270b6f7d654aa0aa6e23a6c35c5f0c3f2a5d83189551b9288623d639097e712b04bf9679286e4e729ba544a0d94c71710aa0f8375d7dd336b3fc312acef1aae83eeea285b014541ae502acc659397a577d52fed8f8d9a889377bdd8a9110b4c099dc3e75ce344671cfa4cbda3c1aefed8d884882c26e03b4ecb39b4a9d9ed0f7fddb44f93d0e8bd848e1cfbdf58d492f35f5da73b500995a83f3b974b40dae442532bd9103e1c629af0c8e92f2bfec33f81425a4b4381c88d7e1a1adc252a4eeca245007bbb558802ebbf3a9f3870056dc28b648b2f695348990bfab8615aab3f14c6a6572f6a3d88f3c5b4deef5798d9c0006ddc3f6f8d5a35de26f028c97a55d92e2d9600c5c58934fd5f31f16f3e7367dd3c08ca3931a3d166724316fc532d616c12ec56eebd83761e4f463442c98b7088156f8e613279a73f9d57cd89fbe8373419159a910371f4955455ba1e119351d42579e2e0cc6fe9e8285ae0098ed3f8eaba1967de0d44a8f6876a3dbc8c586c2d00383cf22bdf9ff1badc5ef144371cccec503e95cac741fc1cc15c30ee15133db910a7b3e1";
        String exponent = "65537";
        Key pubKey = getKey(modulus, exponent);
        System.out.println(pubKey);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] cipherByte = cipher.doFinal(s2.getBytes("UTF-8"));
        System.out.println("encrypted: " + cipherByte); 
        String cipherText= getBase(cipherByte);
        System.out.println(cipherText);


    }

    public static String getBase(byte[] input) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {  
        Encoder base = Base64.getEncoder();

        return base.encodeToString(input);

    } 

    public static byte[] getSHA(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {  

        MessageDigest md = MessageDigest.getInstance("SHA-256");  

        return md.digest(getByte(input));  
    } 

    public static byte[] getByte(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {  

        return input.getBytes("UTF-8");  
    } 

    public static Key getKey(String modulus, String exponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
        BigInteger b1 = new BigInteger(modulus, 16);
        BigInteger b2 = new BigInteger(exponent);
        RSAPublicKeySpec spec = new RSAPublicKeySpec(b1, b2);
        KeyFactory kf = KeyFactory.getInstance("RSA");
         return kf.generatePublic(spec);
    }
}
Atiq
  • 1
  • 1
  • Found the answe https://stackoverflow.com/questions/16325057/why-does-rsa-encrypted-text-give-me-different-results-for-the-same-text – Atiq May 08 '20 at 17:23

1 Answers1

0

Why does RSA encrypted text give me different results for the same text

Random bits are generated and added to the text. Thats why the output is always diffrent.

Atiq
  • 1
  • 1