Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package encrypt;
- import java.util.Scanner;
- import java.util.Random;
- import java.lang.*;
- public class Encrypt
- {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- String plainText, encryptedText, decryptedText;
- System.out.print("Enter message: ");
- plainText = sc.nextLine();
- System.out.println();
- System.out.println("Encrypted message");
- encryptedText = EncryptMessage(plainText);
- System.out.println(encryptedText);
- System.out.println();
- System.out.println("Decrypted message:");
- decryptedText = DecryptMessage(encryptedText);
- System.out.println(decryptedText);
- }
- // This function takes a character and returns it as an encrypted 6-character string
- // Use concatination (substrings, charat, etc....)
- public static String EncryptCharacter(char character)
- {
- Random generator = new Random();
- String encryptedCharacter;
- int ASCIIcode = (int)character;
- String hexCode = Integer.toHexString(ASCIIcode);
- char randChar1 = (char)(61 + generator.nextInt(25));
- char randChar2 = (char)(61 + generator.nextInt(25));
- int randNum1 = generator.nextInt(10);
- int randNum2 = generator.nextInt(10);
- encryptedCharacter = randChar1 + hexCode.substring(0,1) + randNum1 + randChar2 + hexCode.substring(1,2) + randNum2;
- return encryptedCharacter;
- }
- // This function takes an encrypted character string and returns a decrypted character
- public static char DecryptCharacter(String encryptedCharacter)
- {
- char decryptedCharacter;
- String hexRep = encryptedCharacter.substring(1,4);
- decryptedCharacter = (char)Integer.parseInt(hexRep,16);
- return decryptedCharacter;
- }
- // This function takes a plaintext message and returns the message as an encrypted string.
- public static String EncryptMessage(String plainText)
- {
- String encryptedString = "";
- for(int i=0; i<plainText.length(); i++)
- {
- char character = plainText.charAt(i);
- String encodedCharacter = EncryptCharacter(character);
- encryptedString = encryptedString + encodedCharacter;
- }
- return encryptedString;
- }
- // This function takes an encrypted message and returns it as a decrypted message.
- public static String DecryptMessage(String encryptedText)
- {
- String sixCharacterString;
- String decryptedString = "";
- for(int i=0; i<encryptedText.length(); i+=6)
- if(i>=encryptedText.length()-6)
- {
- sixCharacterString = encryptedText.substring(i,i+6);
- char decodedCharacter = DecryptCharacter(sixCharacterString);
- decryptedString = decryptedString + decodedCharacter;
- }
- return decryptedString;
- }
- }
Add Comment
Please, Sign In to add comment