我在我的密码书中加入了这两个密码,虽然我只是为了好玩才实现它们。在此之后,我继续开发基于这两种密码的密码,将每个密码的弱点降到最低。我很感谢你对此的专家意见,如果可能的话,可以进行密码分析。
import java.util.*;
import java.io.*;
class CaesarCipher{
static String encrypt(String s, int key){
List<Character> arr = Arrays.asList('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
StringBuilder str = new StringBuilder();
int index;
for(int i = 0; i< s.length(); i++)
{
if(arr.contains(Character.toUpperCase(s.charAt(i))) == false)
str.append(s.charAt(i));
else{
index = arr.indexOf(Character.toUpperCase(s.charAt(i)));
str.append(arr.get(Math.abs(index + key) % arr.size()));
}
}
return str.toString();
}
}
class VigenereCaesar{
static String encrypt(String s, String k, int h){
List<Character> arr = Arrays.asList('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
for(int i = 0; i< s.length(); i++)
{
if(i % k.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(s.charAt(i))) == false)
str.append(s.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(s.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + index2) % arr.size()));
}
}
return str.toString();
}
static String encryptFile(BufferedReader buff, String k,int h) throws IOException{
List<Character> arr = Arrays.asList('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
String input = buff.readLine();
int count = 0;
while(input != null){
for(int i = 0; i< input.length(); i++)
{
if(count % k.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(input.charAt(i))) == false)
str.append(input.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(input.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + index2) % arr.size()));
}
count++;
}
str.append("\n");
input = buff.readLine();
}
return str.toString();
}
static String decrypt(String s, String k,int h){
List<Character> arr = Arrays.asList('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
for(int i = 0; i < s.length(); i++)
{
if(i % key.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(s.charAt(i))) == false)
str.append(s.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(s.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + (arr.size() - index2) )% arr.size()));
System.out.println(s.charAt(i) + " ---> " + arr.get((index1 + (arr.size() - index2) )% arr.size()));
}
}
return str.toString();
}
static String decryptFile(BufferedReader buff, String k,int h) throws IOException{
List<Character> arr = Arrays.asList('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
String input = buff.readLine();
int count = 0;
while(input!= null){
for(int i = 0; i < input.length(); i++)
{
if(count % key.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(input.charAt(i))) == false)
str.append(input.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(input.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + (arr.size() - index2) )% arr.size()));
}
count++;
}
str.append("\n");
input = buff.readLine();
}
return str.toString();
}
}
public class MyCipher{
public static void main(String[] args) throws IOException{
if(args.length == 2){
try{
BufferedReader fileBuff = new BufferedReader(new FileReader(args[0]));
if(!args[1].equals("encrypt") && !args[1].equals("decrypt")){
System.out.println("Error Usage:\n- Encryption: java MyCipher fileName encrypt\n- Decryption: java MyCipher fileName decrypt");
System.exit(1);
}
if(args[1].equals("encrypt")){
System.out.println("Enter encryption key (The longer and more random, the harder it gets to decrypt!) :");
String key; int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.println("Encrypting file...");
String out = VigenereCaesar.encryptFile(fileBuff,key,hash);
try{
PrintWriter writer = new PrintWriter(args[0] + " - Encrypted");
writer.write(out);
writer.close();
}catch(IOException exc){
System.out.println("Error: Could not write to file." + exc.getMessage());
System.exit(1);
}
System.out.println("File encryption complete.");
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: i_kh@icloud.com >\n");
System.exit(0);
}
else if(args[1].equals("decrypt")){
System.out.println("Enter decryption key:");
String key;
int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.println("Decrypting file...");
String out = VigenereCaesar.decryptFile(fileBuff,key,hash);
try{
PrintWriter writer = new PrintWriter(args[0] + "-Decrypted");
writer.write(out);
writer.close();
}catch(IOException exc){
System.out.println("Error: Could not write to file." + exc.getMessage());
System.exit(1);
}
System.out.println("File decryption complete.");
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: i_kh@icloud.com >\n");
System.exit(0);
}
}catch(IOException exc){
System.out.println("Error: Cannot procces file. " + exc.getMessage());
System.exit(1);
}
}
if(args.length != 1){
System.out.println("Error Usage:\n- Encryption: java MyCipher encrypt\n- Decryption: java MyCipher decrypt");
System.exit(1);
}
if(args[0].equals("encrypt")){
System.out.println("Enter encryption key (The longer and more random, the harder it gets to decrypt!) :");
String key; int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.print("Plaintext: ");
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
String out = VigenereCaesar.encrypt(s,key,hash);
System.out.println("-----------\nCiphertext: " + out);
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: i_kh@icloud.com >\n");
System.exit(0);
}
else if(args[0].equals("decrypt")){
System.out.println("Enter decryption key:");
String key;
int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.print("Ciphertext: ");
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
String out = VigenereCaesar.decrypt(s,key,hash);
System.out.println("\n-----------\nPlaintext: " + out);
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: i_kh@icloud.com >\n");
System.exit(0);
}
else{
System.out.println("Error Usage:\n- Encryption: java MyCipher encrypt\n- Decryption: java MyCipher decrypt");
System.exit(1);
}
}
}
发布于 2016-07-12 07:56:28
坦率地说,缩进是混乱的,我不认为它完全是由Markdown格式造成的。
同时,请在你的牙套风格上保持一致。例如:
if(arr.contains(Character.toUpperCase(s.charAt(i))) == false)
str.append(s.charAt(i));
else {
index = arr.indexOf(Character.toUpperCase(s.charAt(i)));
str.append(arr.get(Math.abs(index + key) % arr.size()));
}
不需要浏览额外的{
和}
字符,如果没有它们,则会使扫描代码稍微困难一些,IMHO。
if
条件if(arr.contains(Character.toUpperCase(s.charAt(i))) == false) { /* ... */ }
这可以通过以下方式更好地表达,而无需使用== false
:
if(!arr.contains(Character.toUpperCase(s.charAt(i)))) { /* ... */ }
在VigenereCaesar
中:
static String encrypt(String s, String k, int h){ /* ... */ }
现在还不清楚这些变量代表的是什么。建议使用完整的表单。
您可能需要考虑使用一些第三方图书馆来帮助您解析命令行选项,而不是自己动手。
try-with-resource
您还应该使用try-with-resource
一次,以便安全有效地处理Scanner
实例和底层System.in
输入流。例如:
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String mode = getMode(scanner);
if (mode.equals("encrypt")) {
String key = getKey(scanner);
String hash = getHash(scanner);
// do something with key and hash
} else if (mode.equals("decrypt")) {
// similar to above
} else {
// Indicate input error
}
}
}
在方法getMode(Scanner)
、getKey(Scanner)
和getHash(Scanner)
中,它们可以简单地调用适当的方法来获取用户输入,并调用throws IOException
将任何I/O异常传播回main()
方法。但是,验证错误应该在方法中处理,这样就可以期望它们返回一个结果。
List
集合您正在重复声明Arrays.asList('A', 'B', 'C', /* ... */ 'Z')
。您可以考虑将它作为一个不可修改的public static final List<Character>
集合,然后只在您想要对其进行洗牌的情况下,创建一个基于它的新实例。
https://codereview.stackexchange.com/questions/134031
复制相似问题