S
S
SerS2015-11-27 16:46:15
Java
SerS, 2015-11-27 16:46:15

Who can check the literacy of java code?

I just recently started learning the language. Tried to write RSA. The class generates keys for encryption. The code works. Can you evaluate the literacy of writing code? What can be improved?
Thank you.

package std.sys;

import java.util.ArrayList;
import java.util.Random;

public class Keys {	
  private int limit;
  private int[] openKey = new int[2];
  private int[] closeKey = new int[2];
  
  public Keys(int limit){	
    this.limit = limit;	
  }
  
  public int[] getOpenKey() {
    return openKey;
  }
  
  public int[] getCloseKey(){
    return closeKey;
  }
  
  public void genKeys()
  {
    ArrayList<Integer> num, num2;
    Random rand = new Random();
    int p, q, func;
    
    num = new ArrayList<Integer>(); // Множество простых чисел до указаного предела
    int quantityNumber = 0; // Колличество найденых простых чисел
    
    for(int i = 10; i <= limit; i++)
    {
      if((i%2!=0) && (i%3!=0) && (i%5!=0) && (i%7!=0) && ((i*i-1)%24==0))
        {
          num.add(i);
          quantityNumber++;
        }							
    }
    
    p = num.get(rand.nextInt(quantityNumber));
    do{
    q = num.get(rand.nextInt(quantityNumber));
      }while (p == q);
    
    openKey[1] = p * q;	
    func = (p-1) * (q-1);
    
    num2 = new ArrayList<Integer>();
    int a, b, quantityNum=0; 
    for(int i = 0; i < quantityNumber; i++){
      if(num.get(i)<func)
      {
        a = num.get(i);
        b = func;
        while(b!=0)
          {
            int tmp = a%b;
            a = b;
            b = tmp;
          }
        if (a == 1)
          {
            num2.add(num.get(i));
            quantityNum++; // Кол-во подходящих взаимно простых чисел
          }
      }
      else{break;}	
  }
    openKey[0] = num2.get(rand.nextInt(quantityNum));
     
     num.clear();
     num2.clear();
     quantityNum = 0;
     
     for(int i = 1; i<1000; i++)
     {
       if((i*openKey[0])%func == 1){
         num2.add(i);
         quantityNum++; // Кол-во подходящих чисел i*openKey[0])%func = 1
       }
     }
     
     closeKey[0] = num2.get(rand.nextInt(quantityNum));
     closeKey[1] = openKey[1];
     
     num2.clear();	 
     
  } // End getKeys

} // End Keys

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Kolesnichenko, 2015-11-27
@KolesnichenkoDS

int p, q, func
int a, b
What are the names?
// End getKeys
// End Keys
Such comments are not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question