2013-11-24 230 views
2

嘿傢伙在這裏我有一個問題..在我的Java應用程序中有一個登錄表單。在那裏有一個選項「忘記密碼..?」我想從那發送用戶的密碼..在這種情況下,用戶的電子郵件地址標識用戶名give..no that.But問題是密碼加密..我怎麼能得到那些密碼以默認格式發送。我的登錄名TABEL是登錄和3場未私服,類型..我的郵件發送代碼如下(admin或限制)..通過電子郵件發送密碼

try{ 

    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 

    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { 

     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("[email protected]", "password"); 
     } 
    }); 

    mailSession.setDebug(true); // Enable the debug mode 

    Message msg = new MimeMessage(mailSession); 

    //--[ Set the FROM, TO, DATE and SUBJECT fields 
    msg.setFrom(new InternetAddress("[email protected]")); 
    msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("[email protected]")); 
    msg.setSentDate(new Date(232323)); 
    msg.setSubject("Hello World!"); 

    //--[ Create the body of the mail 
    msg.setText("Hello from my first e-mail sent with JavaMail"); 

    //--[ Ask the Transport class to send our mail message 
    Transport.send(msg); 

}catch(Exception E){ 
    System.out.println("Oops something has gone pearshaped!"); 
    System.out.println(E); 
} 
+7

**不要以純文本**存儲密碼。可逆加密(幾乎)不會更好。你需要理解哈希的含義。 – SLaks

+0

密碼是加密密碼還是哈希密碼?加密密碼需要使用正確的密鑰進行解密。如果密碼被散列,那麼您將無法向用戶發送他們的密碼。相反,您必須要求用戶創建一個新密碼。 – Max

+6

安全性**很難**。不要重新發明輪子。使用現有的,經過驗證的認證軟件包。 – SLaks

回答

3

密碼通常不存儲在加密的形式,而是在申請secure one way hash function的結果形式;最佳做法還包括使用用戶(或至少系統)特定salt以防止使用rainbow tables進行攻擊。所有這些說,你應該爲用戶設置一個新的(可計算的)密碼,然後告訴他們現在已經設置了這個密碼(例如,閱讀員工表,使用他們的ssn的最後六位數字和他們的街道地址數)。