首页 > 解决方案 > 获取每个提供商和服务的所有加密算法

问题描述

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

我的问题:

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

下面你会看到我的解决方案!

帕纳焦蒂斯·万杰拉托斯

标签: androidsecuritykotlincryptography

解决方案


我在 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()
    }

我希望写出可以理解的评论!如果有人想讨论代码,我会很高兴,我正在等待您的意见和优化!

万杰拉托斯·帕纳吉奥蒂斯


推荐阅读