2017-06-22 145 views
-3

我目前正在研究我的最終項目,到目前爲止我有一個註冊頁面,用於存儲您的用戶名,電子郵件和密碼。 對於密碼,它使用SHA算法進行加密,但我發現SHA消化了密碼,從而使其不可解密。 我需要你的幫助,以便我可以找到某種加密解密代碼。 這裏是我的代碼:需要解密的建議

try { 
     PrintWriter arq = new PrintWriter(jTextField1.getText()+".txt"); 
     arq.println("Username: " + jTextField1.getText()); 
     arq.println("Email: " + jTextField2.getText()); 




     String algorithm = "SHA"; 

     byte[] plainText = jPasswordField1.getText().getBytes(); 

    MessageDigest md = null; 

    try {  
     md = MessageDigest.getInstance(algorithm); 
    } catch (NoSuchAlgorithmException e) { 
    } 

    md.reset();  
    md.update(plainText); 
    byte[] encodedPassword = md.digest(); 

    StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < encodedPassword.length; i++) { 
     if ((encodedPassword[i] & 0xff) < 0x10) { 
      sb.append("0"); 
     } 

     sb.append(Long.toString(encodedPassword[i] & 0xff, 16)); 
    } 

     arq.println("Password: " + sb.toString()); 

     arq.close(); 
     if(!jTextField2.getText().equals(jTextField3.getText()) 
    ||!jPasswordField1.getText().equals(jPasswordField2.getText())){ 
      JOptionPane.showMessageDialog(null, "Either your email or 
    password are not corresponding. Please fix the issue."); 
     } 
     else{ 
      JOptionPane.showMessageDialog(null, "Account created!"); 
     } 

    } catch (HeadlessException | FileNotFoundException erro) { 
     JOptionPane.showMessageDialog(null, "Error creating Account. Please 
    try again."); 
    } 

據透露,此代碼灌輸到從JForm的按鈕。 非常感謝您提供任何幫助。

+0

如果添加了相關的編程語言標籤這將有助於(Java的?)。 – Filburt

+0

對不起,我忘記了,是使用的語言是Java。 – KennenBalls

+1

**不要加密密碼**,當攻擊者獲得管理員訪問權限時,他也將獲得加密密鑰。僅僅使用散列函數是不夠的,只是添加鹽對提高安全性沒有多大作用。使用隨機鹽在HMAC上迭代大約100毫秒的持續時間,並用散列表保存鹽。使用諸如'password_hash','PBKDF2','Bcrypt'或類似函數的函數。關鍵是要讓攻擊者花費大量時間通過強力查找密碼。 – zaph

回答

-2

如果您正在尋找Java中的加密和解密,jBcrypt是另一種選擇。它是基於Blowfish密碼的密碼哈希函數。以下是使用jBcrypt加密和解密密碼的示例代碼。 加密:

public String hashPassword(String plainTextPassword){ 
     return BCrypt.hashpw(plainTextPassword, BCrypt.gensalt()); 
} 

檢查密碼相匹配

public boolean checkPass(String plainPassword, String hashedPassword) { 
     if (BCrypt.checkpw(plainPassword, hashedPassword)) 
      return true; 
     else 
      return false; 
} 

參考: Password Encryption using jBcypt

+1

這裏沒有加密或解密。請了解加密和散列之間的區別並編輯您的答案。 –