AS3 密码解码器和编码器(更好的答案?)

AS3 Cipher Decoder & Encoder (better answer?)

昨天晚上,小弟问我有没有可能做一个程序,把字母表中的每个字母替换成另一个字母,把它变成一个代码,然后再把它变回原来的状态。

所以我做了这个:http://www.skyetheguy.com/novatranslationtool

从各方面来看,它都能完成工作,但我觉得,从代码的角度来看,它很笨拙,而且我真的很想提高我对循环等事物的了解...

编辑: 上面已经更新了 DodgerThud 的惊人答案并且像梦一样工作。 :) 梦想成真!

此外,这简直是天上掉馅饼,但如果能处理超过两个字符的内容,那就太棒了。我的代码绝对不会那样做。但是,你知道的,所有那些带有撇号的有趣代码......好东西!

stop();

// SET-UP STUFF
inputText.alpha = .5;
outputText.alpha = .5;
inputText.borderColor = 0xCCCCCC;
outputText.borderColor = 0xCCCCCC;
var textinProgress:String;
textinProgress = inputText.text;
if (textinProgress.charAt(0) == "\r") {
    inputText.text = "";
}
stage.addEventListener(Event.ENTER_FRAME, refresh_frame1);
function refresh_frame1(e:Event):void {
    inputScroll.update();
    outputScroll.update();
}
// ORIGINAL ALPHABET
var letters_aArray:Array = new Array();
letters_aArray[0] = "a";
letters_aArray[1] = "b";
letters_aArray[2] = "c";
letters_aArray[3] = "d";
letters_aArray[4] = "e";
letters_aArray[5] = "f";
letters_aArray[6] = "g";
letters_aArray[7] = "h";
letters_aArray[8] = "i";
letters_aArray[9] = "j";
letters_aArray[10] = "k";
letters_aArray[11] = "l";
letters_aArray[12] = "m";
letters_aArray[13] = "n";
letters_aArray[14] = "o";
letters_aArray[15] = "p";
letters_aArray[16] = "q";
letters_aArray[17] = "r";
letters_aArray[18] = "s";
letters_aArray[19] = "t";
letters_aArray[20] = "u";
letters_aArray[21] = "v";
letters_aArray[22] = "w";
letters_aArray[23] = "x";
letters_aArray[24] = "y";
letters_aArray[25] = "z";
// NEW ALPHABET
var letters_bArray:Array = new Array();
letters_bArray[0] = "m";
letters_bArray[1] = "n";
letters_bArray[2] = "b";
letters_bArray[3] = "v";
letters_bArray[4] = "c";
letters_bArray[5] = "x";
letters_bArray[6] = "z";
letters_bArray[7] = "l";
letters_bArray[8] = "k";
letters_bArray[9] = "j";
letters_bArray[10] = "h";
letters_bArray[11] = "g";
letters_bArray[12] = "f";
letters_bArray[13] = "d";
letters_bArray[14] = "s";
letters_bArray[15] = "a";
letters_bArray[16] = "p";
letters_bArray[17] = "o";
letters_bArray[18] = "i";
letters_bArray[19] = "u";
letters_bArray[20] = "y";
letters_bArray[21] = "t";
letters_bArray[22] = "r";
letters_bArray[23] = "e";
letters_bArray[24] = "w";
letters_bArray[25] = "q";
// ENCODE BUTTON
encode_btn.addEventListener(MouseEvent.CLICK, encode_btnclick);

function encode_btnclick(event:MouseEvent):void
{
    textinProgress = inputText.text;
    outputText.text = "";
    for (var b=0; b<textinProgress.length; b++) {
        if (textinProgress.charAt(b).search(letters_aArray[0]) == 0) {
            outputText.appendText(letters_bArray[0]);
        } else if (textinProgress.charAt(b).search(letters_aArray[0].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[0].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[1]) == 0) {
            outputText.appendText(letters_bArray[1]);
        } else if (textinProgress.charAt(b).search(letters_aArray[1].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[1].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[2]) == 0) {
            outputText.appendText(letters_bArray[2]);
        } else if (textinProgress.charAt(b).search(letters_aArray[2].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[2].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[3]) == 0) {
            outputText.appendText(letters_bArray[3]);
        } else if (textinProgress.charAt(b).search(letters_aArray[3].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[3].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[4]) == 0) {
            outputText.appendText(letters_bArray[4]);
        } else if (textinProgress.charAt(b).search(letters_aArray[4].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[4].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[5]) == 0) {
            outputText.appendText(letters_bArray[5]);
        } else if (textinProgress.charAt(b).search(letters_aArray[5].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[5].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[6]) == 0) {
            outputText.appendText(letters_bArray[6]);
        } else if (textinProgress.charAt(b).search(letters_aArray[6].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[6].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[7]) == 0) {
            outputText.appendText(letters_bArray[7]);
        } else if (textinProgress.charAt(b).search(letters_aArray[7].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[7].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[8]) == 0) {
            outputText.appendText(letters_bArray[8]);
        } else if (textinProgress.charAt(b).search(letters_aArray[8].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[8].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[9]) == 0) {
            outputText.appendText(letters_bArray[9]);
        } else if (textinProgress.charAt(b).search(letters_aArray[9].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[9].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[10]) == 0) {
            outputText.appendText(letters_bArray[10]);
        } else if (textinProgress.charAt(b).search(letters_aArray[10].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[10].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[11]) == 0) {
            outputText.appendText(letters_bArray[11]);
        } else if (textinProgress.charAt(b).search(letters_aArray[11].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[11].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[12]) == 0) {
            outputText.appendText(letters_bArray[12]);
        } else if (textinProgress.charAt(b).search(letters_aArray[12].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[12].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[13]) == 0) {
            outputText.appendText(letters_bArray[13]);
        } else if (textinProgress.charAt(b).search(letters_aArray[13].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[13].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[14]) == 0) {
            outputText.appendText(letters_bArray[14]);
        } else if (textinProgress.charAt(b).search(letters_aArray[14].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[14].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[15]) == 0) {
            outputText.appendText(letters_bArray[15]);
        } else if (textinProgress.charAt(b).search(letters_aArray[15].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[15].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[16]) == 0) {
            outputText.appendText(letters_bArray[16]);
        } else if (textinProgress.charAt(b).search(letters_aArray[16].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[16].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[17]) == 0) {
            outputText.appendText(letters_bArray[17]);
        } else if (textinProgress.charAt(b).search(letters_aArray[17].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[17].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[18]) == 0) {
            outputText.appendText(letters_bArray[18]);
        } else if (textinProgress.charAt(b).search(letters_aArray[18].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[18].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[19]) == 0) {
            outputText.appendText(letters_bArray[19]);
        } else if (textinProgress.charAt(b).search(letters_aArray[19].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[19].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[20]) == 0) {
            outputText.appendText(letters_bArray[20]);
        } else if (textinProgress.charAt(b).search(letters_aArray[20].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[20].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[21]) == 0) {
            outputText.appendText(letters_bArray[21]);
        } else if (textinProgress.charAt(b).search(letters_aArray[21].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[21].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[22]) == 0) {
            outputText.appendText(letters_bArray[22]);
        } else if (textinProgress.charAt(b).search(letters_aArray[22].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[22].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[23]) == 0) {
            outputText.appendText(letters_bArray[23]);
        } else if (textinProgress.charAt(b).search(letters_aArray[23].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[23].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[24]) == 0) {
            outputText.appendText(letters_bArray[24]);
        } else if (textinProgress.charAt(b).search(letters_aArray[24].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[24].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_aArray[25]) == 0) {
            outputText.appendText(letters_bArray[25]);
        } else if (textinProgress.charAt(b).search(letters_aArray[25].toUpperCase()) == 0) {
            outputText.appendText(letters_bArray[25].toUpperCase());
        } else if (textinProgress.charAt(b) == "\r") {
            outputText.appendText("\n");
        } else {
            outputText.appendText(textinProgress.charAt(b));
        }
    }
}
// DECODE BUTTON
decode_btn.addEventListener(MouseEvent.CLICK, decode_btnclick);

function decode_btnclick(event:MouseEvent):void
{
    textinProgress = inputText.text;
    outputText.text = "";
    for (var b=0; b<textinProgress.length; b++) {
        if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
            outputText.appendText(letters_aArray[0]);
        } else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[0].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
            outputText.appendText(letters_aArray[1]);
        } else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[1].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
            outputText.appendText(letters_aArray[2]);
        } else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[2].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
            outputText.appendText(letters_aArray[3]);
        } else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[3].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
            outputText.appendText(letters_aArray[4]);
        } else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[4].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
            outputText.appendText(letters_aArray[5]);
        } else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[5].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
            outputText.appendText(letters_aArray[6]);
        } else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[6].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
            outputText.appendText(letters_aArray[7]);
        } else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[7].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
            outputText.appendText(letters_aArray[8]);
        } else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[8].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
            outputText.appendText(letters_aArray[9]);
        } else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[9].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
            outputText.appendText(letters_aArray[10]);
        } else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[10].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
            outputText.appendText(letters_aArray[11]);
        } else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[11].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
            outputText.appendText(letters_aArray[12]);
        } else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[12].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
            outputText.appendText(letters_aArray[13]);
        } else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[13].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
            outputText.appendText(letters_aArray[14]);
        } else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[14].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
            outputText.appendText(letters_aArray[15]);
        } else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[15].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
            outputText.appendText(letters_aArray[16]);
        } else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[16].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
            outputText.appendText(letters_aArray[17]);
        } else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[17].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
            outputText.appendText(letters_aArray[18]);
        } else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[18].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
            outputText.appendText(letters_aArray[19]);
        } else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[19].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
            outputText.appendText(letters_aArray[20]);
        } else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[20].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
            outputText.appendText(letters_aArray[21]);
        } else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[21].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
            outputText.appendText(letters_aArray[22]);
        } else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[22].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
            outputText.appendText(letters_aArray[23]);
        } else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[23].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
            outputText.appendText(letters_aArray[24]);
        } else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[24].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
            outputText.appendText(letters_aArray[25]);
        } else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[25].toUpperCase());
        } else if (textinProgress.charAt(b) == "\r") {
            outputText.appendText("\n");
        } else {
            outputText.appendText(textinProgress.charAt(b));
        }
    }
}
// RECODE BUTTON (scrambles, takes what's in the output and applies the cipher again)
recode_btn.addEventListener(MouseEvent.CLICK, recode_btnclick);

function recode_btnclick(event:MouseEvent):void
{
    textinProgress = outputText.text;
    outputText.text = "";
    for (var b=0; b<textinProgress.length; b++) {
        if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
            outputText.appendText(letters_aArray[0]);
        } else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[0].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
            outputText.appendText(letters_aArray[1]);
        } else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[1].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
            outputText.appendText(letters_aArray[2]);
        } else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[2].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
            outputText.appendText(letters_aArray[3]);
        } else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[3].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
            outputText.appendText(letters_aArray[4]);
        } else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[4].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
            outputText.appendText(letters_aArray[5]);
        } else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[5].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
            outputText.appendText(letters_aArray[6]);
        } else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[6].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
            outputText.appendText(letters_aArray[7]);
        } else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[7].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
            outputText.appendText(letters_aArray[8]);
        } else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[8].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
            outputText.appendText(letters_aArray[9]);
        } else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[9].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
            outputText.appendText(letters_aArray[10]);
        } else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[10].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
            outputText.appendText(letters_aArray[11]);
        } else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[11].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
            outputText.appendText(letters_aArray[12]);
        } else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[12].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
            outputText.appendText(letters_aArray[13]);
        } else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[13].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
            outputText.appendText(letters_aArray[14]);
        } else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[14].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
            outputText.appendText(letters_aArray[15]);
        } else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[15].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
            outputText.appendText(letters_aArray[16]);
        } else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[16].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
            outputText.appendText(letters_aArray[17]);
        } else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[17].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
            outputText.appendText(letters_aArray[18]);
        } else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[18].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
            outputText.appendText(letters_aArray[19]);
        } else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[19].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
            outputText.appendText(letters_aArray[20]);
        } else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[20].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
            outputText.appendText(letters_aArray[21]);
        } else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[21].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
            outputText.appendText(letters_aArray[22]);
        } else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[22].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
            outputText.appendText(letters_aArray[23]);
        } else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[23].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
            outputText.appendText(letters_aArray[24]);
        } else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[24].toUpperCase());
        } else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
            outputText.appendText(letters_aArray[25]);
        } else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
            outputText.appendText(letters_aArray[25].toUpperCase());
        } else if (textinProgress.charAt(b) == "\r") {
            outputText.appendText("\n");
        } else {
            outputText.appendText(textinProgress.charAt(b));
        }
    }
}

只想告诉你为什么我做了我所做的所有令人难以置信的任意和疯狂的事情......

我确实知道有更简单、更节能的方法来对此进行编码,但我不知道该怎么做。

您应该搜索 Substitution ciphers, e.g., one of the simplest ones is the Caesar cipher

我将扩展我的评论并向您展示如何简化您的代码并削减大量代码。

首先,您不需要使用数组。 String 对象本质上已经是字节值数组。所以你可以这样做而不是创建大数组

var alphabet:String = "abcdefghijklmnopqrstuvwxyz";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewq";

为了访问特定的字符,您可以简单地调用字符串上的 indexOf 方法。

var indexOfJ = alphabet.indexOf("j");
trace(indexOfJ); //puts out 9

现在您已经了解了如何从特定索引获取字符串的字符。

var charAtIndex7:String = alphabet.charAt(7);
trace(charAtIndex7); //puts out "h"

在此处编辑:

看来我昨晚写那个答案的时候才半醒。双 for 循环的问题很简单,它总是 运行 整个过程,即使它找到了一个被接受的 if 子句。你已经想通了 你自己的最后一个 else 子句导致输入字符被打印 25 次。所以,让我们修复它。

function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
    textinProgress = input.text;
    var result:String = "";

    for(var b=0;b<textinProgress.length;b++)
    {
        var anyHit = false;
        for(var i=0;i<alphabet.length;i++){
            if(textinProgress.charAt(b).search(alphabet.charAt(i)) == 0){
                result += substituton.charAt(i);
                anyHit = true;
                break;
            }
            else if(textinProgress.charAt(b).search(alphabet.charAt(i).toUpperCase()) == 0){
                result += substituton.charAt(i).toUpperCase();
                anyHit = true;
                break;
            }
            else if(textinProgress.charAt(b) == "\r"){
                result += "\n";
                anyHit = true;
                break;
            }
        }
        if(anyHit == false){
            result += textinProgress.charAt(b);
        }
    }
    output.text = result;
}

今天早些时候,我让一位同事为我测试了这段代码,它应该可以正常工作。

但这并不是我们为简化您的代码所能做的全部。我们可以通过用小写和大写字母定义我们的字母表和替换变量来删除第二个 if 子句。

var alphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";

您还应该能够将特殊符号放入您的字母表和替换字符串中,例如 \r

var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";

我不确定这是否有效,但应该可以。

现在您已经大大缩短了代码,但我们甚至可以进一步缩短它。内部for循环真的有必要吗?因为我们的字母和我们的替换长度相同,也许我们可以进一步简化它。

function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
    textinProgress = input.text;
    var result:String = "";

    for(var b=0;b<textinProgress.length;b++)
    {
        var index:int = alphabet.indexOf(textinProgress.charAt(b));
        if(index >= 0){
            result += substitution.charAt(index);
        }else{
            result += textinProgress.charAt(b);
        }
    }
    output.text = result;
}

把它们放在一起。

var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";

function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
    textinProgress = input.text;
    var result:String = "";

    for(var b=0;b<textinProgress.length;b++)
    {
        var index:int = alphabet.indexOf(textinProgress.charAt(b));
        if(index >= 0){
            result += substitution.charAt(index);
        }else{
            result += textinProgress.charAt(b);
        }
    }
    output.text = result;
}

function encode_btnclick(event:MouseEvent):void{
   cipher(inputText, outputText, alphabet, substitution);
}

function decode_btnclick(event:MouseEvent):void{
   cipher(inputText, outputText, substitution, alphabet);
}

function recode_btnclick(event:MouseEvent):void{
   cipher(outputText, outputText, alphabet, substitution);
}

这个应该可以处理

  • 任意长度的文本
  • 换行符
  • 大小写字母
  • 未定义的字符