Java:加密程序

时间:2016-07-10 18:46:20

标签: java arrays string encryption char

我一直在尝试使用Cesar的Cipher创建一个加密邮件的程序,它基本上用字母替换每个字母后面的三个字母。

这是我的代码:

public static String Encript(String stringa, JTextArea encriptedTxt) {
    char[] stringaArray = stringa.toCharArray();

    for (int i = 0; i <= stringaArray.length; i++) {
        switch (stringaArray[i]) {
            case 'a':
                stringaArray[i] = ('D');
                break;
            case 'b':
                stringaArray[i] = 'E';
                break;
            case 'c':
                stringaArray[i] = 'F';
            case 'd':
                stringaArray[i] = 'G';
            case 'e':
                stringaArray[i] = 'H';
                break;
            case 'f':
                stringaArray[i] = 'I';
                break;
            case 'g':
                stringaArray[i] = 'J';
                break;
            case 'h':
                stringaArray[i] = 'K';
                break;
            case 'i':
                stringaArray[i] = 'L';
                break;
            case 'j':
                stringaArray[i] = 'M';
                break;
            case 'k':
                stringaArray[i] = 'N';
                break;
            case 'l':
                stringaArray[i] = 'O';
                break;
            case 'm':
                stringaArray[i] = 'P';
                break;
            case 'n':
                stringaArray[i] = 'Q';
                break;
            case 'o':
                stringaArray[i] = 'R';
                break;
            case 'p':
                stringaArray[i] = 'S';
                break;
            case 'q':
                stringaArray[i] = 'T';
                break;
            case 'r':
                stringaArray[i] = 'U';
                break;
            case 's':
                stringaArray[i] = 'V';
                break;
            case 't':
                stringaArray[i] = 'W';
                break;
            case 'u':
                stringaArray[i] = 'X';
                break;
            case 'v':
                stringaArray[i] = 'Y';
                break;
            case 'w':
                stringaArray[i] = 'Z';
                break;
            case 'x':
                stringaArray[i] = 'A';
                break;
            case 'y':
                stringaArray[i] = 'B';
                break;
            case 'z':
                stringaArray[i] = 'C';
                break;
        }
    }
    String encripted = new String(stringaArray);
    return encripted;
}

然后在使用netBeans默认可视化编辑器构建的图形界面中使用此方法,如:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    String stringa = messageTxt.getText();
    Util.Encript(stringa, encriptedTxt);
}

但是我收到了这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7
at Util.Util.Encript(Util.java:31)
at Main.Interface.jButton1MouseClicked(Interface.java:89)
at Main.Interface.access$000(Interface.java:14)
at Main.Interface$1.mouseClicked(Interface.java:53)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6538)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

有什么问题,我该如何解决?

0 个答案:

没有答案