java basic encryption program - java

I'm trying to make a basic encryption program that used random numbers to encrypt the whole alphabet, encrypt a user submitted phrase and then decrypt it back to the original phrase but I am finding it very hard. Can anyone help point out my mistakes please! It shouldn't code two letters to the same letter, ie a and b shouldn't ever be both matched to c.
public class MainClass {
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public static final char[] Ualpha =
{'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
};
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];
Random rgenerator = new Random();
public MainClass(){
int randNum = rgenerator.nextInt(ALPHASIZE);
for(int i=0; i<ALPHASIZE ; i++)
{
//makes sure that it won't assign a letter to itself or to one that has already been assigned
do {
randNum = rgenerator.nextInt(26);
} while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));
encryptU[i] = Ualpha[randNum];
encryptL[i] = Lalpha[randNum];
decrypt[i] = randNum;
}
}
public String encrypt(String secret)
{
System.out.println(Arrays.toString(encryptU));
int position = 0;
char[] mess = secret.toCharArray();
for(int i = 0 ; i<mess.length;i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Ualpha[j]) {
position = j;
}
mess[i] = encryptU[position];
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Lalpha[j]) {
position = j;
}
mess[i] = encryptL[position];
}
}
}
return new String(mess);
}
public String decrypt(String secret)
{
char[] mess = secret.toCharArray();
for(int i = 0 ; i<mess.length;i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptU[j]){
mess[i] = Ualpha[j];
}
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptL[j]){
mess[i] = Lalpha[j];
}
}
}
}
return new String(mess);
}
}

You should really consider using a Map to store character/encoding pairs. Oh, and to create these random pairs you can add your characters to a List and make use of Collections.shuffle instead of reinventing the wheel yourself.
Let me demonstrate using only Lalpha (only lowercase letters). You want something along these lines:
List<Character> l = new ArrayList<Character>(Lalpha.length);
for (char c : Lalpha)
l.add(c);
Collections.shuffle(l);
Map<Character, Character> encoding = new HashMap<Character, Character>(Lalpha.length);
Map<Character, Character> decoding = new HashMap<Character, Character>(Lalpha.length);
for (int i = 0 ; i < Lalpha.length ; i++) {
encoding.put(Lalpha[i], l.get(i));
decoding.put(l.get(i), Lalpha[i]);
}
Now lets say we wanted to encode / decode the string helloworld, we would do this:
String s = "helloworld";
// Encode:
String enc = "";
for (char c : s.toCharArray())
enc += encoding.get(c);
System.out.println(enc);
// Decode:
String dec = "";
for (char c : enc.toCharArray())
dec += decoding.get(c);
System.out.println(dec);
Output (one of many possible):
vjwwmtmcwz
helloworld
Of course, you can incorporate uppercase letters and what-not using the same idea.

It sounds like you need to generate a permutation of the allowed letters. This is how I would do it for lower case letters:
public char[] permutation =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public generatePermutation()
{
Random r = new Random();
char tmp;
int rand;
for(int i = 0; i < permutation.length; ++i)
{
rand = r.nextInt(permutation.length - i);
tmp = permutation[i];
permutation[i] = permutation[rand];
permutation[rand] = tmp;
}
}
Finally, you can access this array for encryption by doing permutation[inputChar-'a'] (assuming you've already made sure inputChar is a lower case letter). And for decryption you find the letter that matches your input char and add 'a' to the index.

If you're having problems creating a random mapping between your set of letters and an "encrypted" set, you could start with this:
List<Character> alphabet = new LinkedList<Character>(
new String[] {'a', 'b', ..., 'Y', 'Z'});
List<Character> shuffledAlphabet = new LinkedList<Character>(alphabet);
Collections.shuffle(shuffledAlphabet);
Map<Character, Character> encryptionMap = new HashMap<Character, Character>();
Map<Character, Character> decryptionMap = new HashMap<Character, Character>();
for (int i=0; i < alphabet.size(); i++) {
encryptionMap.put(alphabet.get(i), shuffledAlphabet.get(i));
decryptionMap.put(shuffledAlphabet.get(i), alphabet.get(i));
}
Now you encrypt by taking each character for the typed String, doing a get on the encryptionMap, and replacing with the result of that get. To decrypt, do the same thing with the decryptionMap.

Related

How can I move multiple character arrays to the back of an array based upon a specific index?

I have a string "Hello, World!" that I have to convert into a char array. I then found the index of the char ',' - to which I want to create a new char array that contains " World!, Hello".
I've got the first index of the char array moved to the back - such that it prints out "ello, World!H".
How can I use my variable indexDelimiter to move the rest of the char arrays (as well as the ',') to the back? I've been looking at this problem forever, and I'm very confused as to how I could go about this.
I can't use ListArray. I has to be an Array.
public class ArrayTest {
public static void main(String[] args) {
String s = "Hello, World!";
char[] oldCharArray = s.toCharArray();
char[] newCharArray = new char[oldCharArray.length];
char delimiter = ',';
int indexDelimiter = new String(oldCharArray).indexOf(delimiter);
for (int i = 0; i < oldCharArray.length - 1; i++) {
newCharArray[i] = oldCharArray[i + 1];
}
newCharArray[oldCharArray.length - 1] = oldCharArray[0];
for (int i = 0; i < newCharArray.length; i++) {
System.out.print(newCharArray[i]);
}
// This prints out "ello, World!H" but I want " World!, Hello"
}
}
This code will produce "World!,Hello", take a look and see if it meets your needs.
public static void main(String args[]) {
String s = "Hello, World!";
char[] oldCharArray = s.toCharArray();
char[] newCharArray = new char[oldCharArray.length];
char delimiter = ',';
int indexDelimiter = new String(oldCharArray).indexOf(delimiter);
int i = 0;
for (i = 0; i < oldCharArray.length-indexDelimiter-1; i++) {
newCharArray[i] = oldCharArray[indexDelimiter + i + 1];
}
newCharArray[i] = delimiter;
i++;
int j = i;
while (i < oldCharArray.length) {
newCharArray[i] = oldCharArray[i - j];
i++;
}
System.out.println(newCharArray);
}
If you mean backward, you can reverse the by splitting it first
public char[] reverse_comma_split(String str) {
String[] spl = str.split(",");
String reversed = new String();
for (int i = spl.length - 1; i >= 0; i--) {
reversed += spl[i];
if (i != 0)
reversed += ", ";
}
return reversed.toCharArray();
}
Calling reverse_comma_split("Hello, world!") would return a char array of " world!, Hello"
However, if you insist to get uppercase char in every split, you can modify the loop in which spl[i] to spl[i].substring(0, 1).toUpperCase() + spl[i].substring(1)

How to refacor a code use only loops and simple arrays?

I wrote that code and it's working. But I need to refactor it. I can use only simple methods for solving the problem, for example: "for" loops and simple array.
public class Anagram {
public static void main(String[] args) throws IOException {
Anagram anagrama = new Anagram();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));) {
System.out.println("Enter word or phrase: ");
String userText = reader.readLine();
String resultAnagrama = anagrama.makeAnagram(userText);
System.out.println("Result of Anagrama : " + resultAnagrama);
}
}
This method take user's text and make anagram, but all non-letters should stay on the same places
/**
* #param text
* #return reversed text and all non-letter symbols stay on the same places
*/
public String makeAnagram(String text) {
HashMap<Integer, Character> mapNonLetters;
String[] textFragments = text.split(" ");
StringBuilder stringBuilder = new StringBuilder();
//Check each elements of array for availability symbols and make reverse of elements
for (int i = 0; i < textFragments.length; i++) {
char[] arrayCharacters = textFragments[i].toCharArray();
mapNonLetters = saerchNonLetters(arrayCharacters); // search symbols
StringBuilder builderAnagramString = new StringBuilder(textFragments[i]);
//Delete all non-letters from element of array
int reindexing = 0;
for (HashMap.Entry<Integer, Character> entry : mapNonLetters.entrySet()) {
int key = entry.getKey();
builderAnagramString.deleteCharAt(key - reindexing);
reindexing ++;
}
builderAnagramString.reverse();
//Insert all non-letters in the same places where ones stood
for (HashMap.Entry<Integer, Character> entry : mapNonLetters.entrySet()) {
int key = entry.getKey();
char value = entry.getValue();
builderAnagramString.insert(key, value);
}
textFragments[i] = builderAnagramString.toString();
stringBuilder.append(textFragments[i]);
if (i != (textFragments.length - 1)) {
stringBuilder.append(" ");
}
mapNonLetters.clear();
}
return stringBuilder.toString();
}
This method search all non-letters from each worв of user's text
/**
* Method search symbols
* #param arrayCharacters
* #return HashMap with symbols found from elements of array
*/
public HashMap<Integer, Character> saerchNonLetters(char[] arrayCharacters) {
HashMap<Integer, Character> mapFoundNonLetters = new HashMap<Integer, Character>();
for (int j = 0; j < arrayCharacters.length; j++) {
//Letters lay in scope 65-90 (A-Z) and 97-122 (a-z) therefore other value is non-letter
if (arrayCharacters[j] < 65 || (arrayCharacters[j] > 90 && arrayCharacters[j] < 97) ||
arrayCharacters[j] > 122) {
mapFoundNonLetters.put(j, arrayCharacters[j]);
}
}
return mapFoundNonLetters;
}
}
public class Anagram {
public static void main(String[] args) {
String text = "!Hello123 ";
char[] chars = text.toCharArray();
int left = 0;
int right = text.length() - 1;
while (left < right) {
boolean isLeftLetter = Character.isLetter(chars[left]);
boolean isRightLetter = Character.isLetter(chars[right]);
if (isLeftLetter && isRightLetter) {
swap(chars, left, right);
left++;
right--;
} else {
if (!isLeftLetter) {
left++;
}
if (!isRightLetter) {
right--;
}
}
}
String anagram = new String(chars);
System.out.println(anagram);
}
private static void swap(char[] chars, int index1, int index2) {
char c = chars[index1];
chars[index1] = chars[index2];
chars[index2] = c;
}
}
If I understand correctly and you need only 1 anagram, this should work:
String originalString = "This is 1 sentence with 2 numbers!";
System.out.println("original: "+originalString);
// make a mask to keep track of where the non letters are
char[] mask = originalString.toCharArray();
for(int i=0; i<mask.length; i++)
mask[i] = Character.isLetter(mask[i]) ? '.' : mask[i];
System.out.println("mask: "+ new String(mask));
// remove non letters from the string
StringBuilder sb = new StringBuilder();
for(int i=0; i< originalString.length(); i++) {
if(mask[i] == '.')
sb.append(originalString.charAt(i));
}
// find an anagram
String lettersOnlyAnagram = sb.reverse().toString();
// reinsert the non letters at their place
int letterIndex = 0;
for(int i=0; i<mask.length; i++) {
if(mask[i] == '.') {
mask[i] = lettersOnlyAnagram.charAt(letterIndex);
letterIndex++;
}
}
String anagram = new String(mask);
System.out.println("anagram: "+ anagram);
It prints out:
original: This is 1 sentence with 2 numbers!
mask: .... .. 1 ........ .... 2 .......!
anagram: sreb mu 1 nhtiwecn etne 2 ssisihT!

How can I change the chars of a String in a method? (same chars, randomly order)

//Please, help me to fix this codes. I wanna return a String that the have same chars with the sending String but is in different order.
public static String mix(String s){
int random;
int n= s.length();
int [] control = new int[n];
String miX="";
for(int i=0 ; i < n ; i++){
random = (int)(1+Math.random()*(n));
if( control[i] != random ){
control[i]= random;
miX += s.charAt(random);
}
}
return miX;
}
You can make use of Collections.shuffle.
String word= "word";
ArrayList<Character> chars =newArrayList<Character>(word.length());
for(char c : word.toCharArray()){
chars.add(c); }
Collections.shuffle(chars);
char[] shuffled =newchar[chars.size()];
for(int i =0; i < shuffled.length; i++){
shuffled[i]= chars.get(i);
}
String shuffledWord =newString(shuffled);
Another way similar to your code without using functions is:
public static void main(String[] args) {
// Create a random object
Random r = new Random();
String word = "Animals";
System.out.println("Before: " + word );
word = scramble( r, word );
System.out.println("After : " + word );
}
public static String scramble( Random random, String inputString )
{
// Convert your string into a simple char array:
char a[] = inputString.toCharArray();
// Scramble the letters
for( int i=0 ; i<a.length-1 ; i++ )
{
int j = random.nextInt(a.length-1);
// Swap letters
char temp = a[i]; a[i] = a[j]; a[j] = temp;
}
return new String( a );
}
You should select two positions within the length of the String RANDOMLY and then exchange them .
Run this within a loop for any number of times depending on the amount of randomness you want in your String.
Based on your code you might miss some of the characters in your new string if random selects the same index twice
Mix string chars with this code
import java.util.*;
class MixStringChars
{
public static String mix(String s)
{
StringBuilder sb = new StringBuilder(s);
Random r = new Random();
for (int i = 0; i < s.length(); i++)
{
char curr = sb.charAt(i); //current char
int rix = r.nextInt(s.length()); //random index
char temp = sb.charAt(rix); //memorize char at index rix
sb.setCharAt(rix, curr); //put current char to rix index
sb.setCharAt(i , temp); //put memorized char to i index
}
return sb.toString();
}
public static void main (String[] args)
{
System.out.println(mix("Hello"));
}
}

Splitting a string with CaesarCipherBreaker

How would I add code to this example for creating a CaesarCipherBreaker method that splits the encrypted message by two keys. So far I have this much written down:
import edu.duke.*;
public class TestCaesarCipherTwo {
public int[] countOccurrencesOfLetters(String message) {
//snippet from lecture
String alph = "abcdefghijklmnopqrstuvwxyz";
int[] counts = new int[26];
for (int k=0; k < message.length(); k++) {
char ch = Character.toLowerCase(message.charAt(k));
int dex = alph.indexOf(ch);
if (dex != -1) {
counts[dex] += 1;
}
}
return counts;
}
public int maxIndex(int[] values) {
int maxDex = 0;
for (int k=0; k < values.length; k++) {
if (values[k] > values[maxDex]) {
maxDex = k;
}
}
return maxDex;
}
public String halfOfString(String message, int start) {
StringBuilder halfString = new StringBuilder();
for (int index=start;index < message.length();index += 2) {
halfString.append(message.charAt(index));
}
return halfString.toString();
}
public void simpleTests() {
FileResource fileResource = new FileResource();
String fileAsString = fileResource.asString();
CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(17, 3);
String encrypted = cctk.encrypt(fileAsString);
System.out.println("Encrypted string:\n"+encrypted);
String decrypted = cctk.decrypt(encrypted);
System.out.println("Decrypted string:\n"+decrypted);
String blindDecrypted = breakCaesarCipher(encrypted);
System.out.println("Decrypted string using breakCaesarCipher():\n"+blindDecrypted);
}
public String breakCaesarCipher(String input) {
int[] freqs = countOccurrencesOfLetters(input);
int freqDex = maxIndex(freqs);
int dkey = freqDex - 4;
if (freqDex < 4) {
dkey = 26 - (4-freqDex);
}
CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(dkey);
return cctk.decrypt(input);
}
}
WARNING: I also have a constructor error on this line CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(dkey); stating CaesarCipherTwoKeys in class CaesarCipherTwoKeys cannot be applied to given types; required int,int; found int....
The breakCaesarCipher method I have now only figures out one key, not two. How should I go about writing a method that splits an encrypted string, figuring out two keys used for decryption.
If I understand your Code correctly, you could just call your halfOfString (two times) to get the two parts of the ciphertext and then use your usual approach to breaking a Ceaser-Cipher on both parts separately.
Your error seems to result from the fact that the two-key-encryption expects (unsurprisingly) two keys. You should give them both to the constructor.
public String breakCaesarCipher(String input) {
String in_0 = halfOfString(input, 0);
String in_1 = halfOfString(input, 1);
// Find first key
// Determine character frequencies in ciphertext
int[] freqs_0 = countOccurrencesOfLetters(in_0);
// Get the most common character
int freqDex_0 = maxIndex(freqs_0);
// Calculate key such that 'E' would be mapped to the most common ciphertext character
// since 'E' is expected to be the most common plaintext character
int dkey_0 = freqDex_0 - 4;
// Make sure our key is non-negative
if (dkey_0 < 0) {
dkey_0 = dkey_0+26;
}
// Find second key
int[] freqs_1 = countOccurrencesOfLetters(in_1);
int freqDex_1 = maxIndex(freqs_1);
int dkey_1 = freqDex_1 - 4;
if (freqDex_1 < 4) {
dkey_1 = dkey_1+26;
}
CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(dkey_0, dkey_1);
return cctk.decrypt(input);
}

How to use split a string into character array without special characters?

Scanner _in = new Scanner(System.in);
System.out.println("Enter an Equation of variables");
String _string = _in.nextLine();
char[] cArray = _string.toCharArray();
I want to remove the symbols "+,=" and I want to remove any repeating variables.
so far I have:
for(int i = 0; i < cArray.length; i++){
if(cArray[i].equals(+)|| cArray[i].equals(=)){
cArray[i] = null;
}
}
However, I dont know how to condence the array to remove any gaps and I don't know how to remove repeating characters, I think I am making this harder than it needs to be
You can use:
_string.replaceAll("[+,=]","");
This sounds like a good use for regular expressions:
String result = _string.replaceAll("[+=]", "");
Here, the [+=] is a character class that consists of + and =. You can add other characters as required.
Try the next:
public static void main(String[] args) {
String input = "a+a+b=c+d-a";
char[] cArray = input.replaceAll("[-+=]", "") // gaps
.replaceAll("(.)(?=.*\\1)", "") // repeating
.toCharArray();
System.out.println(Arrays.toString(cArray));
}
Output:
[b, c, d, a]
Or you can se another array, like this:
Scanner in = new Scanner(System.in);
String s = in.nextLine();
char [] cArray = s.toCharArray();
int count = 0;
char [] cArray2 = new char[cArray.length];
for (int i = 0; i < cArray.length; i++){
if (cArray[i] != '+' || cArray[i] != '='){
cArray2[count++] = cArray[i];
}
}
for (int i = 0; i < count; i++){
boolean repeated = false;
for (int j = i + 1; j < count; j++){
if (cArray2[i] == cArray2[j]){
repeated = true;
break;
}
}
if (!repeated){
//do what you want
}
}
You can extend LinkedHashSet (Which enforces uniqueness and retains order). Override the add() function to not accept any characters that you don't want to use. Then put the contents in a char array.
public static char[] toCharArray(String str) {
// Here I am anonymously extending LinkedHashSet
Set<Character> characters = new LinkedHashSet<Character>() {
// Overriding the add method
public boolean add(Character character) {
if (!character.toString().matches("[\\+=]")) {
// character is not '+' or '='
return super.add(character);
}
// character is '+' or '='
return false;
}
};
// Adding characters from str to the set.
// Duplicates, '+'s, and '='s will not be added.
for (int i = 0; i < str.length(); i++) {
characters.add(str.charAt(i));
}
// Put Characters from set into a char[] and return.
char[] arrayToReturn = new char[characters.size()];
int i = 0;
for (Character c : characters) {
arrayToReturn[i++] = c;
}
return arrayToReturn;
}

Categories