帮帮我,我无法解密工作? 这个程序已经获得加密而不是解密如何解决这个问题?
import javax.crypto.*;
import java.util.Scanner;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class CIBAw
{
public static void main(String[] argv)
{
Scanner input = new Scanner(System.in);
Scanner File = new Scanner(System.in);
try
{
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
System.out.println("Encrypt a File");
System.out.print("Enter a sentence:");
//String file = File.nextLine();
byte[] text = "".getBytes();
System.out.println("" + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("File Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("File Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}catch(NoSuchPaddingException e)
{
e.printStackTrace();
}catch(InvalidKeyException e)
{
e.printStackTrace();
}catch(IllegalBlockSizeException e)
{
e.printStackTrace();
}catch(BadPaddingException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:-1)
好看了之后,我改变了代码,使其更具可读性,并为您提供更好的技巧来解决这个问题:
解密输入并将其打印出来。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try
{
//Prompt for String
System.out.print("Enter a sentence:");
String in = input.next();
//Generate Key for encryption/decryption
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
//Cast the input into bytes
byte[] text = in.getBytes();
System.out.println("" + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("File Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("File Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}catch(NoSuchPaddingException e)
{
e.printStackTrace();
}catch(InvalidKeyException e)
{
e.printStackTrace();
}catch(IllegalBlockSizeException e)
{
e.printStackTrace();
}catch(BadPaddingException e)
{
e.printStackTrace();
}
}