一次性密码加密

huangapple go评论142阅读模式
英文:

OneTimePad Encryption

问题

我正在使用ASCII值处理加密文本我有以下代码

```java
import java.util.*;
public class OneTimePad{
   public static void main(String[] args){

        Scanner _user_ = new Scanner(System.in);  //Scanner对象
    
        System.out.print("输入您的消息:");
        String Message = _user_.next();
        System.out.print("输入您的密钥:");
        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);
            }
        }
    }
}

这是我的输出:

输入您的消息: hello

输入您的密钥: 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);
    }
  }
}

为什么它不起作用?


<details>
<summary>英文:</summary>

I&#39;m working on encrypted text using ascii value and I have this code:

    import java.util.*;
    public class OneTimePad{
       public static void main(String[] args){
    
            Scanner _user_ = new Scanner(System.in);  //Scanner Obj
        
            System.out.print(&quot; Enter your Message : &quot;);
            String Message = _user_.next();
            System.out.print(&quot; Enter your Key : &quot;);
            String Key = _user_.next();
            
            char Fnal;
            int Total;
    
             for(int l = 0 , m = 0; (l &lt; Message.length() &amp;&amp; m &lt; Key.length()); l++,m++){   
                Total = (int)Message.charAt(l) + (int)Key.charAt(m);
               
                if((Total &gt;=65 &amp;&amp; Total &lt;= 90) || (Total &gt;= 97 &amp;&amp; Total &lt;=122 )){
                        System.out.print(&quot;Case 1&quot;);
                        Fnal = (char)Total;
                        System.out.print(Fnal);
                }else{
                    System.out.print(&quot;Case 2&quot;);
                    int a    = Total % 26;
                    Fnal = (char)a;
                    System.out.print(Fnal);
                }
            }
        }
    }

Here is my output:

&gt; **Enter your Message :** hello
&gt; 
&gt; **Enter your Key :** hihih

Why only this two lines are printed on console? I don&#39;t see any error.


----------

**UPDATE**

Here&#39;s my new code:

    for(int l = 0 , m = 0; (l &lt; Message.length() &amp;&amp; m &lt; 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(&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;)){
        if(Total &gt;=65 &amp;&amp; Total &lt;= 90){
          Fnal = (char)Total;
          System.out.print(Fnal);
        }else{
           int a    = Total % 26 + 65 ;
           Fnal = (char)a;
           System.out.print(Fnal);
        }
      }
    }

Why it didn&#39;t Working?



</details>


# 答案1
**得分**: 0

以下是翻译好的部分:

不是每个整数都与可见字符关联。您可以使用这个简单的方法来打印整数列表和相关字符:

```java
public static void printChars(){
    for (int i=0; i<1000; i++){
        System.out.println(i + " -> " + (char)i);
    }//for
}//printChars

那么,为什么你看不到任何输出呢?让我们尝试一个例子。

输入您的消息: a

输入您的密钥: s

  • (int)Message.charAt(l) 将是 97;
  • (int)Key.charAt(m) 将是 115;
  • 最后,Total = (int)Message.charAt(l) + (int)Key.charAt(m) 将是 212

两个条件 Total >= 65 && Total <= 90Total >= 97 && Total <= 122 都没有 得到验证,所以我们必须看看在 "else" 块中发生了什么。

  • int a = Total % 26 将是 4

正如您可以通过使用先前的方法 printChars() 看到的,数字 4 没有与可见字符关联,因此没有显示任何输出。


提示: 尝试使用一些数字输入,例如

输入您的消息: 0

输入您的密钥: 3

  • Total 将是 48+51=99

条件得到验证,所以我们必须进入 "if" 块。输出将是 (int)99,所以我们将看到:

c


更新

在您的新代码中,首先您必须将 TotalFnal 定义为 int。例如:

int Total = (int)Message.charAt(l) + (int)Key.charAt(m);

主要错误是使用了 contains(...)

如果您写 comp.contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),这意味着字符串必须包含 ABCDEFGHIJKLMNOPQRSTUVWXYZ 作为子字符串(而不是 "A 或 B 或 C ...")。

可能您需要一个 Pattern,您可以在这里阅读类似的问题:https://stackoverflow.com/questions/5238491/check-if-string-contains-only-letters

英文:

Not every integer has a visible character associated with it. You can use this simple method to print a list of integer and the relative char:

public static void printChars(){
    for (int i=0; i&lt;1000; i++){
        System.out.println(i+&quot; -&gt; &quot;+(char)i);
    }//for
}//printChars

So, why you don't see any output? Lets try with an example.

> Enter your Message: a
>
> Enter your Key: s

  • (int)Message.charAt(l) will be 97;
  • (int)Key.charAt(m) will be
    115;
  • Finally, Total = (int)Message.charAt(l) + (int)Key.charAt(m) will be 212.

The two conditions Total &gt;=65 &amp;&amp; Total &lt;= 90 and Total &gt;= 97 &amp;&amp; Total &lt;=122 are not verified, so we have to see what happens in the "else" block.

  • int a = Total % 26 will be 4

As you can see by using the previous method printChars(), no visible character is associated with the number 4, therefore nothing is shown as output.


Tip: try to use some numeric input, for example

> Enter your Message: 0
>
> Enter your Key: 3

  • Total will be 48+51=99

The conditions are verified, so we have to go in the "if" block. The output will be (int)99, so we will see:

> c


UPDATE

In your new code, first of all you have to define Total and Fnal as int. For example:

> int Total = (int)Message.charAt(l) + (int)Key.charAt(m);

The main error is the use of contains(...).

If you write comp.contains(&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;), you say that the string must have ABCDEFGHIJKLMNOPQRSTUVWXYZ as a substring (and not "A or B or C ...").

Probably you need a Pattern, and you can read a similar question here: https://stackoverflow.com/questions/5238491/check-if-string-contains-only-letters

答案2

得分: 0

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 : ");
        }
    }
}

Please note that the code provided above has not been translated, as it contains programming code that is not suitable for translation. If you have any specific questions or need assistance with understanding the code, feel free to ask.

英文:
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(&quot; Enter Message : &quot;);
String Message = _user_.next();
System.out.print(&quot; Enter Key &quot;);
String Key = _user_.next();
// Converting each String latter into Character . And finding that latter&#39;s ascii value and 
// And add that value to key&#39;s ascii value , and if it is greater than max than % 26.
if (Message.length() == Key.length())
{	
for(int i = 0 , j = 0 ; i &lt; Message.length() &amp;&amp; j &lt; Key.length() ; i++,j++)
{
char chmsg = Message.charAt(i);      // &quot;V&quot; = &#39;V&#39;    
char chkey = Key.charAt(j);	   // &quot;D&quot; = &#39;D&#39;	
int chpr = (int)chmsg + (int)chkey; 
if(chpr &gt;= 65 &amp;&amp; chpr &lt;= 90)
{
System.out.print((char)chpr);
}
else
{
chpr = chpr % 26+ 65;
System.out.print((char)chpr);
}
} 
}
else
{
System.out.print(&quot; Length of Message and Key is not same : &quot;);
}
}
} 
// What it is write code and the logic is right.
//Please Tell, It&#39;s a OneTimePad Encryption.

huangapple
  • 本文由 发表于 2020年7月31日 00:47:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63177757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定