Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. A company would like to encrypt a token that it uses to identify data. The to

ID: 3587185 • Letter: 8

Question

8. A company would like to encrypt a token that it uses to identify data. The token is a four digit integer. The encryption method is as follows- replace each digit with the result of adding 7 to the digit and getting the remainder by dividing the resulting value by 10. Write a method that will return the answer as a new four digit integer. Now, write a method that will DECRYPT the integer token you just created back to your original four digit integer. Points will be awarded for the correct algorithm as well as the corret method. (20)

Explanation / Answer

Since the programming language is not given, giving it in Java. Hope it helps. If it did, please do rate the answer. Thank you.

public class EncryptDecrypt {

   private static String encrypt(String num)

   {

       String result = "";

       char ch;

       int digit;

       for(int i = 0; i < num.length(); i++)

       {

           ch = num.charAt(i);

           digit = ch - '0';// get the int value of the character by subtraction ascii value of '0'

           //to encrypt add 7 and take mod with 10, so that it is a number in range 0-9 again

           digit = (digit + 7) % 10;

           result += digit; //append to result;

       }

      

       return result;

   }

  

   private static String decrypt(String num)

   {

       String result = "";

       char ch;

       int digit;

       for(int i = 0; i < num.length(); i++)

       {

           ch = num.charAt(i);

           digit = ch - '0';// get the int value of the character by subtraction ascii value of '0'

           //to decrypt subtract 7 and if result goes into -ve number, add 10 to bring back the number

           //in range 0- 9

          

           digit = (digit - 7);

           if(digit < 0)

               digit = digit + 10;

           result += digit; //append to result;

       }

      

       return result;

   }

  

   public static void main(String[] args) {

       String token = "1234";

       String encrypted = encrypt(token);

       String decrypted = decrypt(encrypted);

      

       System.out.println(token + " is encrypted as " + encrypted);

       System.out.println(encrypted + " is decrypted as " + decrypted);

       if(decrypted.equals(token))

           System.out.println("Encryption and Decryption worked correctly");

       else

           System.out.println("Something went wrong in Encryption/decryption.");

      

   }

}

output

1234 is encrypted as 8901
8901 is decrypted as 1234
Encryption and Decryption worked correctly

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote