简体   繁体   English

用Java解密加密的文本

[英]Decrypt encrypted text in java

i have an assignment. 我有一个作业。 i have done all the parts of the assignment. 我已经完成了作业的所有部分。 but i am strucked in below part of my assignment. 但我对我的作业感到震惊。 can you help me 你能帮助我吗

Assignment guidelines 分配准则

III.Write one constructor method for the EncryptedNode class. III。为EncryptedNode类编写一个构造方法。 This constructor should take a String as an argument. 此构造函数应将String作为参数。 Unlike other constructors you have written, this constructor should function conditionally. 与您编写的其他构造函数不同,此构造函数应有条件地起作用。 If the message is one character long, simply assign that character to the letter instance variable. 如果消息是一个字符长,只需将该字符分配给字母实例变量。 If the message is two characters long, assign the first character to the letter, and the other character as a string to the right EncryptedNode. 如果消息长两个字符,则将第一个字符分配给字母,将另一个字符作为字符串分配给右侧的EncryptedNode。 In any other case, find the middle value of the input String, assign the character at the first index to letter, characters 在任何其他情况下,请找到输入字符串的中间值,并将第一个索引处的字符分配给字母,字符

from 1 up to the middle character should go to the right EncryptedNode, and the remaining unclaimed 从1到中间的字符应转到正确的EncryptedNode,其余未声明

characters should go the left EncryptedNode. 字符应位于左侧的EncryptedNode。 Note: you will be instantiating new EncryptedNode objects from this constructor, thus creating a recursive construction. 注意:您将从此构造函数实例化新的EncryptedNode对象,从而创建递归构造。

IV. IV。 Write the following methods: 编写以下方法:

a. 一种。 The decrypt method takes no arguments and returns a String. 解密方法不带任何参数,并返回一个字符串。 This method should re-build the original unencrypted String by adding the letter, left and right elements of each EncryptedNode recursively. 此方法应通过递归添加每个EncryptedNode的字母,左元素和右元素来重建原始的未加密String。 This process will be left to you to solve. 这个过程将留给您解决。

my sample code is 我的示例代码是

class EncryptedNode {

public EncryptedNode left, right ;
public char letter ;


// EncryptedNode Class constructor method.
public EncryptedNode (String message) {

    // get String length
    int message_length = message.length() ;

    if (message_length == 1) {
        this.letter = message.charAt(0) ;
    }else if (message_length == 2) {
        this.letter = message.charAt(0) ;
        this.right = new EncryptedNode (message.substring(1));
    } else {
        this.letter = message.charAt(0) ;

        // get the middle index of the message string.
        int middle_index = message.substring(1).length() /2 ;

        // get left and right strings
        String rightStr = message.substring(1,middle_index+1);
        String leftStr = message.substring(middle_index+1);

        this.left = new EncryptedNode (leftStr);
        this.right = new EncryptedNode (rightStr);
    }

    System.out.println (this.letter);
    // System.out.println (this.right);

}


public static void main (String [] args) {

    EncryptedNode en = new EncryptedNode("ABCDEF") ;

    en.decrypt();
}

public String decrypt () {
    if (this.left == null && this.right==null) {
        return this.letter;
    }else if (this.left == null && this.right != null) {
        return this.letter + this.right; 
    }else if (this.left !=null && this.right != null) {
        return this.left + this.letter + this.right;
    }
}

} }

I'm not going to write the code for you, since this is a classroom assignment, but I can explain how you should go about solving it. 因为这是课堂作业,所以我不会为您编写代码,但是我可以解释您应该如何解决它。

You want to work backward through the encryption method in order to decrypt it. 您想向后遍历加密方法以便对其进行解密。 Right now, you're just returning "st" no matter what the encrypted string is. 现在,无论加密的字符串是什么,您都只是返回“ st”。

I misread the code before. 我之前看错了代码。 This is a recursive method, so you have to start your way from the bottom and work your way up. 这是一种递归方法,因此您必须从头开始并逐步进行。 You're going to have to decrypt the left side and then decrypt the right side and add them together. 您将必须解密左侧,然后解密右侧,并将它们添加在一起。

In the decrypt method, you have to recursively call this.left.decrypt(); 在解密方法中,您必须递归调用this.left.decrypt(); and this.right.decrypt(); this.right.decrypt(); until you get to the base case. 直到您了解基本情况为止。 The base case is when the left and the right are equal to null. 基本情况是左和右等于null时。 At that point you just return the letter. 此时,您只需退回信件即可。 This is the bottom of the recursion. 这是递归的底部。

So to write your decrypt method, you want to return this.left.decrypt() + this.letter + this.right.decrypt(); 因此,要编写您的解密方法,您想return this.left.decrypt() + this.letter + this.right.decrypt(); unless this.left or this.right are null. 除非this.left或this.right为null。

If this.right is null, you'll return this.letter; 如果this.right为null,则return this.letter;

If this.left is null, you'll return this.letter + this.right.decrypt(); 如果this.left为null,则return this.letter + this.right.decrypt();

Sorry for the initial confusion, I thought this was an easier assignment than it really was. 对最初的困惑感到抱歉,我认为这是一个比实际要容易的任务。

Hopefully I helped this time! 希望这次我有所帮助!

Solution: 解:

public String decrypt () {

    if (this.right == null) {
       return this.letter;
    }
    if (this.left == null) {
        return this.letter + this.right.decrypt();
    }
    return this.left.decrypt() + this.letter + this.right.decrypt();
}

You are very close to a valid solution. 您非常接近有效的解决方案。 Keep in mind that according to your specs you store the text as letter + right + left and not ( left + letter + right ). 请记住,根据您的规范,您将文本存储为letter + right + left而不是( left + letter + right )。

I am posting a solution, take a close look to understand how it works. 我正在发布解决方案,请仔细查看以了解其工作原理。 If you are allowed to use StringBuilder (or the thread-safe StringBuffer) use those when you need to change the value of a String (I am putting the StringBuilder concatenation version in comments). 如果允许使用StringBuilder(或线程安全的StringBuffer),则在需要更改String的值时使用它们 (我将StringBuilder串联版本放在注释中)。

public static void main (String [] args) {

    EncryptedNode en = new EncryptedNode("ABCDEF") ;

    System.out.println(en.decrypt());
}

public String decrypt () {

    //if you are not allowed to use StringBuilder
    String s = String.valueOf(this.letter);
    if (this.right != null) {
        s = s + this.right.decrypt();
    }
    if (this.left != null) {
        s = s + this.left.decrypt();
    }
    return s;

    /*
    StringBuilder sb = new StringBuilder();
    sb.append(this.letter);
    if (this.right != null)
        sb.append(this.right.decrypt());
    if (this.left != null)
        sb.append(this.left.decrypt());
    return sb.toString();
    */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM