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

时间:2016-08-30 21:59:59

标签: actionscript-3 flash encryption encoder decoder

昨晚,我的小弟弟问我是否有可能制作一个程序,将每个字母替换为字母表中的另一个字母,将其转换为代码,并将其恢复到正常状态。

所以我做了这个: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));
        }
    }
}

要告诉你为什么我做了我所做的所有令人难以置信的任意和循环的事情......

  • 如果你做了其他人在这里谈到的其他正常替换事物(至少从我发现的事情),你最终会在某一点上替换你自己的工作。例如,如果你想让B成为A,但是你希望A成为C,那么如果你输入&#34; BA&#34;,你就会回来&#34; CC&#34;而不是&#34; AC&#34;。因此,我的代码实际上会将每个角色一次一个地吐出来。
  • 我做了那些乏味的事情&#34; .toUpperCase()&#34;确保此工具区分大小写的东西 - 因此,如果我输入小写或大写的键,它将始终生成适当的位置。这就是RegExp为我工作的原因。
  • 它还考虑了换行符和字母表中未包含的随机符号等内容。

我知道确实有更简单,更节能的方法来编码,但我根本不知道如何。

2 个答案:

答案 0 :(得分:2)

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

首先,您不需要使用数组。 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"

在此编辑fom:

当我昨晚写下答案时,看起来我只是半醒。 double 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);
}

这应该能够处理

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

答案 1 :(得分:1)

您应该搜索Substitution ciphers,例如,最简单的一个是Caesar cipher