Advertisement
nick43ui

Untitled

Mar 20th, 2021
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. class Eyler{
  6. int EylerValue(int value_p, int value_q, int res, int value_d){
  7. int E_fn = (value_p-1)*(value_q-1);
  8. int value_e = (1+(1*E_fn))/(value_d);
  9. System.out.println("Public key(" + value_e + "," + res + ")");
  10. System.out.println("Private key(" + value_d + "," + res + ")");
  11. // int k = (value_e * value_d) % E_fn;
  12. // System.out.println(k); where result == 1
  13. return value_e;
  14. }
  15. }
  16.  
  17. class EncryptText{
  18. String Encrypt(String value_alphabet,int value_e,int result_n) {
  19. // int value_x = 69; /*to check if the algorithm works*/
  20. // int result_x = 493;
  21. String toStringBigDec="";
  22. int[] numArr = Arrays.stream(value_alphabet.split(" ")).mapToInt(Integer::parseInt).toArray();
  23. for (int i = 0; i < numArr.length; i++) {
  24. BigDecimal bigDecimal = new BigDecimal(numArr[i]).pow(value_e);
  25. BigDecimal[] bigDecimal1 = bigDecimal.divideAndRemainder(new BigDecimal(result_n));
  26. toStringBigDec += bigDecimal1[1].toString() + " ";
  27. }
  28. return toStringBigDec;
  29. }
  30. }
  31.  
  32. class DecryptText{
  33. String Dencrypt(String value_encrypt,int value_d,int result_n) {
  34. // int value_x = 13; /*to check if the algorithm works*/
  35. // int result_x = 493;
  36. String toStringBigDec="";
  37. int[] numArr = Arrays.stream(value_encrypt.split(" ")).mapToInt(Integer::parseInt).toArray();
  38. for (int i = 0; i < numArr.length; i++) {
  39. BigDecimal bigDecimal = new BigDecimal(numArr[i]).pow(value_d);
  40. BigDecimal[] bigDecimal1 = bigDecimal.divideAndRemainder(new BigDecimal(result_n));
  41. toStringBigDec += bigDecimal1[1].toString() + " ";
  42. }
  43. return toStringBigDec;
  44. }
  45. }
  46.  
  47. public class CryptoLastName {
  48. public static void main(String[] args) {
  49.  
  50. int value_p = 23;
  51. int value_q = 43;
  52. int value_d = 37;
  53. int result_n = value_p * value_q;
  54.  
  55. Scanner in = new Scanner(System.in);
  56. System.out.print("Enter Test: ");
  57. String s = in.nextLine(); //"VASYLKIVSKIY OLEG " or your name, lastname
  58. String t = "";
  59. for (int i = 0; i < s.length(); ++i) {
  60. char ch = s.charAt(i);
  61. if (Character.isLetter(ch)) { // check if the character is a letter
  62. if (!t.isEmpty()) {
  63. t += " ";
  64. }
  65. int n = (int) ch - (int) 'A'; // if you want to check, then add 'A'+ 1. Where the beginning of the alphabet begins with 1
  66. t += String.valueOf(n);
  67. }
  68. }
  69. System.out.println("*******************************************");
  70. System.out.println(s); //Output of the text that was entered
  71. System.out.println(t);
  72. System.out.println("*******************************************");
  73. Eyler metod = new Eyler();
  74. int value_e = metod.EylerValue(value_p,value_q,result_n,value_d);
  75. System.out.println("*******************************************");
  76.  
  77. EncryptText enc = new EncryptText();
  78. String Encrypt_value = enc.Encrypt(t,value_e,result_n);
  79. System.out.println("Encrypted text: " + Encrypt_value);
  80.  
  81. DecryptText dec = new DecryptText();
  82. String Decrypt_value = enc.Encrypt(Encrypt_value,value_d,result_n);
  83.  
  84. int[] numArr = Arrays.stream(Decrypt_value.split(" ")).mapToInt(Integer::parseInt).toArray();
  85. System.out.print("Decrypted text: ");
  86. for (int i = 0; i < numArr.length; ++i) {
  87. char c = (char)((int)'A' + numArr[i]);
  88. System.out.print(c + " ");
  89. }
  90. System.out.println();
  91. System.out.println("*******************************************");
  92. }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement