0
fun main () {
    var integers = mutableListOf(0)
    for (x in 1..9) {
        integers.add(x)
    }
    //for or while could be used in this instance
    var lowerCase = listOf("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")
    var upperCase = listOf('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')
    println(integers)
    println(lowerCase)
    println(upperCase)
    //Note that for the actual program, it is also vital that I use potential punctuation
    val passwordGeneratorKey1 = Math.random()*999
    val passwordGeneratorKey2 = passwordGeneratorKey1.toInt()
    var passwordGeneratorL1 = lowerCase[(Math.random()*lowerCase.size).toInt()]
    var passwordGeneratorL2 = lowerCase[(Math.random()*lowerCase.size).toInt()]
    var passwordGeneratorL3 = lowerCase[(Math.random()*lowerCase.size).toInt()]
    var passwordGeneratorU1 = upperCase[(Math.random()*upperCase.size).toInt()]
    var passwordGeneratorU2 = upperCase[(Math.random()*upperCase.size).toInt()]
    var passwordGeneratorU3 = upperCase[(Math.random()*upperCase.size).toInt()]
    val password = passwordGeneratorKey2.toString()+passwordGeneratorL1+passwordGeneratorL2+passwordGeneratorL3+passwordGeneratorU1+passwordGeneratorU2+passwordGeneratorU3
    println(password)
    //No, this isn't random, but it's pretty close to it
    //How do I now run through every possible combination of the lists //lowerCase, integers, and upperCase?
}

如何遍历所有可能的排列以最终解决随机生成的密码?这是在 Kotlin 中。

4

2 回答 2

0

我认为您应该将所有列表附加在一起,然后通过随机索引从中提取,这样您可以确保数字、小写和大写的位置也是随机的。此外,您不需要编写所有字符,您可以使用Range它为您生成它们。

    fun main() {
        val allChars = mutableListOf<Any>().apply {
            addAll(0..9) // creates range from 0 to 9 and adds it to a list
            addAll('a'..'z') // creates range from a to z and adds it to a list
            addAll('A'..'Z') // creates range from A to Z and adds it to a list
        }
        val passwordLength = 9
        val password = StringBuilder().apply {
            for (i in 0 until passwordLength) {
                val randomCharIndex =
                    Random.nextInt(allChars.lastIndex) // generate random index from 0 to lastIndex of list
                val randomChar = allChars[randomCharIndex] // select character from list
                append(randomChar) // append char to password string builder 
            }
        }.toString()
    
        println(password)
    }

使用列表方法可以实现更短的解决方案

 fun main() {
    val password = mutableListOf<Any>()
        .apply {
            addAll(0..9) // creates range from 0 to 9 and adds it to a list
            addAll('a'..'z') // creates range from a to z and adds it to a list
            addAll('A'..'Z') // creates range from A to Z and adds it to a list
        }
        .shuffled() // shuffle the list
        .take(9) // take first 9 elements from list
        .joinToString("") // join them to string 
    println(password)
}
于 2021-09-13T20:25:53.820 回答
0

正如其他人指出的那样,以以下格式生成初始密码的方法不那么痛苦:1 到 3 位数字,后跟 3 个小写字符,然后是 3 个大写字符。

要暴力破解此密码,您需要"a..z"考虑"A..Z". 在这两种情况下,这种 3 排列的数量都是15600 = 26! / (26-3)!。在最坏的情况下,您将不得不检查1000 * 15600 * 15600组合,平均为一半。

使用下面的代码可能在几个小时内就可以实现:


import kotlin.random.Random
import kotlin.system.exitProcess


val lowercaseList = ('a'..'z').toList()
val uppercaseList = ('A'..'Z').toList()

val lowercase = lowercaseList.joinToString(separator = "")
val uppercase = uppercaseList.joinToString(separator = "")

fun genPassword(): String {
    val lowercase = lowercaseList.shuffled().take(3)
    val uppercase = uppercaseList.shuffled().take(3)

    return (listOf(Random.nextInt(0, 1000)) + lowercase + uppercase).joinToString(separator = "")
}


/**
 * Generate all K-sized permutations of str of length N. The number of such permutations is:
 * N! / (N-K)!
 *
 * For example: perm(2, "abc") =  [ab, ac, ba, bc, ca, cb]
 */
fun perm(k: Int, str: String): List<String> {

    val nk = str.length - k

    fun perm(str: String, accumulate: String): List<String> {
        return when (str.length == nk) {
            true -> listOf(accumulate)
            false -> {
                str.flatMapIndexed { i, c ->
                    perm(str.removeRange(i, i + 1), accumulate + c)
                }
            }
        }
    }
    return perm(str, "")
}

fun main() {
    val password = genPassword().also { println(it) }

    val all3LowercasePermutations = perm(3, lowercase).also { println(it) }.also { println(it.size) }
    val all3UppercasePermutations = perm(3, uppercase).also { println(it) }.also { println(it.size) }


    for (i in 0..999) {
        println("trying $i")
        for (l in all3LowercasePermutations) {
            for (u in all3UppercasePermutations) {
                if ("$i$l$u" == password) {
                    println("found: $i$l$u")
                    exitProcess(0)
                }
            }
        }
    }
}
于 2021-09-13T22:24:28.880 回答