【问题标题】:Java reverse encodingJava反向编码
【发布时间】:2013-05-09 18:23:51
【问题描述】:

所以我正在制作一个程序,您可以在其中使用关键字对消息进行编码或解码。我有编码部分,但我不确定如何解码消息。我是 java 新手,所以任何帮助将不胜感激!

到目前为止,这是我的代码:

            import java.util.Scanner;

            class Coder
            {
              public static void main(String[] arg)
             {
                boolean encode = false;
                boolean decode = false;
                int multi = 1;
                Scanner inputdata=new Scanner(System.in);
                System.out.print("Type E to encode or D to decode:\n");
                String opt=inputdata.nextLine();
                if (opt.equals("e")||opt.equals("E"))
                {
                    System.out.print("Type the keyword to use for encoding:\n");
                    encode = true;
                }
                else if (opt.equals("d")||opt.equals("D"))
                {
                    System.out.print("Type the keyword for decoding:\n");
                    decode = true;
                }
                else
                {
                    System.out.print("Error:This is not an option");
                }


                if (encode==true)
                {
                    String keyword=inputdata.nextLine();
                    int[] key = new int[1000];
                    for (int k = 0; k < key.length; ++k)
                    {
                        char c = keyword.charAt(k % keyword.length());
                        if (c >= 'a' && c <= 'z')
                        {
                            key[k] = c-'a';
                        }
                    }
                    System.out.print("Type encode message:\n");
                    String message=inputdata.nextLine();
                    for (int i = 0; i < message.length(); ++i)
                    {

                        if (message.charAt(i) >= 'a' && message.charAt(i) <= 'z')
                        {
                            System.out.println((int)message.charAt(i) - (int)'a' + key[i]);
                        }
                        else if (message.charAt(i) >= 'A' && message.charAt(i) <= 'Z')
                        {
                            System.out.println((int)message.charAt(i) - (int)'A' + key[i]);
                        }
                        else if (message.charAt(i) == ' ')
                        {
                            System.out.println(" ");
                        }
                        else
                        {
                            System.out.println(message.charAt(i));
                        }
                    }
                }


                if (decode==true)
                {

                }
                }
            }

【问题讨论】:

  • 好的。然后,您输入了一个字符串作为编码键和一个要编码的字符串。您编码并生成一堆数字,每行打印一个。你如何输入要解码的字符串?一堆数字?
  • 您从哪里获得代码的编码部分的代码/算法?
  • @HovercraftFullOfEels 非常接近于“Captain Midnight Decoder Ring”,在您浏览消息时可按顺序使用可变数量的环。所以 key = def 和 msg=abc 产生 dfh。但它不会在字母表的末尾滚动。
  • @LeeMeador:是的,我以前见过这种事情,但我认为他没有编造这种算法。如果他得到了编码算法,他肯定得到了解码算法。它与 ROT 13 一样安全,但这不是问题。
  • 您可以查看MessageDigest,使用 SHA 算法进行解密。

标签: java loops decode encode


【解决方案1】:

好吧,我不确定您要编写哪种加密/解密算法,但它看起来像是某种 ROT shuffle。我最好的猜测是李的回答,但老实说,我并没有真正消化你的代码。使用 main 函数和 if 条件中的所有内容很难阅读。

我猜您是 Java 新手,但这里有一些有用的实践和代码设计技巧,可以让生活更轻松。

JUnit 是你的朋友:

在 main 方法中运行和调试代码是一个非常糟糕的主意。每次要检查代码时,您都必须手动运行每个场景并检查程序的当前状态。您可以编写为您完成所有工作的测试程序,此外,当您需要检查特定条件时,它可以更轻松地跳转到调试器。

JUnit 是内置于 Java 中的,但还有其他库(例如 Hamcrest 和 Mockito)使其更易于使用。

例如这都需要一个基本的测试:

public class EncryptionTest {
    @Test // Test annotation marks that this method should be evaluated
    public testEncoding() {
          final String orginalString = "fooBar";
          final String expectedEncryptionResults = "barFoo"; // You should get the point.

          final String encryptedString = Encoder.encode(oringalString) 

// Or however you want to structure the encoder object.

          assertTrue( encryptedString.equals( expectedEncryptionResults ) );
          // If false, the test will fail.                                                               
    }
} 

设计模式

要牢记的一个好的经验法则是Wikipedia: Law Of Demeter。简而言之,这条规则表明你的方法或对象应该知道的比必要的多。

实用程序

实用程序模式是一种易于使用的设计模式。

public class Encoder {
    private Encoder() {} // Private constructor

    public static encode( final String message ) {
        String encodedMessage = null
        // Complex encryption magic goes here
        return encodedMessage;
    }
}

由于此模式有一个私有构造函数,您不能实例化 Encoder 对象,只能使用其中定义的静态方法。这是重用通用逻辑或检查的好方法,Apache Utitls 类是实用程序模式的一个很好的例子。

所以在我的 Junit 演示中,我也使用了这个 Encoder 类。

final String encryptedString = Encoder.encode(oringalString) 

工厂

工厂是另一种常见且易于使用的设计模式。工厂是一种简单的对象,在生活中有一个目标,就是制造其他对象。

public class EncryptionFactory {
     public String build( String message ) {
         // Complex magic goes here.
         return encryptedString;
     }
}

public class EncryptionFactoryTest {

    final EncryptionFactory factory = new EncryptionFactory();
     
     @Test
     public void testEncryption() {
            final String = originalMessage = "fooBar";
            
            final String encryptedMessage = factory.build(originalMessage);

            assertTrue( encryptedMessage.equals("barFoo") );
     }
}

使用设计模式的主要好处是它将诸如加密算法之类的实现逻辑推送到更小的代码段中,从而更容易重用和发现任何错误。此外,它还减少了开发和维护时间^_^

【讨论】:

    【解决方案2】:

    我在这里猜测,但我怀疑您想使用与您的编码类似的代码进行解码,但请更改此代码:

    (int)message.charAt(i) - (int)'a' + key[i]
    

    到这里:

    (int)message.charAt(i) + (int)'a' - key[i]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 2015-10-31
      • 2021-11-25
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多