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
| import javax.crypto.*;
import java.io.*;
import java.security.*;
public class main {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
String texte = "Hello world";
File text = new File("test.txt");
FileWriter writer = new FileWriter(text);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(text)));
text.createNewFile();
Key key = keygen();
byte[] texte_crypte = Encrypt(texte, key);
writer.write(texte_crypte.toString());
writer.flush();
System.out.println(key.toString());
System.out.println(Decrypt(reader.readLine().getBytes(), key));
}
public static byte[] Encrypt(String text, Key key) {
try {
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, key);
System.out.println("Text encrypted");
return cipher.doFinal(text.getBytes());
}catch(Exception e) { e.printStackTrace(); }
return null;
}
public static String Decrypt(byte[] text, Key key) {
try {
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(cipher.DECRYPT_MODE, key);
System.out.println("Text decrypted");
return new String(cipher.doFinal(text), "UTF8");
}catch(Exception e) { e.printStackTrace(); }
return null;
}
public static Key keygen() {
try {
KeyGenerator keygen = KeyGenerator.getInstance("Blowfish");
keygen.init(56);
System.out.println("key generates");
return keygen.generateKey();
}catch(Exception e) { e.printStackTrace(); }
return null;
}
} |
Partager