加密的Andr​​oid [英] Encryption Android

查看:100
本文介绍了加密的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解一个code,列出了所有支持的Andr​​oid设备上的加密算法。
我只是想知道为什么有必要从服务名称添加与('Alg.Alias​​')的步骤,并删除这些字符?该应用程序停止工作,没有它,我不明白为什么!

包com.example.lab_enc_dec;

 进口java.security.Provider中;
进口java.security.Security;
进口java.util.Iterator的;
进口java.util.Set中;
进口java.util.TreeSet中;进口com.example.lab_enc_dec.R;进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;公共类ListCryptoAlgorithms延伸活动{
    静态最后弦乐TAG =ListCryptoAlgorithms;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.algorithm_list);        ListSupportedAlgorithms();
    }    公共无效ListSupportedAlgorithms(){
        字符串结果=;        //得到所有的供应商
        提供商[]提供商= Security.getProviders();        为(中间体p值= 0; P&下; providers.length,P ++){
            //得到所有服务类型为特定的供应商
            SET<对象> KS =提供商[P] .keySet();
            SET<串GT; servicetypes =新TreeSet的<串GT;();
            对于(迭代器<对象>它= ks.iterator(); it.hasNext();){
                串K = it.next()的toString()。
                K = k.split()[0];
                如果(k.startsWith(Alg.Alias​​。))
                    K = k.substring(10);                servicetypes.add(k.substring(0,k.indexOf())'。');
            }            //取得所有算法为特定的服务类型
            INT S = 1;
            对于(迭代器<串GT;其= servicetypes.iterator(); its.hasNext();){
                串STYPE = its.next();
                SET<串GT;算法=新TreeSet的<串GT;();
                对于(迭代器<对象>它= ks.iterator(); it.hasNext();){
                    串K = it.next()的toString()。
                    K = k.split()[0];
                    如果(k.startsWith(STYPE +。))
                        algorithms.add(k.substring(stype.length()+ 1));
                    否则如果(k.startsWith(Alg.Alias​​。+ STYPE +。))
                        algorithms.add(k.substring(stype.length()+ 11));
                }                INT A = 1;
                对于(迭代器<串GT; ITA = algorithms.iterator(); ita.hasNext();){
                    结果+ =([P#+(P + 1)+:+提供商[P] .getName()+]+
                            [S#S +:+ STYPE +]+
                            [A#+ A +:+ ita.next()+] \\ n);
                    一个++;
                }                小号++;
            }
        }        TextView的电视=(的TextView)findViewById(R.id.supp_alg_result);
        tv.setText(结果);
    }}


解决方案

别名是别名已根据不同的名称注册现有的算法。这些名字与 pfixed $ P $Alg.Alias​​。在列表中,使他们能够从初始登记进行区分。但是,你不应该在不同的JCE类的的getInstance 的方法,如<$使用Alg.Alias​​ C $ C> Cihper 。因此列表中删除Alg.Alias​​。从服务名称。

I'm trying to understand a code that lists all supported encryption algorithms on an Android device. I'm just wondering why is it necessary to add the step with ('Alg.Alias') and remove these characters from the service name ? The application stops working without it and I don't understand why !

package com.example.lab_enc_dec;

import java.security.Provider;
import java.security.Security;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import com.example.lab_enc_dec.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ListCryptoAlgorithms extends Activity {
    static final String TAG = "ListCryptoAlgorithms";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.algorithm_list);

        ListSupportedAlgorithms();
    }

    public void ListSupportedAlgorithms() {
        String result = "";

        // get all the providers
        Provider[] providers = Security.getProviders();

        for (int p = 0; p < providers.length; p++) {
            // get all service types for a specific provider
            Set<Object> ks = providers[p].keySet();
            Set<String> servicetypes = new TreeSet<String>();
            for (Iterator<Object> it = ks.iterator(); it.hasNext();) {
                String k = it.next().toString();
                k = k.split(" ")[0];
                if (k.startsWith("Alg.Alias."))
                    k = k.substring(10);                

                servicetypes.add(k.substring(0, k.indexOf('.')));
            }

            // get all algorithms for a specific service type
            int s = 1;
            for (Iterator<String> its = servicetypes.iterator(); its.hasNext();) {
                String stype = its.next();
                Set<String> algorithms = new TreeSet<String>();
                for (Iterator<Object> it = ks.iterator(); it.hasNext();) {
                    String k = it.next().toString();
                    k = k.split(" ")[0];
                    if (k.startsWith(stype + "."))
                        algorithms.add(k.substring(stype.length() + 1));
                    else if (k.startsWith("Alg.Alias." + stype +".")) 
                        algorithms.add(k.substring(stype.length() + 11));
                }

                int a = 1;
                for (Iterator<String> ita = algorithms.iterator(); ita.hasNext();) {
                    result += ("[P#" + (p + 1) + ":" + providers[p].getName() + "]" +
                            "[S#" + s + ":" + stype + "]" +
                            "[A#" + a + ":" + ita.next() + "]\n");
                    a++;
                }

                s++;
            }
        }

        TextView tv = (TextView)findViewById(R.id.supp_alg_result);
        tv.setText(result);
    }

}

解决方案

Aliases are aliases to existing algorithms that have been registered under a different name. Those names are prefixed with "Alg.Alias." in the list so they can be distinguished from the initial registration. However, you should not use "Alg.Alias" in the getInstance methods of the various JCE classes such as Cihper. Hence the list removes the "Alg.Alias." from the service name.

这篇关于加密的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆