2015-02-08 140 views
-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(); 
     } 

    } 
} 
+0

它是什麼應該做,什麼不,它實際上做什麼? (瞭解這兩件事情是調試問題的普遍第一步) – immibis 2015-02-08 01:50:37

+0

該程序加密一個空字符串,然後再解密。 JSON究竟與它有什麼關係? – EJP 2015-02-08 02:28:14

回答

-1

好看着這之後,我改變了代碼周圍,使其更具可讀性,並給你一個更好的技術到如何處理這個問題:

  1. 讀輸入。
  2. 創建密鑰(公共/私人)的實例。
  3. 將輸入轉換爲字節。
  4. 加密輸入並將其打印出來。
  5. 解密輸入並打印出來。

    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(); 
        } 
    
    } 
    
+0

它只能用於單個單詞嗎?好的,我會 – David 2015-02-08 01:57:38