Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 修改密码_Java_Encryption - Fatal编程技术网

Java 修改密码

Java 修改密码,java,encryption,Java,Encryption,编辑:我稍微更改了代码。我试着让它更整洁 好的,我是Java的完全初学者。如果你必须将信息中的字母移动一定量(例如:shift 1=a变为B,B变为C,等等。shift 2=a->C,B->D,等等),那么我必须制作一个CaesaerCipher 我尝试了很多方法,但我一无所获。我不知道该试什么。帮忙 import java.util.*; import java.io.*; public class CaesarCipher { public static void main(Stri

编辑:我稍微更改了代码。我试着让它更整洁

好的,我是Java的完全初学者。如果你必须将信息中的字母移动一定量(例如:shift 1=a变为B,B变为C,等等。shift 2=a->C,B->D,等等),那么我必须制作一个CaesaerCipher

我尝试了很多方法,但我一无所获。我不知道该试什么。帮忙

import java.util.*;
import java.io.*;

public class CaesarCipher {

  public static void main(String[] args)
    throws FileNotFoundException, IOException {


    System.out.println("Welcome to CaesarCipher");
    System.out.print("Enter 1 to encipher, or 2 to decipher (-1 to exit): " );
    Scanner console = new Scanner(System.in);
    int i = console.nextInt();
    if (i == -1) {
      System.out.print("DONE!");
    //repeat question encipher/decipher/quit until choose quit
    }

    if (i == 1) {
      System.out.println();
      System.out.print("What shift should I use? ");
      int j = console.nextInt();
      System.out.println();
      System.out.print("What is the input file name? ");
      caesarEncipher();
    if (i == 1) { //from answer to encipher, decipher, or exit
      System.out.println(strLine);
      //send to String caesarDecipher
      //?????????????
    }
//    if (i == 2) { //from answer to encipher, decipher, or exit
      //send to String caesarEncipher
      //?????????????
//    }

      System.out.print("What is the output file name? ");
      String fileName = new Scanner(System.in).nextLine();
      //File file = new File( fileName );
//      fstream = new FileInputStream("/Users/steph/Desktop/Programming/!6 Problem Set TUES/PartB/6/cipher.txt");
         //change (".txt") to -fileName- ???
      //File file = new File( fileName );
//      while ((strLine = br.readLine()) != null)   {
//      System.out.println (strLine);
    }
  }

public static String caesarEncipher (int k)
  throws FileNotFoundException, IOException {

      //input file name code...
      //String fileName = scanner.nextLine();

      FileInputStream fstream = new FileInputStream("/Users/steph/Desktop/Programming/!6 Problem Set TUES/PartB/6/cipher.txt");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null)   {
      System.out.println (strLine);
    }
    in.close();
      System.out.println();


    int shiftKey = Integer.parseInt(k[1]);
    shiftKey = shiftKey % 26;  

      String cipherText = ""; 

      for (k=0; k<strLine.length(); k++){ 

         int asciiValue = (int) strLine.charAt(k); 
         if (asciiValue < 65 || asciiValue > 90){ 
            cipherText += strLine.charAt(k); 
            continue; 
         } 

         int basicValue = asciiValue - 65; 
         int newAsciiValue = 65 + ((basicValue + shiftKey) % 26)  ; 
         cipherText += (char) newAsciiValue; 

       } 

       System.out.print(cipherText);              

    in.close();
      System.out.println();
}
}

    //ignore any character that is not an uppercase alphabetic!!!!!!!!!  

//  public static String caesarDecipher (String input, int shift) {
    //reverse process to decipher
    //go back to what shift (line 11-12): j
    //make it -inputted number
    //ignore any character that is not an uppercase alphabetic!!!!!!!!!  
//  }

//  public static String alphabet (?) {
//    Iterator<String> abc = new Abc<Integer>()??;
    //list [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, Y, Z]
    //rotate(list, distance)
import java.util.*;
导入java.io.*;
公共类密码{
公共静态void main(字符串[]args)
抛出FileNotFoundException,IOException{
System.out.println(“欢迎使用凯撒密码”);
系统输出打印(“输入1进行加密,或输入2进行解密(-1退出):”;
扫描仪控制台=新扫描仪(System.in);
int i=console.nextInt();
如果(i==-1){
系统输出打印(“完成!”);
//重复问题加密/解密/退出,直到选择退出
}
如果(i==1){
System.out.println();
System.out.print(“我应该使用哪个班次?”);
int j=console.nextInt();
System.out.println();
System.out.print(“输入文件名是什么?”);
凯撒密码();
如果(i==1){//从应答到加密、解密或退出
系统输出打印LN(斯特林);
//发送到字符串解密程序
//?????????????
}
//如果(i==2){//从应答到加密、解密或退出
//发送到字符串加密程序
//?????????????
//    }
System.out.print(“输出文件名是什么?”);
字符串文件名=新扫描仪(System.in).nextLine();
//文件=新文件(文件名);
//fstream=newfileinputstream(“/Users/steph/Desktop/Programming/!6习题集TUES/PartB/6/cipher.txt”);
//将(“.txt”)更改为-fileName-???
//文件=新文件(文件名);
//而((strLine=br.readLine())!=null){
//System.out.println(strLine);
}
}
公共静态字符串加密(int k)
抛出FileNotFoundException,IOException{
//输入文件名代码。。。
//字符串文件名=scanner.nextLine();
FileInputStream fstream=newfileinputstream(“/Users/steph/Desktop/Programming/!6习题集TUES/PartB/6/cipher.txt”);
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;
而((strLine=br.readLine())!=null){
System.out.println(strLine);
}
in.close();
System.out.println();
int-shiftKey=Integer.parseInt(k[1]);
shiftKey=shiftKey%26;
字符串密文=”;
对于(k=0;k90){
密文+=strLine.charAt(k);
继续;
} 
int basicValue=ASCIVALUE-65;
int newascivalue=65+((基本值+移位键)%26);
密文+=(字符)newascivalue;
} 
系统输出打印(密文);
in.close();
System.out.println();
}
}
//忽略任何不是大写字母的字符!!!!!!!!!
//公共静态字符串解密(字符串输入,int移位){
//破译的反向过程
//回到哪个班次(第11-12行):j
//使之成为输入的数字
//忽略任何不是大写字母的字符!!!!!!!!!
//  }
//公共静态字符串字母(?){
//迭代器abc=新abc()??;
//列表[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,Y,Z]
//旋转(列表、距离)
编辑:问题是我在哪里加了星号。它希望k是一个数组。我不知道如何在不破坏其余代码的情况下做到这一点 至于学习,你会认为所有这些代码就足够了。我从只懂BBCode(什么都不懂)变成了这个。我主修心理学。我的大脑不是这样工作的。这很有趣,但我遇到了困难


Edit2:确切的错误是“需要数组,但找到int”

使用StringBuilder尝试此实现。它是一个专门为被多次修改的字符串创建的类。
编辑:另外,您最好将字符串转换为字符数组,因为您正在逐个字符地处理它

StringBuilder cipherText = new StringBuilder(); 
char[] letters = strLine.toCharArray();

for (k=0; k < letters.length; k++){ 

     int asciiValue = (int) letters[k]; 
     if (asciiValue < 65 || asciiValue > 90){ 
        cipherText.append( letters[k] );
        continue; 
     } 

     int basicValue = asciiValue - 65; 
     int newAsciiValue = 65 + ((basicValue + shiftKey) % 26)  ; 
     cipherText.append( (char) newAsciiValue );

} 

System.out.print( cipherText.toString() );
StringBuilder密文=新的StringBuilder();
char[]字母=strLine.toCharArray();
对于(k=0;k90){
密文。附加(字母[k]);
继续;
} 
int basicValue=ASCIVALUE-65;
int newascivalue=65+((基本值+移位键)%26);
附加((char)newascivalue);
} 
System.out.print(cipherText.toString());

您可以发布错误吗?我们可以为您写作业,但学习在哪里?
k
不能是数组。您看到的具体错误消息是什么?另外,使用方法来澄清您的逻辑,而不是将它们放在您的
main()中
,将使调试和可视化更加容易。@Makoto完全不相关。。但单片main=new BandName();