Java中的Caesar密码(西班牙语字符) 我在阅读,我想知道是否有任何方式来考虑整个字符范围?例如,“a”,“e”,“o”,“n'”,而不考虑“[(空间)]?(例如,我的字符串是“Hello World”,标准结果是“Khoor#Zruog”;我想删除该“#”,因此结果将是“KhoorZruog”)
我相信我的答案就在这段代码中:Java中的Caesar密码(西班牙语字符) 我在阅读,我想知道是否有任何方式来考虑整个字符范围?例如,“a”,“e”,“o”,“n'”,而不考虑“[(空间)]?(例如,我的字符串是“Hello World”,标准结果是“Khoor#Zruog”;我想删除该“#”,因此结果将是“KhoorZruog”),java,special-characters,encryption,Java,Special Characters,Encryption,我相信我的答案就在这段代码中: if (c >= 32 && c <= 127) { // Change base to make life easier, and use an // int explicitly to avoid worrying... cast later int x = c - 32; x = (x + shift) % 96;
if (c >= 32 && c <= 127)
{
// Change base to make life easier, and use an
// int explicitly to avoid worrying... cast later
int x = c - 32;
x = (x + shift) % 96;
chars[i] = (char) (x + 32);
}
if(c>=32&&c不过滤掉的ASCII码32的空格。您可以尝试:
if (c >= 33 && c <= 127)
{
// Change base to make life easier, and use an
// int explicitly to avoid worrying... cast later
int x = c - 32;
x = (x + shift) % 96;
chars[i] = (char) (x + 32);
}
如果(c>=33&&c未过滤掉的ASCII码32的空格。您可以尝试:
if (c >= 33 && c <= 127)
{
// Change base to make life easier, and use an
// int explicitly to avoid worrying... cast later
int x = c - 32;
x = (x + shift) % 96;
chars[i] = (char) (x + 32);
}
如果(c>=33&&c您可以使用它。它将为您检查给定的int值是否代表一个文本。
因此,您的函数可以如下所示:
if (Character.isLiteral(c) )
{
// Change base to make life easier, and use an
// int explicitly to avoid worrying... cast later
int x = c - Character.MIN_VALUE;
x = (x + shift) % Character.MAX_VALUE;
chars[i] = (char) (x + Character.MIN_VALUE);
}
您可以使用它。它将为您检查给定的int值是否代表一个文本。
因此,您的函数可以如下所示:
if (Character.isLiteral(c) )
{
// Change base to make life easier, and use an
// int explicitly to avoid worrying... cast later
int x = c - Character.MIN_VALUE;
x = (x + shift) % Character.MAX_VALUE;
chars[i] = (char) (x + Character.MIN_VALUE);
}
请参阅此伪代码-应该可以轻松实现:
// you need to define your own range, obviously - it's not at all obvious whether
// e.g. "ź" should be included and that it should come after "z"
array char_range = ['a','á','b','c','č', (...), 'z','ź','ž']
// the text to encode
string plaintext = 'some text here'
// this will contain encoded text
stringbuilder ciphertext = ''
// the classic Caesar Cipher shifts by 3 chars to the right
// to decipher, reverse the sign
int shift_by = 3
// note: character != byte, esp. not in UTF-8 (1 char could be 1 or more bytes)
for each character in plaintext
get character_position of character in char_range // e.g. "a" would return 0
if not in char_range // e.g. spaces and other non-letters
do nothing // drop character
// alternately, you can append it to ciphertext unmodified
continue with next character
add shift_by to character_position
if character_position > char_range.length
character_position modulo char_range.length
if character_position < 0 // useful for decoding
add char_range.length to character_position
get new_character at character_position
append new_character to ciphertext
done
//显然,您需要定义自己的范围-是否
//例如,“ź”应包括在内,且应在“z”之后
数组字符范围=['a'、'a'、'b'、'c'、'c'、č'、(…)、'z'、'ź'、'ž']
//要编码的文本
字符串纯文本='some text here'
//这将包含编码文本
stringbuilder密文=“”
//经典的凯撒密码向右移动3个字符
//要破译,请反转符号
int shift_by=3
//注意:字符!=字节,特别是UTF-8中没有的字符(1个字符可以是1个或更多字节)
对于纯文本中的每个字符
获取字符范围中字符的位置//例如,“a”将返回0
如果不在字符范围内//例如空格和其他非字母
什么也不做//删除字符
//或者,您可以将其附加到未经修改的密文中
继续下一个字符
将shift\u by添加到字符位置
如果字符位置>字符范围.length
字符位置模字符范围长度
如果字符_位置<0//用于解码
将char_range.length添加到character_位置
在字符位置获取新字符
在密文中附加新的_字符
完成
请参阅此伪代码-应该可以轻松实现:
// you need to define your own range, obviously - it's not at all obvious whether
// e.g. "ź" should be included and that it should come after "z"
array char_range = ['a','á','b','c','č', (...), 'z','ź','ž']
// the text to encode
string plaintext = 'some text here'
// this will contain encoded text
stringbuilder ciphertext = ''
// the classic Caesar Cipher shifts by 3 chars to the right
// to decipher, reverse the sign
int shift_by = 3
// note: character != byte, esp. not in UTF-8 (1 char could be 1 or more bytes)
for each character in plaintext
get character_position of character in char_range // e.g. "a" would return 0
if not in char_range // e.g. spaces and other non-letters
do nothing // drop character
// alternately, you can append it to ciphertext unmodified
continue with next character
add shift_by to character_position
if character_position > char_range.length
character_position modulo char_range.length
if character_position < 0 // useful for decoding
add char_range.length to character_position
get new_character at character_position
append new_character to ciphertext
done
//显然,您需要定义自己的范围-是否
//例如,“ź”应包括在内,且应在“z”之后
数组字符范围=['a'、'a'、'b'、'c'、'c'、č'、(…)、'z'、'ź'、'ž']
//要编码的文本
字符串纯文本='some text here'
//这将包含编码文本
stringbuilder密文=“”
//经典的凯撒密码向右移动3个字符
//要破译,请反转符号
int shift_by=3
//注意:字符!=字节,特别是UTF-8中没有的字符(1个字符可以是1个或更多字节)
对于纯文本中的每个字符
获取字符范围中字符的位置//例如,“a”将返回0
如果不在字符范围内//例如空格和其他非字母
什么也不做//删除字符
//或者,您可以将其附加到未经修改的密文中
继续下一个字符
将shift\u by添加到字符位置
如果字符位置>字符范围.length
字符位置模字符范围长度
如果字符_位置<0//用于解码
将char_range.length添加到character_位置
在字符位置获取新字符
在密文中附加新的_字符
完成
<代码>定义“整范围”。我不考虑<代码> O/<代码>,但将包含<代码> <代码> -显然,范围定义由你决定。接近ASCII 32-127的任意位置;根据字符集的不同,它们可能位于任意位置。只要保留26个基本拉丁字母,字符集将不会在所选编码中形成连续范围。定义“整个范围”我不会考虑<代码> O/<代码>,但是会包含<代码> <代码>——很明显,范围定义是由你决定的,也不是,“a”,“e”,“o”或“n'”。任何地方都接近ASCII 32-127;根据字符集的不同,它们可能在任何地方。只要您保留26个基本拉丁字母,字符集就不会在所选编码中形成连续的范围。当然,虽然这是一个很好的练习,但它并不是真正的密码学-即5岁以上的任何人都可以在5分钟。当然,虽然这是一个很好的练习,但它并不是真正的密码——也就是说,5岁以上的任何人都可以在5分钟内破解。