Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. //Shift numbers into new places based on the key
  2. public static int[] arrayShift(int shiftKey, int[]numSent)
  3. {
  4. int x = 0;
  5.  
  6. //Shift array
  7. for(int number1 : numSent) //shiftKey is the amount the number is shifted
  8. {
  9. number1 += shiftKey;
  10. }
  11.  
  12. //Loop array around ASCII alphabet
  13. for(int number2 : numSent)
  14. {
  15. //If
  16. if(number2 > 122) //x is the variable that tells how much it should be shifted after looping back around from z to a
  17. {
  18. x = (number2 - 122);
  19. number2 = (96 + x);
  20. }
  21. }
  22.  
  23. //array of numbers representing ASCII values in int form
  24. return numSent;
  25. }
  26.  
  27. //Turn number array (ASCII values in int form) into character array (ASCII values in char form)
  28. public static char[] charShift(int[]numSent, int sentLength)
  29. {
  30. char[]charSent = new char[sentLength]; //Makes an array preparing to contain the characters
  31. int b = 0;
  32.  
  33. for(int number3 : numSent)
  34. {
  35. charSent[b] = (char)number3; //Turns the int values into char values
  36. b++;
  37. }
  38.  
  39. //array of chars in ASCII value form
  40. return charSent;
  41. }
  42.  
  43. public static String shift(int shiftKey, String trueSentence)
  44. {
  45. String trueSent = trueSentence.trim();
  46. int trueSentLength = trueSent.length();
  47.  
  48. char[] trueSentSplit = trueSent.toCharArray();
  49. int[]shiftSent = new int[trueSentLength]; //Creates an array preparing to hold integer values representing ASCII
  50. int a = 0;
  51.  
  52. for(char sentPiece : trueSentSplit)
  53. {
  54. shiftSent[a] = sentPiece; //Turns each value in the character array into an integer array representing ASCII values
  55. a++;
  56. }
  57.  
  58. //Shift numbers
  59. int numList[] = arrayShift(shiftKey, shiftSent);
  60.  
  61. //Turn number array into character array
  62. char charList[] = charShift(numList, trueSentLength);
  63.  
  64. //Turn character array into new, translated String
  65. String falseSent = new String(charList);
  66.  
  67. //new, encrypted sentence
  68. return falseSent;
  69. }
  70.  
  71. //Runs shift()
  72. public static String getFalseSent(int shiftKey1, String trueSentence1)
  73. {
  74. String falseSent = shift(shiftKey1, trueSentence1);
  75. return falseSent;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement