-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.java
More file actions
68 lines (57 loc) · 2.24 KB
/
Cipher.java
File metadata and controls
68 lines (57 loc) · 2.24 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
import java.util.Scanner;
public class Cipher {
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz ";
public static void main(String[] args) {
// TODO Auto-generated method stub
int shift = (int) (Math.random() *25)+1;
Scanner input = new Scanner(System.in);
System.out.println("Please enter your word or sentence to be encrypted: ");
String userSentence = new String();
userSentence = input.nextLine();
String userSentenceLowerCase = userSentence.toLowerCase();
String encryptedText =createCipher(userSentenceLowerCase, shift);
System.out.println("Your encrypted sentence is now: "+ encryptedText);
System.out.println("After decrypting your text is now: " + decrypt(encryptedText,shift));
System.out.println("Press 0 to discontinue");
}
public static String createCipher(String userSentence,int shift){
userSentence = userSentence.toLowerCase();
String encryptedMessage = "";
for (int index = 0; index < userSentence.length(); index++)
{
if(userSentence.charAt(index) != ' '){
int characterIndex = ALPHABET.indexOf(userSentence.charAt(index));
int keyOfCharacter = (shift + characterIndex)%26;
char newCharacter = ALPHABET.charAt(keyOfCharacter);
encryptedMessage += newCharacter;
}
else{
encryptedMessage += ' ';
}
}
return encryptedMessage;
}
public static String encrypt(String userSentence, int shift){
return createCipher(userSentence, shift);
}
public static String decrypt(String encryptedText, int shift){
String descryptedText = "";
for (int index = 0; index < encryptedText.length(); index++)
{
if(encryptedText.charAt(index) != ' '){
int charPosition = ALPHABET.indexOf(encryptedText.charAt(index));
int positionKey = (charPosition - shift)%26;
if (positionKey < 0)
{
positionKey = ALPHABET.length() + positionKey;
}
char newCharacter = ALPHABET.charAt(positionKey);
descryptedText += newCharacter;
}
else{
descryptedText += ' ';
}
}
return descryptedText;
}
}