-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEncryptViginere.java
More file actions
52 lines (43 loc) · 1.66 KB
/
EncryptViginere.java
File metadata and controls
52 lines (43 loc) · 1.66 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package viginere;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author rafae
*/
public class EncryptViginere {
public static void main(String args[]) throws FileNotFoundException, IOException{
System.out.println("Input content:");
String mensagem= (new Scanner (System.in)).nextLine();
System.out.println("Cypher key:");
String key= (new Scanner (System.in)).nextLine();
int indexKeyChar=0;
byte[] encryptedMessage= new byte[mensagem.length()];
for(int i=0;i< mensagem.length();i++){
char charOnMensagem=mensagem.charAt(i);
char charOnKey=key.charAt(indexKeyChar++);
char cypheredChar=(char)(charOnMensagem+charOnKey);
encryptedMessage[i]=(byte)cypheredChar;
//loopback to the first char of the key
if(indexKeyChar>=key.length()){
indexKeyChar=0;
}
}
System.out.println("Cyphered message: "+new String(encryptedMessage));
File f=new File("cyphered_message");
FileOutputStream fout= new FileOutputStream(f);
DataOutputStream dout= new DataOutputStream(fout);
dout.write(encryptedMessage);
dout.close();
fout.close();
}
}