Created
November 30, 2016 01:12
-
-
Save JashuaCovington/3b6b58419363bf4f130b8210748d872d to your computer and use it in GitHub Desktop.
Java Encryption program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* interface Encryptable. | |
* | |
* @author (Jashua Covington activity 26-29) | |
* @version (9/14/2015) | |
*/ | |
public interface Encryptable | |
{ | |
/** | |
* Represents the interface for an object that can be encrypted or decrypted. | |
* | |
* | |
* | |
*/ | |
public void encrypt(); | |
public String decrypt(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
/** | |
* public class Secret using an interface. | |
* | |
* Adopted / Utilized protected visibility inplace of private visibility. | |
* | |
* @author (Jashua Covington activity 26-29) | |
* @version (9/14/2015) | |
*/ | |
public class Secret implements Encryptable | |
{ | |
// instance variables | |
protected String message; | |
protected boolean encrypted; | |
protected int shift; | |
protected Random generator; | |
public Secret(String msg) | |
{ | |
//Constructor - stores the original message and establishes a value | |
//for the encryption shift. | |
message = msg; | |
encrypted = false; | |
generator = new Random(); | |
shift = generator.nextInt(10) + 5; | |
} | |
public void encrypt() | |
{ | |
//Encrypts this secret using a caesar cipher. | |
//Has no effect if the secret is already encrypted. | |
if(!encrypted) | |
{ | |
String masked = " "; | |
for(int index =0; index < message.length();index++) | |
masked = masked + (char) (message.charAt(index)+ shift); | |
message = masked; | |
encrypted = true; | |
} | |
} | |
public String decrypt() | |
{ | |
//Dcrypts and returns this secret. | |
//Has n effect if the secret is already decrypted. | |
if(encrypted) | |
{ | |
String unmasked = unmasked = " "; | |
for(int index = 0; index < message.length(); index++) | |
unmasked = unmasked + (char) (message.charAt(index)- shift); | |
message = unmasked; | |
encrypted = false; | |
} | |
return message; | |
} | |
public boolean isEncrypted() | |
//returns true if this secret is currently encrypted. | |
{ | |
return encrypted; | |
} | |
public String toString() | |
//returns this secret (may be encrypted). | |
{ | |
return message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*Tester class. | |
* | |
* @author (Jashua Covington activity 26-29) | |
* @version (9/14/2015) | |
*/ | |
public class Tester | |
{ | |
public static void main(String [] args) | |
{ | |
//demonstrates the use of a formal interface | |
//Creates a Secret object and exercises its encryption. | |
Secret code = new Secret("Trust in the Lord Jash, Believe in his Word to you."); | |
System.out.println(code); | |
code.encrypt(); | |
System.out.println(code); | |
code.decrypt(); | |
System.out.println(code); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment