解鎖圖案-九宮格有多少種組合?安全嗎?用程序來解答

如圖所示的九宮格密碼有多少種組合呢?這麼密碼是否是比數字密碼更安全呢?下面用數字來講話。java

 

一般設置密碼至少4個點,最多9個點,規則一般是兩點之間有一點,必需要過中間這個點,好比從1開始,必需要通過2才能夠到3。1是能夠直接到6的,但一般這種設置比較少。安全

運行附錄的程序獲得以下的數據:
size: 4 count0: 144 count1: 96 count2:40
size: 5 count0: 600 count1: 336 count2:152
size: 6 count0: 2880 count1: 1344 count2:304
size: 7 count0: 15120 count1: 4272 count2:496
size: 8 count0: 80640 count1: 18432 count2:1024
size: 9 count0: 362880 count1: 32256 count2:784
sum:  count0: 462264 count1: 56736 count2:2800
use time: 453msapp

例如:4個點組合可能有144種可能,排除飛點(1直接到3)的狀況,剩下96種可能,若是不考慮跨點(1到6)的狀況,僅剩下40種可能。this

因此得出的結論是:4-9個點任意組合的九宮格有56736種組合,排除跨點的狀況,有2800種可能。
通常人設置在5-7個點,一般形狀並不複雜的可能性僅爲1000種左右,試出來的可能性仍是很大的,而使用4位數字密碼的組合則爲10000種左右。spa

 

源碼附錄:code

package string;

import java.security.InvalidParameterException;

public class HowMany {
    public static void main(String[] args) {
        String[] except1 = new String[]{
                "46", "64", "28", "82", "19", "91", "37", "73",
                "13", "31", "39", "93", "97", "79", "17", "71"};

        String[] except2 = new String[]{
                "16", "61", "18", "81", "34", "43", "38", "83",
                "72", "27", "76", "67", "92", "29", "94", "49"};

        long lastTime = System.currentTimeMillis();

        long sum0 = 0;
        long sum1 = 0;
        long sum2 = 0;
        for (int i = 4; i <= 9; i++) {
            long count0 = 0;
            long count1 = 0;
            long count2 = 0;
            for (int j = 1; j <= 10 - i; j++) {
                Combines num = new Combines(j, i);
                String str;
                boolean flag1 = false;
                boolean flag2 = false;

                do {
                    flag1 = false;
                    flag2 = false;
                    str = num.getOne();
                    for (String except : except1) {
                        if (str.contains(except)) {
                            flag1 = true;
                            break;
                        }
                    }
                    for (String except : except2) {
                        if (str.contains(except)) {
                            flag2 = true;
                            break;
                        }
                    }
                    
                    count0++;
                    if (!flag1) {
                        count1++;
                    }
                    if (!flag1 && !flag2) {
                        count2++;
                        //System.out.println(str);
                    }
                } while (num.moveNext());
            }
            sum0 += count0;
            sum1 += count1;
            sum2 += count2;
            System.out.println("size: " + i + " count0: " + count0 + " count1: " + count1 + " count2:" + count2);
        }

        System.out.println("sum: " + " count0: " + sum0 + " count1: " + sum1 + " count2:" + sum2);
        System.out.println("use time: " + (System.currentTimeMillis() - lastTime) + "ms");
    }
    
    private static class Combines {
        private int base;
        private int[] number;
        private int[] poll;
        private long amount;
        private long count;

        public Combines(int start, int size) {
            if (size <= 1) {
                throw new InvalidParameterException();
            }

            base = start;
            number = new int[size];
            poll = new int[size];
            amount = this.jieCheng(size);

            this.reset();
        }

        public void reset() {
            initNumber();
            initPoll();
            count = 0;
        }

        public String getOne() {
            StringBuffer strBuf = new StringBuffer();

            for (int i = 0; i < poll.length; i++) {
                strBuf.append(getNumber(poll[i]));
            }

            return strBuf.toString();
        }

        public boolean moveNext() {
            if (++count < amount) {
                this.stepNext(1);
                this.initNumber();
                return true;
            } else {
                return false;
            }
        }

        private long jieCheng(int x) {
            long y = 1;

            for (int i = 1; i <= x; i++) {
                y = y * i;
            }

            return y;
        }

        private int getNumber(int index) {
            int num;
            for (int i = 0; i < number.length; i++) {
                if (number[i] != 0) {
                    if (index == 0) {
                        num = number[i];
                        number[i] = 0;
                        return num;
                    }
                    index--;
                }
            }

            return 0;
        }

        private void stepNext(int index) {
            if (poll[poll.length - 1 - index] == index) {
                this.stepNext(index + 1);
                poll[poll.length - 1 - index] = 0;
            } else {
                poll[poll.length - 1 - index]++;
                return;
            }
        }

        private void initNumber() {
            for (int i = 0; i < number.length; i++) {
                number[i] = i + base;
            }
        }

        private void initPoll() {
            for (int i = 0; i < poll.length; i++) {
                poll[i] = 0;
            }
        }
    }
}
相關文章
相關標籤/搜索