获取每个提供商和服务的所有密码算法

时间:2019-11-14 23:30:53

标签: android security kotlin cryptography

我的名字叫Panagiotis,我想与您分享我遇到的问题的解决方案。

我的问题:

最近,我为我的应用程序创建了一个android库,该库已实现了有关加密的一些标准功能。我的主要问题是我可以使用哪种类型的转换来加密和解密数据。我的第一步是在互联网上寻找信息,但没有找到适合我的东西。我想要一个包含所有正确转换名称的列表。

下面您将看到我的解决方案!

Panagiotis Vangelatos

1 个答案:

答案 0 :(得分:1)

我在Kotlin的解决方案:

首先,我创建了“ AlgorithmsPerProviderAndService”类,其中包含服务名称和每个提供程序的所有可用算法。

private class AlgorithmsPerProviderAndService constructor(private val serviceName: String)
{
    private val ServiceName:  String      get() = serviceName
    val ProviderName: HashMap<String, ArrayList<String>> = HashMap()
}

我创建了下面的代码部分,以获取android使用的每个服务和提供程序的所有可用算法。

    // StringBuilder object to display the data
    val sb  = StringBuilder()

    // "Info" HashMap that will has as key the name of the service and as value my custom class
    // that will hold the algorithm's names per provider
    val info: HashMap<String, AlgorithmsPerProviderAndService> = HashMap()

    // Part of code that reads all the available providers and for each provider get the 
    // corresponding services and the algorithm's names
    Security.getProviders().toList().sortedBy { it.name }.forEach {
        it.services.sortedBy { it1 -> it1.type }.forEach {it1 ->
            // Check if the "info" HashMap is empty and if is empty then add the first service
            // and the corresponding FIRST provider and FIRST algorithm
            if (info.size == 0)
            {
                val currentData = AlgorithmsPerProviderAndService(it1.type)
                currentData.ProviderName.put(it.name, arrayListOf(it1.algorithm))
                info.put(it1.type, currentData)
            }
            // If the "info" HashMap is not empty, then check if the current service already exists.
            // The reason that I check if the current services exists is because I want to have 
            // each service only one time into the "info" HashMap
            else
            {
                // Because the current service may have been implemented by more than one provider
                // and because I want to have the all algorithms that have been implemented by all 
                // provides, in case of the current service already exists, I check if the current
                // provider exists or not.
                if (info.containsKey(it1.type))
                {
                    // If the provider exists, I want to add only the available algorithms for the 
                    // current provider 
                    if (info[it1.type]!!.ProviderName.containsKey(it.name))
                    {
                        info[it1.type]!!.ProviderName[it.name]!!.add(it1.algorithm)
                    }
                    // If the provider does not exist, then I want to add both the new provider and 
                    // only the FIRST algorithm (because the other algorithms will be add when the 
                    // above condition is true)
                    else
                    {
                        info[it1.type]!!.ProviderName.put(it.name, arrayListOf(it1.algorithm))
                    }
                }
                // If this case, the current service does not exists, so adding it and in addition
                // I am adding provider (of the current service) and only the first algorithm.
                else
                {
                    val currentData = AlgorithmsPerProviderAndService(it1.type)
                    currentData.ProviderName.put(it.name, arrayListOf(it1.algorithm))
                    info.put(it1.type, currentData)
                }
            }
        }
    }

    // Display data
    info.toSortedMap().forEach {
        sb.appendln("Service: " + it.key)

        it.value.ProviderName.toSortedMap().forEach {it1 ->
            sb.appendln("\tProvider: " + it1.key)

            it1.value.sort()
            it1.value.forEach {it2 ->
                sb.appendln("\t\tAlgorithm: $it2")
            }

            sb.appendln()
        }

        sb.appendln()
    }

我希望写出可以理解的评论!如果有人想讨论代码,那将是我的荣幸,我正在等待您的评论和优化!

Vangelatos Panagiotis