4

我必须实现一个 Java 方法,该方法将确定输入字符串是散列(由机器生成)还是纯文本(由人类编写)。

例子:

isThisEncrypted("qwertyuiopasdfghjklzxcvbnm"); // returns true
isThisEncrypted("some normal human text"); // returns false

我考虑过使用 Kolmogorov-Smirnov 测试 (jsc.goodnessfit.KolmogorovTest),它将检查字符串中的字符是否来自正态分布,但我了解到,仅检查一个短字符串可能不是决定性的。

你知道如何在 Java 中解决这个问题(最好使用现有的库)吗?

4

3 回答 3

3

从您的评论中:

人工输入可以是随机的

此方法必须确定字符串是否来自此方法或表单用户

那么只有字符串就无法解决您的问题。你需要额外的信息。

如果您期望 Blowfish 以给定格式返回字符串,那么您就错了。现代加密算法的目标是高熵,这意味着它们必须看起来和感觉都是随机的。

于 2013-05-08T11:08:17.067 回答
0

您已经说过您只需要一个近似解决方案(80% 的准确度),可能是 AClassName 形式的类(注意大写),并且给定的加密文本样本中没有大写字母。所以

public class Test{

    public static void main(String args[]){
        String[] tests=new String[5];

        tests[0]="MyClass";
        tests[1]="Short";
        tests[2]="thsrjtyzfgnmytkzrhjstk";
        tests[3]="tatm";
        tests[4]="The result is good";

        for(int i=0;i<tests.length;i++){
            System.out.println(tests[i]+ "- Encrypted:" + isProbablyEncrypted(tests[i]));
        }


    }

    public static boolean isProbablyEncrypted(String in){
        int noOfWords= countOccurrences(in, ' ') + countCaps(in);
        if (noOfWords==0){
            return true;
        }else{
            double averageWordLength=(double)(in.length())/(noOfWords+1);

            if (averageWordLength>15){
                return true;
            }else{
                return false;
            }
        }
    }

    public static int countOccurrences(String haystack, char needle)
    {
        int count = 0;
        for (int i=0; i < haystack.length(); i++)
        {
            if (haystack.charAt(i) == needle)
            {
                 count++;
            }
        }
        return count;
    }

    public static int countCaps(String in){
        int caps=0;
        for (int i=0; i<in.length(); i++) {
            if (Character.isUpperCase(in.charAt(i)))caps++;
        }
        return caps;
    }
}

这是一个好的解决方案吗?不,它是否提供> 80%的准确度?是的

于 2013-05-08T12:04:43.237 回答
0

您将输入拆分为单词并对照字典检查它们(检查字典中的单词)。

从现在开始,一切都取决于您的实施。IMO,如果一半的单词与字典匹配,那么你的方法应该返回 false。

于 2013-05-08T10:56:36.393 回答