2010-10-24 76 views
0

法語:SLT,評論JE peut放任樂dechiffrement連接utilisant AES花蓮週一代碼
英語: 嗨,我該怎麼辦使用AES解密?這是我的代碼:解密在Java中

public class NewClass1{ 

    private Key key; 

    private void generateKey() throws NoSuchAlgorithmException{ 
     KeyGenerator generator; 
     generator = KeyGenerator.getInstance("AES"); 
     generator.init(new SecureRandom()); 
     key = generator.generateKey(); 
    } 

    private String decrypt(String encrypted) throws InvalidKeyException, 
     NoSuchAlgorithmException, 
     NoSuchPaddingException, 
     IllegalBlockSizeException, 
     BadPaddingException, 
     IOException{ 

     Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.DECRYPT_MODE, key); 
     BASE64Decoder decoder = new BASE64Decoder(); 
     byte[] raw = decoder.decodeBuffer(encrypted); 
     byte[] stringBytes = cipher.doFinal(raw); 
     // converts the decoded message to a String 
     String clear = new String(stringBytes); 
     return clear; 
    } 

    public NewClass1(String encrypted){ 
     try{ 
      System.out.println("encrypted message: " + encrypted); 
      generateKey(); 
      String decrypted = decrypt(encrypted); 
      System.out.println("decrypted message: " + decrypted); 
     } catch(NoSuchAlgorithmException e){ 
      e.printStackTrace(); 
     } catch(NoSuchPaddingException e){ 
      e.printStackTrace(); 
     } catch(InvalidKeyException e){ 
      e.printStackTrace(); 
     } catch(UnsupportedEncodingException e){ 
      e.printStackTrace(); 
     } catch(IllegalBlockSizeException e){ 
      e.printStackTrace(); 
     } catch(BadPaddingException e){ 
      e.printStackTrace(); 
     } catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args){ 
     new NewClass1("vbfhdhhhjhtrrrrrrrrrrrrrrjrdfes"); 
    } 
} 
+1

此網站爲英文,請用英文提問。 – theomega 2010-10-24 11:47:51

+1

'嗨,我如何使用AES解密是我的代碼:'是她的意思 – 2010-10-24 11:48:22

+0

對於任何人想知道:http://blog.stackoverflow.com/2009/07/non-english-question-policy/ – 2010-10-24 11:49:39

回答

3

一些基本的錯誤在這裏是錯誤的。您的代碼意味着「vbfhdhhhjhtrrrrrrrrrrrrrrjrdfes」是AES加密結果的base64編碼。不是。你的代碼也會生成一個隨機的密鑰。這是行不通的。我建議從Wikipedia articles on encryption開始。