lundi 29 mars 2021

How to convert key of type string to type long?

I am doing a stream cipher program in Java that should take three arguments: a key from a file, the name of an input file and the name of an output file. It should further take the key as a seed to generate a stream of random numbers. I have tried to achieve this first step in the code below, but since the seed needs to be of type long and my array list is of type String it does not work. I am also aware of that storing the key in an array list might be unnecessary. Could someone help me figure out what the best solution is?

public class Streamcipher {

    public static void main(String[] args) throws IOException {

        List<String> inputKey = new ArrayList<String>();


        File key = null;
        if(0 < args.length) {
             key = new File(args[0]);

        }

        BufferedReader br = null;

    try {
        String s="";

     br = new BufferedReader(new FileReader(key));
      while((s=br.readLine())!=null){
        inputKey.add(s);
        
      }

      System.out.println(inputKey);

      randomGenerator(inputKey);
    
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(br != null){
            br.close();
        }
    }

    

}

public static int randomGenerator(long seed) {
    
        Random myRandom = new Random(seed);
        int keyStream = myRandom.nextInt(256);
        System.out.println(keyStream);

    return keyStream;

   

}

}



Aucun commentaire:

Enregistrer un commentaire