Search code examples
javaencryptionaessha1prng

PRNG with seed in an interval


So my problem is the following : I'm trying to implement in Java a way to encrypt a message M (like from 1 character to let's say 1000) with a password P, chosen by the user (let's say "4z327yU10p"). I then want to hide the message in an image using a Pseudo Random Number Generator (PRNG) to choose the pixels. My seed is the password.

Here's my approach :

  1. sha3 on the password to get a 256b output to use as key
  2. use AES with the previously generated key to get an encrypted message
  3. use the output as a seed for my PRNG

For (1): is this possible for a short password?

For (2): can AES be used for small messages?

For (3): how can I have random number in the interval of my image ? (0,...,480000) because my algorithm gives me an int?

here's the code :

public void initSeed(String password){ //pour initier la seed avec le password
    Message message = new Message();
    message.initMessageASCII(password);
    List <Integer> temp = message.getMsg();
    byte[] vect = new byte[temp.size()];
    for (int i=0; i< temp.size(); i++){
        vect[i] = temp.get(i).byteValue();
    }
    this.seed = vect;
}

public void init() { //pour initier le remplissage du vecteur randList
    SecureRandom random;
    try {
        random = SecureRandom.getInstance("SHA1PRNG"); //SHA1PRNG est un algorithme très efficace
        random.setSeed(seed);
        random.nextBytes(randList); //fonction pour créer les bytes aléatoires et les écrire dans "bytes"
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

Solution

  • I'll answer the separate questions (ask separate questions):

    1. Yes, SHA-3 can be used for arbitrarily sized messages.
    2. Yes, AES can be used for arbitrarily sized messages.
    3. Random.nextInt(int).

    You can see this as a consultants curse though, I gave you what you asked for instead of what you need. For instance, you don't need the (not yet standardized) SHA-3 but you should use a PBKDF instead.