· 9 years ago · Oct 09, 2016, 03:40 PM
1public class DecipherDES_ex3 {
2 public static void main(String[] args) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException {
3 // ir buscar a chave e verificar se bate certo com o ficheiro
4 String everything = "";
5 try(BufferedReader br = new BufferedReader(new FileReader("chave.txt"))) {
6 StringBuilder sb = new StringBuilder();
7 String line = br.readLine();
8 while (line != null) {
9 sb.append(line);
10 sb.append(System.lineSeparator());
11 line = br.readLine();
12 }
13 everything = sb.toString();
14 }
15
16 // decode the base64 encoded string
17 BASE64Decoder decoder = new BASE64Decoder();
18 byte[] decodedKey = decoder.decodeBuffer(everything);
19 // rebuild key using SecretKeySpec
20 SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
21 System.out.println("CHAVE");
22 System.out.println(Base64.getEncoder().encodeToString(originalKey.getEncoded()));
23
24
25
26 }
27}