0

The way i understand it, a key encoding should return a sequence of bytes in some specific encoding such as UTF-8 for example.

However, logging the following:

 KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
 kpg.initialize(256);
 KeyPair kp = kpg.generateKeyPair();

 Log("TEST : ${kp.public.encoded}")
 Log("Test : ${kp.public.encoded}")

Equivalently, in Java:

kp.getPublic().getEncoded();

Is giving me 2 different byte arrays! What am i missing here?

It seems that every time i call the encoded method, different pairs are generated.

idk
  • 3
  • 5
  • 3
    **You're not looking at the contents.** Each call to `getEncoded` on a given key object returns a _newly copied_ (and mutable) `byte[]` with the _same_ contents, but you are using `Object.toString()` which displays the hashcode (i.e. identity) of the array not its contents. See [one of the oldest and most duplicated Qs on Stack](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – dave_thompson_085 Apr 17 '21 at 20:52

1 Answers1

0

Try in this way

import android.util.Base64;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncyption {

    private static final int pswdIterations = 10;
    private static final int keySize = 128;
    private static final String cypherInstance = "AES/CBC/PKCS5Padding";
    private static final String secretKeyInstance = "PBKDF2WithHmacSHA1";
    private static final String plainText = "sampleText";
    private static final String AESSalt = "exampleSalt";
    private static final String initializationVector = "8119745113154120";

    public static String encrypt(String textToEncrypt) throws Exception {

        SecretKeySpec skeySpec = new SecretKeySpec(getRaw(plainText, AESSalt), "AES");
        Cipher cipher = Cipher.getInstance(cypherInstance);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(initializationVector.getBytes()));
        byte[] encrypted = cipher.doFinal(textToEncrypt.getBytes());
        return Base64.encodeToString(encrypted, Base64.DEFAULT);
    }

    public static String decrypt(String textToDecrypt) throws Exception {

        byte[] encryted_bytes = Base64.decode(textToDecrypt, Base64.DEFAULT);
        SecretKeySpec skeySpec = new SecretKeySpec(getRaw(plainText, AESSalt), "AES");
        Cipher cipher = Cipher.getInstance(cypherInstance);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(initializationVector.getBytes()));
        byte[] decrypted = cipher.doFinal(encryted_bytes);
        return new String(decrypted, "UTF-8");
    }

    private static byte[] getRaw(String plainText, String salt) {
        try {
            SecretKeyFactory factory = SecretKeyFactory.getInstance(secretKeyInstance);
            KeySpec spec = new PBEKeySpec(plainText.toCharArray(), salt.getBytes(), pswdIterations, keySize);
            return factory.generateSecret(spec).getEncoded();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

}