-2
幫幫我我不能讓解密工作? 這個程序已經得到加密,但不解密如何解決這個問題?Java加密/解密
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();
}
}
}
它是什麼應該做,什麼不,它實際上做什麼? (瞭解這兩件事情是調試問題的普遍第一步) – immibis 2015-02-08 01:50:37
該程序加密一個空字符串,然後再解密。 JSON究竟與它有什麼關係? – EJP 2015-02-08 02:28:14