Skip to content

Commit

Permalink
fix:修改所有输出为英文
Browse files Browse the repository at this point in the history
  • Loading branch information
lzwgiter committed Jan 31, 2024
1 parent c250900 commit d8c84ff
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 44 deletions.
47 changes: 25 additions & 22 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void banner() {
" / ____/______ ______ / /_____ / __ )____ ______\n" +
" / / / ___/ / / / __ \\/ __/ __ \\______/ __ / __ `/ ___/\n" +
"/ /___/ / / /_/ / /_/ / /_/ /_/ /_____/ /_/ / /_/ / / \n" +
"\\____/_/ \\__, / .___/\\__/\\____/ /_____/\\__,_/_/ (by lzwgiter v1.0) \n" +
"\\____/_/ \\__, / .___/\\__/\\____/ /_____/\\__,_/_/ (by lzwgiter v1.1) \n" +
" /____/_/ \n" +
"\033[0m"
);
Expand All @@ -35,26 +35,26 @@ private static CommandLine parseArgs(String[] args) {
// 帮助选项
options.addOption(Option.builder("h")
.longOpt("help")
.desc("显示帮助信息")
.desc("show this message")
.build());
// 支持算法
options.addOption(Option.builder("l")
.longOpt("list")
.desc("查看支持算法")
.desc("Supported algorithms")
.build());
// 密钥生成算法
options.addOption(Option.builder("g")
.longOpt("genKey")
.argName("keyType")
.desc("生成PEM格式公私钥对(RSA2048、ECC)")
.desc("Generate key pair")
.build());
// 算法名称
options.addOption(
Option.builder("n")
.longOpt("algo")
.argName("algo")
.hasArg()
.desc("算法名称,例如AES。-l查看所有支持算法。")
.desc("Algorithm name, using -l for supported algorithms")
.build()
);
// 工作模式
Expand All @@ -63,23 +63,23 @@ private static CommandLine parseArgs(String[] args) {
.longOpt("mode")
.hasArg()
.argName("mode")
.desc("工作模式,加密(e)、解密(d)、签名(s)、验签(v)")
.desc("Work mode, encrypt(e) | decrypt(d) | signature(s) | verify(v)")
.build()
);
// 密钥
options.addOption(
Option.builder("k")
.longOpt("key")
.hasArg()
.desc("加密/签名、解密/验签密钥文件路径")
.desc("Key file path")
.build()
);
// 签名内容
options.addOption(
Option.builder("s")
.longOpt("sign")
.hasArg()
.desc("待校验签名内容")
.desc("Signature to be verified")
.build()
);
// 输入内容
Expand All @@ -88,7 +88,7 @@ private static CommandLine parseArgs(String[] args) {
.longOpt("input")
.hasArg()
.argName("input")
.desc("输入内容")
.desc("Input data")
.build()
);
// 输出到文件
Expand All @@ -97,14 +97,14 @@ private static CommandLine parseArgs(String[] args) {
.longOpt("output")
.hasArg()
.argName("output")
.desc("输出文件路径")
.desc("Output file path")
.build()
);
// bonus
options.addOption(
Option.builder("x")
.longOpt("bonus")
.desc("彩蛋")
.desc("Bonus")
.build()
);
// 创建命令行解析器
Expand All @@ -115,20 +115,22 @@ private static CommandLine parseArgs(String[] args) {
line = parser.parse(options, args);
if (line.hasOption("x")) {
System.out.println(Utils.getBonus());
System.exit(0);
}
if (line.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar crypto-bar.jar -n <algo_name> [options] <args>\n\033[38;5;10m" +
"- eg: 生成SM3摘要:xxx -n sm3 -i <待哈希数据>\n" +
"- eg: 生成RSA公私钥:xxx -n rsa -g -o ./output\n" +
"- eg: 验签:xxx -n rsa -m d -i <原始数据> -s <签名内容> -k <公钥>\n" +
"- eg: 对称加[解]密:xxx -n sm4 -m e[d] -i <待加/[解]密数据> -k <秘密值>\033[0m",
"- eg: generate SM3 hash: xxx -n sm3 -i <data>\n" +
"- eg: generate RSA key pair: xxx -n rsa -g -o ./output\n" +
"- eg: Verify a Signature: xxx -n rsa -m d -i <original data> -s <signature> -k <pubkey>\n" +
"- eg: En/Decrypt: xxx -n sm4 -m e[d] -i <data to be en/decrypt> -k <secret>\033[0m",
options);
System.exit(0);
} else {
return line;
}
} catch (ParseException exp) {
System.out.println("参数解析错误,请使用 -h 查看帮助信息。");
System.out.println("parameter parse failed, using -h for usage.");
}
return null;
}
Expand All @@ -139,21 +141,22 @@ public static void main(String[] args) {
CommandLine cmdLine = parseArgs(args);
if (cmdLine != null) {
if (cmdLine.hasOption("l")) {
System.out.println("对称加密算法:SM4(128bits)、AES(192bits)");
System.out.println("非对称加密算法:SM2、RSA(2048bits)");
System.out.println("签名算法:ECC(Ed25519)");
System.out.println("哈希摘要算法:SM3、SHA256、MD5");
System.out.println("Symmetric: SM4(128bits), AES(192bits)");
System.out.println("Asymmetric: SM2, RSA(2048bits)");
System.out.println("Signature: ECC(Ed25519)");
System.out.println("Hash: SM3, SHA256, MD5");
System.exit(0);
}
if (!cmdLine.hasOption("n")) {
System.out.println("请指定要使用的算法!请使用-l查看支持的算法");
System.out.println("Algorithm name is needed, using -l for supported algorithms");
} else {
// 获取算法上下文和代理对象
AlgoContext context = AlgoContext.buildAlgoContext(cmdLine);
AlgoAgentAbstract agent = Utils.getAgentFactory(cmdLine.getOptionValue("n").toUpperCase());
if (agent != null) {
System.out.println(agent.process(context));
} else {
System.out.println("不受支持的算法!请使用-l查看支持的算法");
System.out.println("unsupported algorithm, using -l for supported algorithms");
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/asymmetric/AsymmetricAgentAbstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String process(AlgoContext context) {
return this.generateKey(context);
}
if (context.getMode() == null) {
throw new RuntimeException("请指定工作模式!");
throw new RuntimeException("Work mode is needed!, using -h for usage.");
}
// 否则为加解密功能
switch (context.getMode()) {
Expand Down Expand Up @@ -61,14 +61,14 @@ protected String generateKey(KeyPairGenerator keyPairGenerator, boolean writeToF
filePath + ".pub");
Utils.writeToFile(Utils.byteToBase64String(keyPair.getPrivate().getEncoded()),
filePath + ".pri");
return "公私钥已写入文件" + filePath;
return "key file:" + filePath + "(.pri/.pub)";
} else {
StringBuilder sb = new StringBuilder();
sb.append(Utils.getWineHere());
sb.append("\033[38;5;10m公钥(X.509格式):\033[0m\n-----BEGIN PUBLIC KEY-----\n");
sb.append("\033[38;5;10mpubkey(X.509):\033[0m\n-----BEGIN PUBLIC KEY-----\n");
sb.append(Utils.normalizeFormat(keyPair.getPublic().getEncoded()));
sb.append("\n-----END PUBLIC KEY-----\n");
sb.append("\033[38;5;10m私钥(PKCS#8):\033[0m\n-----BEGIN PRIVATE KEY-----\n");
sb.append("\033[38;5;10mprikey(PKCS#8):\033[0m\n-----BEGIN PRIVATE KEY-----\n");
sb.append(Utils.normalizeFormat(keyPair.getPrivate().getEncoded()));
sb.append("\n-----END PRIVATE KEY-----");
return sb.toString();
Expand Down Expand Up @@ -166,9 +166,9 @@ protected String sign(String algoName, String signKey, String dataToSign) {
byte[] result = signature.sign();
StringBuilder sb = new StringBuilder();
sb.append(Utils.getWineHere());
sb.append("原始数据:");
sb.append("Original data:");
sb.append(dataToSign);
sb.append("\n签名数据:");
sb.append("\nSignature:");
sb.append(Utils.byteToBase64String(result));
return sb.toString();
} catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
Expand Down Expand Up @@ -196,9 +196,9 @@ protected String verify(String algoName, String verifyKey, String signToVerifyba
signature.initVerify(publicKey);
signature.update(originalDataHash.getBytes(StandardCharsets.UTF_8));
if (signature.verify(signToVerify)) {
return Utils.getWineHere() + "签名验证通过!";
return Utils.getWineHere() + "Signature valid!";
} else {
return Utils.getWineHere() + "签名验证失败!";
return Utils.getWineHere() + "Signature invalid!";
}
} catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
throw new RuntimeException(e);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/asymmetric/SM2Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public String generateKey(AlgoContext context) {
context.getOutputWay() + ".pub");
Utils.writeToFile(Utils.byteToBase64String(keyPair.getPrivate().getEncoded()),
context.getOutputWay() + ".pri");
return Utils.getWineHere() + "已写入文件:" + context.getOutputWay();
return Utils.getWineHere() + "key file:" + context.getOutputWay() + "(.pri/.pub)";
} else {
StringBuilder sb = new StringBuilder();
sb.append(Utils.getWineHere());
sb.append("\033[38;5;10m公钥(X.509格式):\033[0m\n-----BEGIN PUBLIC KEY-----\n");
sb.append("\033[38;5;10mpubkey(X.509):\033[0m\n-----BEGIN PUBLIC KEY-----\n");
sb.append(Utils.normalizeFormat(keyPair.getPublic().getEncoded()));
sb.append("\n-----END PUBLIC KEY-----\n");
sb.append("\033[38;5;10m私钥(PKCS#8):\033[0m\n-----BEGIN PRIVATE KEY-----\n");
sb.append("\033[38;5;10mprikey(PKCS#8):\033[0m\n-----BEGIN PRIVATE KEY-----\n");
sb.append(Utils.normalizeFormat(keyPair.getPrivate().getEncoded()));
sb.append("\n-----END PRIVATE KEY-----");
return sb.toString();
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/core/AlgoContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void setInputData(String cmdLineInputData) {
Scanner scanner = new Scanner(file).useDelimiter("\\Z");
this.inputData = scanner.next();
} catch (FileNotFoundException e) {
throw new RuntimeException("文件路径不存在!");
throw new RuntimeException("File path not exists!");
}
} else {
// 输入数据为包含"/"的字符串
Expand All @@ -91,7 +91,7 @@ public void setOutputWay(String cmdLineInputData) {
if (!outputFile.exists()) {
this.outputWay = cmdLineInputData;
} else {
throw new RuntimeException("当前路径已存在该文件!");
throw new RuntimeException("File already exists!");
}
}

Expand All @@ -112,10 +112,10 @@ public void setInputKey(String cmdLineInputData) {
Scanner scanner = new Scanner(file).useDelimiter("\\Z");
this.inputKey = scanner.next();
} catch (FileNotFoundException e) {
throw new RuntimeException("文件路径不存在!");
throw new RuntimeException("File path not exists!");
}
} else {
throw new RuntimeException("输入为目录,请使用参数-i输入文件!-h查看用法");
throw new RuntimeException("Input is a directory, using -i to input file, using -h for usage");
}
} else {
// 否则使用终端参数作为密钥
Expand All @@ -135,7 +135,7 @@ public void setSignature(String cmdLineInputData) {
Scanner scanner = new Scanner(file).useDelimiter("\\Z");
this.signature = scanner.next();
} catch (FileNotFoundException e) {
throw new RuntimeException("文件路径不存在!");
throw new RuntimeException("File path not exists!");
}
} else {
this.signature = cmdLineInputData;
Expand Down Expand Up @@ -178,7 +178,7 @@ public static AlgoContext buildAlgoContext(CommandLine cmdLine) {
context.setSignature(cmdLine.getOptionValue("s"));
}
} else {
throw new RuntimeException("请使用-k给出加/解密使用的密钥!使用-h查看用法");
throw new RuntimeException("key is needed, using -k to input key. using -h for usage");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/digest/DigestAgentAbstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class DigestAgentAbstract extends AlgoAgentAbstract {
@Override
public String process(AlgoContext context) {
if (context.getInputData() == null) {
throw new IllegalArgumentException("输入数据为空");
throw new IllegalArgumentException("Input data is empty! using -i to input data, using -h for usage.");
}
if (context.getOutputWay() != null) {
return this.digest(context.getInputData(), context.getOutputWay());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/digest/MD5Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public String digest(String data) {
@Override
public String digest(String data, String filePath) {
Utils.writeToFile(this.digest(data).substring(3), filePath);
return Utils.getWineHere() + "结果已经写入" + filePath;
return Utils.getWineHere() + "Result has been written to " + filePath;
}
}
2 changes: 1 addition & 1 deletion src/main/java/digest/SHA256Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public String digest(String data) {
@Override
public String digest(String data, String filePath) {
Utils.writeToFile(this.digest(data).substring(3), filePath);
return Utils.getWineHere() + "结果已经写入" + filePath;
return Utils.getWineHere() + "Result has been written to " + filePath;
}
}
2 changes: 1 addition & 1 deletion src/main/java/digest/SM3Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public String digest(String data) {
@Override
public String digest(String data, String filePath) {
Utils.writeToFile(this.digest(data).substring(3), filePath);
return Utils.getWineHere() + "结果已经写入" + filePath;
return Utils.getWineHere() + "Result has been written to " + filePath;
}
}
2 changes: 1 addition & 1 deletion src/main/java/symmetric/SymmetricAgentAbstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String process(AlgoContext context) {
} else if ("d".equals(context.getMode())) {
return decrypt(context);
} else {
return "错误的模式!请使用-h查看用法!";
return "Wrong working mode! using -h for usage.";
}
}

Expand Down

0 comments on commit d8c84ff

Please sign in to comment.