使用密码程序编码和解码文本 [英] Encoding and Decoding text using a Cipher program

查看:176
本文介绍了使用密码程序编码和解码文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学的项目创建一个密码程序,我可以创建密码,但是我无法编码它。我的代码在下面,有人可以引导我走向正确的方向吗?前两种方法是确定的,主要是我遇到的最后两种方法。这是我第一学期做任何类型的编程,所以我很新的。

I'm trying to create a Cipher program for a project for college and I am able to create the cipher but I am unable to then encode it. My code is below, can someone steer me in the right direction? The first two methods are ok its mainly the last two methods I am having a problem with. This is my first semester doing any kind of programming so I am quite new to it.

import java.util.Scanner;
import java.util.Random;

public class Cipher
{
public static void main (String[] args){
    System.out.print("Please type a sentence to be encrypted\n");
    Scanner inputScanner = new Scanner(System.in);
    String input = inputScanner.next();
    input = input.toLowerCase();
    char[] inputArray=input.toCharArray();
    inputScanner.close();

    char[] alphabetArray = {' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'
            ,'r','s','t','u','v','w','x','y','z'};
    char[] cipherArray = alphabetArray.clone();
    createCipher(cipherArray);
    encrypt(alphabetArray, inputArray, cipherArray);
    for(int index=0; index<alphabetArray.length; index++)
    {
        System.out.print(alphabetArray[index]);    
    }
    System.out.print("\n");
    for(int index2=0; index2<cipherArray.length; index2++)
    {
        System.out.print(cipherArray[index2]);
    }
    System.out.print("\nYour encrypted message is:\n");
    for(int index3=0; index3<inputArray.length; index3++)
    {
        System.out.print(inputArray[index3]);
    }
    System.out.print("\n");
    decrypt(alphabetArray, inputArray, cipherArray);
    for(int index4=0;index4<inputArray.length; index4++)
    {
        System.out.print(inputArray[index4]);
    }

}
public static void createCipher( char[] alphabet )
{
    if (alphabet!=null)                                                
    {                                                                
        Random generator = new Random();
        for (int index=0; index<alphabet.length; index++ )
        {
            int otherIndex = generator.nextInt(alphabet.length);
            char temp = alphabet[index];
            alphabet[index] = alphabet[otherIndex];
            alphabet[otherIndex] = temp;
        }
    }
}

public static void encrypt(char[] alphabet, char[] input, char[] cipher)
{
    if(input!=null)
    {
        for(int index =0; index<input.length; index++)
        {
            for(int index2 =0; index2<alphabet.length; index2++)
            {
                if(input[index]==alphabet[index2])
                {
                    input[index]=cipher[index2];
                }
            }
        }    
    }
}

public static void decrypt(char[] alphabet, char[] encryptedInput, char[] cipher)
{
    if(encryptedInput!=null)
    {
        for(int index =0; index<encryptedInput.length; index++ )
        {
            for(int index2=0; index2<cipher.length; index2++)
            {
                if(encryptedInput[index]==cipher[index2])
                {
                    encryptedInput[index]=alphabet[index2];
                }
            }
        }
    }
}

}

推荐答案

在此方法中

public static void encrypt(char[] alphabet, char[] input, char[] cipher)
{
    if(input!=null)
    {
        for(int index =0; index<input.length; index++)
        {
            for(int index2 =0; index2<alphabet.length; index2++)
            {
                if(input[index]==alphabet[index2])
                {
                    input[index]=cipher[index2];
                }
            }
        }    
    }
}

您需要在语句 input [index] = cipher [index2]; 之后键入 return; 应该在decrypt()方法中做同样的事情。那么你的程序应该很好现在,你还可以手动执行错误的代码,并告诉我为什么需要一个return语句?

you need to type return; after the statement input[index]=cipher[index2]; You should do the same in the decrypt() method. Then your program should be fine. Now, could you also execute the incorrect code by hand, and tell me why you needed a return statement?

这篇关于使用密码程序编码和解码文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆