我想为要求用户输入用户名,地址和密码的项目编写代码,然后加密密码。 90%的独立类只是加密,所以请随意轻松通过它。
以下是独立类的代码:
import javax.swing.JOptionPane; //Import JOptionPane
public class UserInfo
{
private String firsthalf, secondhalf, firsttwo, lasttwo, firstfourth, secondfourth, thirdfourth, finalfourth; //Declaring private variables
private String username, useraddress, userpassword;
private String encryptedpassword;
public String encrypt, userinfo;
public UserInfo()
{
String encryptedpassword = "";
String username = "";
String useraddress = "";
}
public void setUserPassword(String userpassword) //Get and Set methods
{
encryptedpassword = userpassword;
}
public String getUserPassword()
{
return encryptedpassword;
}
public void setUserName(String username)
{
username = username;
}
public String getUserName()
{
return username;
}
public void setUserAddress(String useraddress)
{
useraddress = useraddress;
}
public String getUserAddress()
{
return useraddress;
}
public String encrypt (String encryptedpassword) //Encrypt method
{
String removeWhitespaceAndConvertToUpper = "";
String substitute = "";
String swapHalfsForEncrypt = "";
String swapFirst2WithLast2 = "";
String swapMiddleChars = "";
String toString = "";
removeWhitespaceAndConvertToUpper (removeWhitespaceAndConvertToUpper);
substitute (substitute);
swapHalfsForEncrypt (swapHalfsForEncrypt);
swapFirst2WithLast2 (swapFirst2WithLast2);
swapMiddleChars (swapMiddleChars);
toString (toString);
return encryptedpassword;
}
public String removeWhitespaceAndConvertToUpper(String encryptedpassword)
{
encryptedpassword = encryptedpassword.trim(); //From here down is just encryption proccesses
encryptedpassword = encryptedpassword.toUpperCase();
return encryptedpassword;
}
public String substitute (String encryptedpassword)
{
encryptedpassword.replaceAll ("A" , "@"); characeters with required characters
encryptedpassword.replaceAll ("E" , "=");
encryptedpassword.replaceAll ("I" , "!");
encryptedpassword.replaceAll ("J" , "?");
encryptedpassword.replaceAll ("O" , "*");
encryptedpassword.replaceAll ("P" , "#");
encryptedpassword.replaceAll ("R" , "&");
encryptedpassword.replaceAll ("S" , "$");
encryptedpassword.replaceAll ("T" , "+");
encryptedpassword.replaceAll ("V" , "^");
encryptedpassword.replaceAll ("X" , "%");
encryptedpassword.replaceAll (" ", "_");
return encryptedpassword;
}
public String swapHalfsForEncrypt (String encryptedpassword) //Swapping halfs for encryption
{
String firsthalf = encryptedpassword.substring(0, encryptedpassword.length()/2); //2 substrings
String secondhalf = encryptedpassword.substring(encryptedpassword.length()/2, encryptedpassword.length());
encryptedpassword = (secondhalf + firsthalf); //Reversed substrings
return encryptedpassword;
}
public String swapFirst2WithLast2 (String encryptedpassword) //Replaces last 2 digits with first 2 and vise versa
{
lasttwo = encryptedpassword.substring(encryptedpassword.length()-1, encryptedpassword.length()); //Last 2 variables
firsttwo = encryptedpassword.substring (0, 1); //First 2
encryptedpassword = lasttwo + encryptedpassword.substring(2, encryptedpassword.length() - 2) + firsttwo;
return encryptedpassword;
}
public String swapMiddleChars (String encryptedpassword)
{
int pwhalflength = encryptedpassword.length()/2;
int lengthminus2 = pwhalflength - 2;
int lengthplus2 = pwhalflength/2 + 2;
int halfpassword = pwhalflength;
int pwlength = encryptedpassword.length();
firstfourth = encryptedpassword.substring (0, lengthminus2); //4 substrings for 4 parts of the password
secondfourth = encryptedpassword.substring (lengthminus2, lengthplus2);
thirdfourth = encryptedpassword.substring (halfpassword, lengthplus2);
finalfourth = encryptedpassword.substring(lengthplus2, encryptedpassword.length());
encryptedpassword = firstfourth + thirdfourth + secondfourth + finalfourth; //rearranging password
return encryptedpassword;
}
public String toString (String encryptedpassword)
{
username += "Username: " + username + "\n";
useraddress += "Address: " + useraddress + "\n";
userpassword += "Password: " + encryptedpassword + "\n";
return userpassword;
}
}
我的驱动程序类的代码:
import javax.swing.JOptionPane; //Importing JOptionPane
public class UserInfoDriver
{
String encryptedpassword;
public static void main (String[] args)
{
int again; //Variable for user controlled exit
do
{
String userpassword = "";
String encryptedpassword = "";
String userinfo = "";
String username, useraddress, password; //Declaring variables
String firsthalf = "";
String secondhalf = "";
String firsttwo = "";
String lasttwo = "";
String firstfourth = "";
String secondfourth = "";
String thirdfourth = "";
String finalfourth = "";
int pwhalflength = userpassword.length()/2;
int lengthminus2 = pwhalflength - 2;
int lengthplus2 = pwhalflength/2 + 2;
int halfpassword = pwhalflength;
int pwlength = userpassword.length();
username = JOptionPane.showInputDialog ("Enter your username: ");
useraddress = JOptionPane.showInputDialog ("Enter your address: ");
userpassword = JOptionPane.showInputDialog ("Enter your password: ");
UserInfo encrypt = new UserInfo();
UserInfo name = new UserInfo();
UserInfo address = new UserInfo();
encrypt.setUserPassword(userpassword); //Setting name, address, password
name.setUserName(username);
address.setUserAddress(useraddress);
JOptionPane.showMessageDialog (null, "Your username is: " + username);
JOptionPane.showMessageDialog (null, "Your address is: " + useraddress);
JOptionPane.showMessageDialog (null, "Your password is: " + encryptedpassword);
again = JOptionPane.showConfirmDialog (null, "Register again?");
}
while (again == JOptionPane.YES_OPTION); //User Controlled exit
}
}
代码编译并运行正常,但是当我到达密码应该显示的JOptionPane时,什么都没有显示出来。其他一切都很好,它只是密码。我想知道问题是什么,以及如何解决它。我知道这不是你见过的最好的帖子,因为大多数是代码,但任何帮助都会受到赞赏。
答案 0 :(得分:1)
这一行
JOptionPane.showMessageDialog (null, "Your password is: " + encryptedpassword);
引用您初始化为空字符串的encryptedpassword
,但它在代码中永远不会再次更改。所以自然它什么都不会显示。查看您的UserInfo
类,看起来您有一个返回加密密码的实际encrypt
方法。您将要使用此返回值。
此外,在一个不相关的说明中,我注意到您正在为每条信息创建一个新的UserInfo
引用。我怀疑这将是你想要的,因为你通过这些引用设置的每条信息都将包含在单独的对象中。拥有UserInfo
课程的重点应该是允许您将一个参考添加到包含代表所有用户信息的成员的单个对象。