首页 > 解决方案 > 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);
    }
  }
}

为什么它没有工作?

标签: java

解决方案


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.

推荐阅读