简体   繁体   中英

Scrambling ASCII characters and writing them to a file (Java)

My assignment is to get a message, do a substitution cipher on it using keys (created from all ASCII characters and also written to a file), and write the encrypted message to a file.

My question has to do with scrambling the ASCII characters to create an encryption key. I write all ASCII characters in order to a file to create a decryption key, scramble those characters, and then write to another file for an encryption key. The decryption key (all ASCII characters in order) works completely fine and prints the way I want it to but the encryption key (scrambled ASCII characters) prints unreadable symbols. Any ideas what I am doing wrong here?

public void substitutionKeys(String encryptKeyFileName, String decryptKeyFileName) throws IOException
{
    //Create array of 256 characters
    char [] asciiTable = new char[256]; 

    //Fill array with all 256 ASCII characters
    for (int i = 0; i < asciiTable.length; i++){
        asciiTable[i] = (char) i;
    }

    BufferedWriter decryptWriter = new BufferedWriter(new FileWriter(decryptKeyFileName));

    //Write ordered ASCII characters to decryptKeyFile
    decryptWriter.write(asciiTable);
    decryptWriter.close();

    BufferedWriter encryptWriter = new BufferedWriter(new FileWriter(encryptKeyFileName));

    //Write scrambled ASCII characters to encryptKeyFile
    encryptWriter.write(shuffle(asciiTable));
    encryptWriter.close();
}

public char[] shuffle(char[] array)
{
    int index;
    char temp;

    Random random = new Random();
    for (int i = 0; i < array.length; i++){
        index = random.nextInt(i + 1);
        temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }

    return array;
}

So the program works but the key contains "unreadable" characters? Sounds like you're creating special/unsupported characters. When converting integers to characters anything out of the range of 33-160 isn't a normal character (Alphabetic/Numeric).

To demonstrate this I wrote a small loop converting ints to characters.

System.out.println("START");
for (int i = 0; i < 200; i++) {
        System.out.println("\tChar[" + i + "] = " + (char) i);
}
System.out.println("END");

This was the output:

START
// Nothing at/below 32 prints successfully and can be copied. Output in console is a square
    Char[33] = !
    Char[34] = "
    Char[35] = #
    Char[36] = $
    Char[37] = %
    Char[38] = &
    Char[39] = '
    Char[40] = (
    Char[41] = )
    Char[42] = *
    Char[43] = +
    Char[44] = ,
    Char[45] = -
    Char[46] = .
    Char[47] = /
    Char[48] = 0
    Char[49] = 1
    Char[50] = 2
    Char[51] = 3
    Char[52] = 4
    Char[53] = 5
    Char[54] = 6
    Char[55] = 7
    Char[56] = 8
    Char[57] = 9
    Char[58] = :
    Char[59] = ;
    Char[60] = <
    Char[61] = =
    Char[62] = >
    Char[63] = ?
    Char[64] = @
    Char[65] = A
    Char[66] = B
    Char[67] = C
    Char[68] = D
    Char[69] = E
    Char[70] = F
    Char[71] = G
    Char[72] = H
    Char[73] = I
    Char[74] = J
    Char[75] = K
    Char[76] = L
    Char[77] = M
    Char[78] = N
    Char[79] = O
    Char[80] = P
    Char[81] = Q
    Char[82] = R
    Char[83] = S
    Char[84] = T
    Char[85] = U
    Char[86] = V
    Char[87] = W
    Char[88] = X
    Char[89] = Y
    Char[90] = Z
    Char[91] = [
    Char[92] = \
    Char[93] = ]
    Char[94] = ^
    Char[95] = _
    Char[96] = `
    Char[97] = a
    Char[98] = b
    Char[99] = c
    Char[100] = d
    Char[101] = e
    Char[102] = f
    Char[103] = g
    Char[104] = h
    Char[105] = i
    Char[106] = j
    Char[107] = k
    Char[108] = l
    Char[109] = m
    Char[110] = n
    Char[111] = o
    Char[112] = p
    Char[113] = q
    Char[114] = r
    Char[115] = s
    Char[116] = t
    Char[117] = u
    Char[118] = v
    Char[119] = w
    Char[120] = x
    Char[121] = y
    Char[122] = z
    Char[123] = {
    Char[124] = |
    Char[125] = }
    Char[126] = ~
    Char[127] = 
    Char[128] = ?
    Char[129] = ?
    Char[130] = ?
    Char[131] = ?
    Char[132] = ?
    Char[133] = ?
    Char[134] = ?
    Char[135] = ?
    Char[136] = ?
    Char[137] = ?
    Char[138] = ?
    Char[139] = ?
    Char[140] = ?
    Char[141] = ?
    Char[142] = ?
    Char[143] = ?
    Char[144] = ?
    Char[145] = ?
    Char[146] = ?
    Char[147] = ?
    Char[148] = ?
    Char[149] = ?
    Char[150] = ?
    Char[151] = ?
    Char[152] = ?
    Char[153] = ?
    Char[154] = ?
    Char[155] = ?
    Char[156] = ?
    Char[157] = ?
    Char[158] = ?
    Char[159] = ?
    Char[160] =  
    Char[161] = ¡
    Char[162] = ¢
    Char[163] = £
    Char[164] = ¤
    Char[165] = ¥
    Char[166] = ¦
    Char[167] = §
    Char[168] = ¨
    Char[169] = ©
    Char[170] = ª
    Char[171] = «
    Char[172] = ¬
    Char[173] = ­
    Char[174] = ®
    Char[175] = ¯
    Char[176] = °
    Char[177] = ±
    Char[178] = ²
    Char[179] = ³
    Char[180] = ´
    Char[181] = µ
    Char[182] = ¶
    Char[183] = ·
    Char[184] = ¸
    Char[185] = ¹
    Char[186] = º
    Char[187] = »
    Char[188] = ¼
    Char[189] = ½
    Char[190] = ¾
    Char[191] = ¿
    Char[192] = À
    Char[193] = Á
    Char[194] = Â
    Char[195] = Ã
    Char[196] = Ä
    Char[197] = Å
    Char[198] = Æ
    Char[199] = Ç
END

Note how 128-159 printed "?", 161-199 were special characters, and that 0-32 didn't print anything at all (or at least printed squares).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM