所以我正在尝试加密在用户键入内容时创建的文件,并且在加密之后我想解密文件。用户创建的文件和加密文件的创建没有问题,但是当我打开加密文件时,我认为它的空白是错误的,因为它上面应该写有一些乱码。
package br.com.rsa;
import java.security.*;
import java.io.*;
import java.util.*;
import javax.crypto.*;
public class Geracao {
public static void main(String[] args) throws IOException {
//Gerando um arquivo que será encriptado e descriptografado.
Scanner entrada1 = new Scanner(System.in);
System.out.println("Digite qualquer coisa: ");
String entrada = entrada1.nextLine();
System.out.println("Arquivo criado.");
FileOutputStream saida = new FileOutputStream("arquivo.txt");
PrintStream imprimir = new PrintStream(saida);
System.setOut(imprimir);
System.out.println(entrada);
saida.close();
//Gerando as chaves publica e privada.
try {
KeyPairGenerator chave = KeyPairGenerator.getInstance("RSA");
chave.initialize(1024);
KeyPair chaves = chave.generateKeyPair();
PrivateKey privada = chaves.getPrivate();
PublicKey publica = chaves.getPublic();
Base64.Encoder cripto = Base64.getEncoder();
System.out.println("Chave privada: " + cripto.encodeToString(privada.getEncoded()));
System.out.println("");
System.out.println("Chave publica: " + cripto.encodeToString(publica.getEncoded()));
System.out.println("");
//Salvando as chaves publica e privada.
try (FileOutputStream s_prv = new FileOutputStream("privada" + ".key")){
s_prv.write(chaves.getPrivate().getEncoded());
}
try (FileOutputStream s_pub = new FileOutputStream("publica" + ".key")){
s_pub.write(chaves.getPublic().getEncoded());
}
Criptografar(chaves, null);
//Descriptografar(chaves, null);
}
//Qualquer erro dentro da geração das chaves
catch (Exception e){
System.out.println(e);
}
}
慕田峪4524236
相关分类