以下是我目前使用的代码:
public static String encrypt(String password) {
String encrypted = null;
char passChars[] = password.toCharArray();
int ascii[] = null;
for(int i=0;i<passChars.length;i++) {
ascii[i] = Integer.parseInt(String.valueOf(passChars[i]));
ascii[i] = ascii[i] + 17;
passChars[i] = (char)ascii[i];
encrypted = encrypted + String.valueOf(passChars[i]);
}
return encrypted;
}
当我尝试运行它时,它正在运行但是当我使用加密方法时,我收到以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "M"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.package.program.Encryptor.encrypt(Encrypter.java:46)
at com.package.program.Main$3.actionPerformed(Main.java:136)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我尝试过更改一些内容,例如更改char转换为int的方式但似乎没有任何效果......我做错了什么?
答案 0 :(得分:1)
如果你想通过将每个字符偏移一定量来“加密”字符串,那么就直接这样做:
String encrypted = null;
char passChars[] = password.toCharArray();
for (int i=0; i < passChars.length; i++) {
passChars[i] += 17;
}
encrypted = new String(passChars);
请注意,在某些ASCII字符上添加17可能会使您无法打印。目前尚不清楚你在这里想要达到的目标;如果你想系统地将密码加密成其他字符,那么我们可能必须使用模数进行包装。
答案 1 :(得分:0)
这一行错了:
ascii[i] = Integer.parseInt(String.valueOf(passChars[i]));
您正在尝试将密码的字母转换为整数。无法保证密码仅由数字组成。
相反,您可以这样做:
ascii[i] = passChars[i] + 17;
直接
答案 2 :(得分:0)
使用有效大小初始化ascii [],您可能会获得索引超出绑定的异常,并且此程序仅适用于仅作为数字字符串的密码。