-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.java
More file actions
37 lines (36 loc) · 1.11 KB
/
Encryption.java
File metadata and controls
37 lines (36 loc) · 1.11 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
import java.util.*;
class Encryption{
static int[] asciiValues;
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Message to encrypt: ");
String str = reader.nextLine();
System.out.print("Key to shift by (1-95): ");
int shift = reader.nextInt();
char[] letters = str.toCharArray();
asciiValues = new int[letters.length];
double[] frequencies = new double[96];
for (int i = 0; i < letters.length; i ++) {//Converts to ASCII
asciiValues[i] = (byte) letters[i];
}
shiftLetters(shift);
for(int i = 0; i < asciiValues.length; i ++){//Converts to letters
letters[i] = (char) asciiValues[i];
}
for(int i = 0; i < letters.length; i ++){
System.out.print(letters[i]);
}
System.out.println("");
}
static void shiftLetters(int num){
for(int i = 0; i < asciiValues.length; i ++){
asciiValues[i] += num;
if(asciiValues[i] < 32){
asciiValues[i] = 128 - (32 - asciiValues[i]);
}
if(asciiValues[i] > 127){
asciiValues[i] = 31 + (asciiValues[i] - 127);
}
}
}
}