-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
95 lines (84 loc) · 2.71 KB
/
Code
File metadata and controls
95 lines (84 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.Scanner;
import java.util.ArrayList;
public class VCipher
{
public static Scanner sc = new Scanner(System.in);
public static String key = "", keyLarge= "", plainText = "", encryptedText = "", decryptedText = "";
public static ArrayList<Integer> spacesAt = new ArrayList<Integer>();
public static int ArrayCounter =0, textCode = 0, keyCode = 0;
// key
// the quick brown fox jumped over the lazy dog
public static void MakeKeyLarge(String str)
{
for(int a =0; a < plainText.length(); a++)
{
keyLarge = keyLarge.concat(str);
}
}
public static void IndexSpaces(String str)
{
for(int b = 0; b < str.length(); b++)
{
if(str.charAt(b) == ' ')
{
int b1 = b;
spacesAt.add(b1);
ArrayCounter++;
}
}
}
public static void RemoveSpaces(String str)
{
for(int c = 0; c < str.length(); c++)
{
plainText = plainText.replace(" ", "");
}
}
public static void MakeKeyEqualLength()
{
keyLarge = keyLarge.substring(0, plainText.length());
}
public static void Encrypt(String key, String text)
{
for(int d =0; d < text.length(); d++)
{
textCode = text.charAt(d);
keyCode = key.charAt(d);
int Evalue = 0;
Evalue = ( ((int)(textCode) % 97) + ((int)(keyCode) % 97));
if(Evalue >= 26)
Evalue %= 26;
//System.out.print((char)(Evalue + 97));
encryptedText = encryptedText.concat( "" + (char)(Evalue + 97));
}
}
public static String SpacesHere()
{
String result = "";
for(int e =0; e < spacesAt.size(); e++)
{
result += spacesAt.get(e) + " ";
}
return result;
}
public static void main(String[] args)
{
System.out.println("Please Enter an Encryption Key");
key = sc.nextLine().trim().toLowerCase();
System.out.println("Please Enter the Sentence you would like to encrypt");
plainText = sc.nextLine().toLowerCase();
IndexSpaces(plainText);
RemoveSpaces(plainText);
MakeKeyLarge(key);
MakeKeyEqualLength();
RemoveSpaces(plainText);
Encrypt(keyLarge, plainText);
System.out.println("KEY-repeated:\n" + keyLarge + "\n"); //System.out.print("\t Number of characters:" + keyLarge.length() + "\n");
System.out.println("ORIGINAL TEXT: \n" + plainText + "\n"); //System.out.print("\t Number of characters:" + plainText.length() + "\n");
System.out.println("ENCRYPTED TEXT: \n" + encryptedText + "\n");
System.out.println("SPACES AT INDEXES: " + SpacesHere() + "\n");
/* Remembe that once a space is added to the beginning of a string, it incremets the indices
* of all the following elements, and by exxtention, the following spaces.
*/
}
}