Guest User

Returning the correct variables

a guest
Mar 28th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package encrypt;
  7.  
  8. import java.util.Scanner;
  9. import java.util.Random;
  10. import java.lang.*;
  11.  
  12.  
  13.  
  14. public class Encrypt
  15. {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args)
  21.     {
  22.         Scanner sc = new Scanner(System.in);
  23.         String plainText, encryptedText, decryptedText;
  24.              
  25.         System.out.print("Enter message: ");
  26.         plainText = sc.nextLine();
  27.        
  28.         System.out.println();
  29.         System.out.println("Encrypted message");
  30.         encryptedText = EncryptMessage(plainText);
  31.         System.out.println(encryptedText);
  32.         System.out.println();
  33.        
  34.         System.out.println("Decrypted message:");
  35.         decryptedText = DecryptMessage(encryptedText);
  36.         System.out.println(decryptedText);
  37.     }
  38.     // This function takes a character and returns it as an encrypted 6-character string
  39.     // Use concatination (substrings, charat, etc....)
  40.     public static String EncryptCharacter(char character)
  41.     {
  42.         Random generator = new Random();
  43.     String encryptedCharacter;
  44.         int ASCIIcode = (int)character;
  45.         String hexCode = Integer.toHexString(ASCIIcode);
  46.         char randChar1 = (char)(61 + generator.nextInt(25));
  47.         char randChar2 = (char)(61 + generator.nextInt(25));
  48.         int randNum1 = generator.nextInt(10);
  49.         int randNum2 = generator.nextInt(10);
  50.        
  51.         encryptedCharacter = randChar1 + hexCode.substring(0,1) + randNum1 + randChar2 + hexCode.substring(1,2) + randNum2;
  52.        
  53.         return encryptedCharacter;
  54.    
  55.     }
  56.    
  57.     // This function takes an encrypted character string and returns a decrypted character
  58.     public static char DecryptCharacter(String encryptedCharacter)
  59.     {
  60.     char decryptedCharacter;
  61.        
  62.         String hexRep = encryptedCharacter.substring(1,4);
  63.         decryptedCharacter = (char)Integer.parseInt(hexRep,16);
  64.                
  65.         return decryptedCharacter;
  66.    
  67.     }
  68.  
  69.   // This function takes a plaintext message and returns the message as an encrypted string.
  70.     public static String EncryptMessage(String plainText)
  71.     {
  72.        
  73.         String encryptedString = "";
  74.        
  75.         for(int i=0; i<plainText.length(); i++)
  76.         {
  77.             char character = plainText.charAt(i);
  78.             String encodedCharacter = EncryptCharacter(character);
  79.             encryptedString = encryptedString + encodedCharacter;
  80.         }
  81.        
  82.         return encryptedString;
  83.     }  
  84.  
  85.     // This function takes an encrypted message and returns it as a decrypted message.
  86.     public static String DecryptMessage(String encryptedText)
  87.     {
  88.        
  89.         String sixCharacterString;
  90.         String decryptedString = "";
  91.        
  92.         for(int i=0; i<encryptedText.length(); i+=6)
  93.            
  94.             if(i>=encryptedText.length()-6)
  95.             {
  96.                 sixCharacterString = encryptedText.substring(i,i+6);
  97.                 char decodedCharacter = DecryptCharacter(sixCharacterString);
  98.                 decryptedString = decryptedString + decodedCharacter;
  99.             }
  100.        
  101.         return decryptedString;
  102.     }
  103.  
  104.  
  105.    
  106. }
Add Comment
Please, Sign In to add comment