[英]Substitution encryption
I'm trying to make a substitution encryption program in java, but I'm having trouble creating the code and I'm wondering if you have any tips. 我正在尝试用Java编写替代加密程序,但是在创建代码时遇到了麻烦,我想知道您是否有任何提示。 My idea was to make the alphabet into numbers and then compare it to the plainText and then make a new string to send back.
我的想法是将字母转换为数字,然后将其与plainText进行比较,然后制作一个新字符串以发送回去。
This is my code: 这是我的代码:
package ComputerSecurity;
public class Substitution {
String str = "abcdefghijklmnopqrstuvwxyz";
public String encrypt(int key, String plainText){
StringBuilder temp = new StringBuilder();
for(int i=0; i< plainText.length(); i++){
int position = plainText.charAt(i);
temp.append(str.charAt(plainText.indexOf(position)+key));
}
return temp.toString();
}
}
key=3
but it's sent in by the main method plainText
is "hello world". key=3
但是它是通过主要方法发送的plainText
是“ hello world”。
I think you try to implement a Caesar Cipher. 我认为您尝试实施凯撒密码。
plainText.charAt(i)
returns the actual current character, not a position. 返回实际的当前字符,而不是位置。 You can use the character to find the position in the alphabet:
您可以使用该字符在字母表中找到位置:
char c = plainText.charAt(i)
int aPos = str.indexOf(c);
Then you can move this position and get the encrypted letter, but you need to wrap around. 然后,您可以移动该位置并获得加密的字母,但需要重新包装。 This is usually done with a modulo operation:
这通常通过模运算来完成:
int encPos = aPos;
if (key >= 0) {
encPos = (aPos + key) % str.length();
} else {
// the key may be bigger than the alphabet length
encPos = (str.length() + ((key + aPos) % str.length())) % str.length();
}
char ec = str.indexOf(encPos);
For every char where aPos
is -1, you can skip the encryption. 对于
aPos
为-1的每个字符,您可以跳过加密。 If you do this, then you can also encrypt spaces and other non-alphabet characters. 如果执行此操作,则还可以加密空格和其他非字母字符。
The problem with your idea to return a "number string" is that you don't know where one character number ends and where the next one begins. 返回“数字字符串”的想法的问题是,您不知道一个数字在哪里结束,下一个数字在哪里开始。 So, if you want that, you will have to add delimiters and this bloats your ciphertext unnecessarily.
因此,如果需要,您将必须添加定界符,这会不必要地使密文膨胀。
There is 3 problems: 有3个问题:
str
. str
。 It will limit the alphabet you can use. str
. str
。 str
. str
中不存在的位置。 My suggestion would be tu use the int
value of the char
. 我的建议是使用
char
的int
值。 You can get it with plainText.charAt(i)
. 您可以使用
plainText.charAt(i)
来获取它。
For "abc" it will return 97, 98 and 99 since the code for a
is 97. If you substract 96, you'll get 1 for a
, 2 for b
... “ABC”时它会返回97,98和99,因为在代码
a
是97。如果你96。减去,您将获得1 a
,2 b
...
You'll just have to add your key to it. 您只需要在其中添加密钥即可。
package ComputerSecurity;
public class Substitution {
public String encrypt(int key, String plainText){
StringBuilder temp = new StringBuilder();
for(int i=0; i< plainText.length(); i++){
int val = plainText.charAt(i) - 96;
temp.append((val+key) + ";");
}
return temp.toString();
}
encrypt(3, "Hello World")
will output -21,8,15,15,18,-61,26,18,21,15,7,
. -21,8,15,15,18,-61,26,18,21,15,7,
encrypt(3, "Hello World")
将输出-21,8,15,15,18,-61,26,18,21,15,7,
If you want to get 11,8,15,15,18,-61,26,18,21,15,7,
put the plainText toLowerCase()
. 如果要获取
11,8,15,15,18,-61,26,18,21,15,7,
请将plainText放入LowerCase toLowerCase()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.