-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDencryptViginere.java
More file actions
53 lines (48 loc) · 1.77 KB
/
DencryptViginere.java
File metadata and controls
53 lines (48 loc) · 1.77 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
/*
* 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.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author rafae
*/
public class DencryptViginere {
public static void main(String args[]) throws FileNotFoundException, IOException{
System.out.println("Decrypt message:");
File f= new File("cyphered_message");
FileInputStream fin=new FileInputStream(f);
DataInputStream din= new DataInputStream(fin);
byte[] menssage=new byte[fin.available()];
din.readFully(menssage);
System.out.println("Key:");
String key= (new Scanner (System.in)).nextLine();
int keyIndex=0;
byte[] decryptedMessage= new byte[menssage.length];
for(int i=0;i< menssage.length;i++){
char charMenssage= (char)menssage[i];
char charKey=key.charAt(keyIndex++);
byte decryptedChar=(byte)(charMenssage-charKey);
decryptedMessage[i]=decryptedChar;
if(keyIndex>=key.length()){
keyIndex=0;
}
}
System.out.println("Decrypted message: "+new String(decryptedMessage));
f=new File("decrypt_message");
FileOutputStream fout= new FileOutputStream(f);
DataOutputStream dout= new DataOutputStream(fout);
dout.write(decryptedMessage);
dout.close();
fout.close();
}
}