Ansichten: QuotePaste - CodePaste - NoPaste
Codesnippet eingetragen am 15.4.2013 um 15:54
Von: Michael
Sprache: Java
Beschreibung:
CodeSnippet:
  1.  
  2. import inout.Console;
  3.  
  4. /**
  5.  *
  6.  * @author Михайл Скиба <michael.skiba@rub.de>
  7.  */
  8. public class Crypto {
  9.  
  10. public static void main(String args[]) {
  11. while (true) {
  12. int a, p, remainder=0, counter = 0;
  13. System.out.println("Please enter a");
  14. a = Console.readInt();
  15. System.out.println("Please enter p (must be prime)");
  16. p = Console.readInt();
  17.  
  18. if (!isPrime(p)) {
  19. System.out.println(p + " is not a prime number!");
  20. System.exit(0);
  21. }
  22.  
  23. do {
  24. counter++;
  25. // remainder = (int) (Math.pow(a, counter) %p);
  26. if(counter == 1){
  27. remainder = a;
  28. }else{
  29. remainder = (remainder * a) %p;
  30. }
  31. /* Debug-Line: */System.out.printf("%d^%d = %d mod %d\n", a, counter, remainder, p);
  32. } while (remainder != 1);
  33. System.out.printf("ord(%d)= %d", a, counter);
  34. if (counter == (p - 1)) {
  35. System.out.printf(" (%d is a primitiv element)\n", a);
  36. }
  37.  
  38. System.out.println("Continue y?");
  39. if(!Console.readString().equalsIgnoreCase("y")){
  40. System.exit(0);
  41. }
  42. }
  43. }
  44.  
  45. // Shamelessly stolen from the interwebs ...
  46. // http://stackoverflow.com/questions/2385909/what-would-be-the-fastest-method-to-test-for-primality-in-java
  47. public static boolean isPrime(int n) {
  48. return !new String(new char[n]).matches(".?|(..+?)\\1+");
  49. }
  50. }
  51.