java - OneTimePad 加密
问题描述
我正在使用 ascii 值处理加密文本,并且我有以下代码:
import java.util.*;
public class OneTimePad{
public static void main(String[] args){
Scanner _user_ = new Scanner(System.in); //Scanner Obj
System.out.print(" Enter your Message : ");
String Message = _user_.next();
System.out.print(" Enter your Key : ");
String Key = _user_.next();
char Fnal;
int Total;
for(int l = 0 , m = 0; (l < Message.length() && m < Key.length()); l++,m++){
Total = (int)Message.charAt(l) + (int)Key.charAt(m);
if((Total >=65 && Total <= 90) || (Total >= 97 && Total <=122 )){
System.out.print("Case 1");
Fnal = (char)Total;
System.out.print(Fnal);
}else{
System.out.print("Case 2");
int a = Total % 26;
Fnal = (char)a;
System.out.print(Fnal);
}
}
}
}
这是我的输出:
输入您的信息:你好
输入您的密钥: hihih
为什么只有这两行打印在控制台上?我没有看到任何错误。
更新
这是我的新代码:
for(int l = 0 , m = 0; (l < Message.length() && m < Key.length()); l++,m++) {
Total = (int)Message.charAt(l) + (int)Key.charAt(m);
//char s = Message.charAt(l);
char dos = Message.charAt(l);
String comp = String.valueOf(dos);
if (comp.contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ")){
if(Total >=65 && Total <= 90){
Fnal = (char)Total;
System.out.print(Fnal);
}else{
int a = Total % 26 + 65 ;
Fnal = (char)a;
System.out.print(Fnal);
}
}
}
为什么它没有工作?
解决方案
import java.util.Scanner;
public class Onetimepad{
public static void main(String[] args){
Scanner _user_ = new Scanner(System.in); // A Scanner Obj for to take input from user.
// System.in is to writing something on console
// Take Message and key from user
System.out.print(" Enter Message : ");
String Message = _user_.next();
System.out.print(" Enter Key ");
String Key = _user_.next();
// Converting each String latter into Character . And finding that latter's ascii value and
// And add that value to key's ascii value , and if it is greater than max than % 26.
if (Message.length() == Key.length())
{
for(int i = 0 , j = 0 ; i < Message.length() && j < Key.length() ; i++,j++)
{
char chmsg = Message.charAt(i); // "V" = 'V'
char chkey = Key.charAt(j); // "D" = 'D'
int chpr = (int)chmsg + (int)chkey;
if(chpr >= 65 && chpr <= 90)
{
System.out.print((char)chpr);
}
else
{
chpr = chpr % 26+ 65;
System.out.print((char)chpr);
}
}
}
else
{
System.out.print(" Length of Message and Key is not same : ");
}
}
}
// What it is write code and the logic is right.
//Please Tell, It's a OneTimePad Encryption.
推荐阅读
- google-cloud-platform - 在同一项目中验证 Google Pubsub 推送的 Cloud Functions URL
- excel - 根据持续时间拆分日期和行
- c# - 一个用户的 Cookie ID 从一个浏览器复制到另一个浏览器 - 它被劫持 - 如何解决这个问题
- cloud - 如何访问 IBM 云私有 CE 控制台管理?
- c# - 在 C# 和 SQL 中拆分表
- python - 根据数组中的条件选择数组数组?
- pytorch - PyTroch,梯度计算
- r - 逐行计算
- python - 如何在python中从pdf中提取表格数据?
- node.js - 在詹金斯工作中下载文件