Google Tink加密库-找不到KeyGenerator AES实现

时间:2018-10-03 20:17:45

标签: android tink

我正在运行Android KitKat的平板电脑设备以及Lollipop上的Samsung Tab-A上遇到此问题。在运行Android M的Acer平板电脑上,效果很好。

这是失败点:

private KeysetHandle getOrGenerateNewKeysetHandle() throws IOException, GeneralSecurityException {
        return new AndroidKeysetManager.Builder()
                .withSharedPref(getApplicationContext(), TINK_KEYSET_NAME, TINK_PREF_FILE_NAME)
                .withKeyTemplate(AeadKeyTemplates.AES256_GCM) // Failure point
                .withMasterKeyUri(ANDROID_KEYSTORE_TINK_MASTER_KEY_URI)
                .build()
                .getKeysetHandle();
    }

然后在初始化Tink时初始化AEAD:

// google\Tink crypto
try {
    TinkConfig.register();
    aead = AeadFactory.getPrimitive(getOrGenerateNewKeysetHandle());
    Log.i(LOG_TAG, "Tink registered.");
} catch (GeneralSecurityException | IOException e) {
    e.printStackTrace();
    Log.e(LOG_TAG, "Tink failed to register or could not generate a keyset handle.");
    Log.e(LOG_TAG, "Tink failed to register: " + e.getMessage());
}

无论哪种方式,Tink都无法使用以下捕获的消息进行初始化:

10-03 16:10:58.319 com.mycompany.myapp.debug E/MainActivity: Tink failed to register: KeyGenerator AES implementation not found

是否有特定的KeyTemplate可以替换以下内容?

AeadKeyTemplates.AES256_GCM

完整的堆栈跟踪:

10-03 17:25:45.235 com.mycompany.myapp.debug W/System.err: java.security.NoSuchAlgorithmException: KeyGenerator AES implementation not found
10-03 17:25:45.245 com.mycompany.myapp.debug W/System.err:     at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
10-03 17:25:45.255 com.mycompany.myapp.debug W/System.err:     at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:170)
10-03 17:25:45.255 com.mycompany.myapp.debug W/System.err:     at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:163)
10-03 17:25:45.255 com.mycompany.myapp.debug W/System.err:     at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:135)
10-03 17:25:45.255 com.mycompany.myapp.debug W/System.err:     at com.google.crypto.tink.integration.android.AndroidKeystoreKmsClient.generateNewAeadKey(AndroidKeystoreKmsClient.java:141)
10-03 17:25:45.265 com.mycompany.myapp.debug W/System.err:     at com.google.crypto.tink.integration.android.AndroidKeystoreKmsClient.getOrGenerateNewAeadKey(AndroidKeystoreKmsClient.java:128)
10-03 17:25:45.265 com.mycompany.myapp.debug W/System.err:     at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.withMasterKeyUri(AndroidKeysetManager.java:157)
10-03 17:25:45.265 com.mycompany.myapp.debug W/System.err:     at com.mycompany.myapp.MainActivity.getOrGenerateNewKeysetHandle(MainActivity.java:2520)
10-03 17:25:45.265 com.mycompany.myapp.debug W/System.err:     at com.mycompany.myapp.MainActivity.initializeRequiredAppComponents(MainActivity.java:2554)
10-03 17:25:45.265 com.mycompany.myapp.debug W/System.err:     at com.mycompany.myapp.MainActivity.onCreate(MainActivity.java:230)
10-03 17:25:45.275 com.mycompany.myapp.debug W/System.err:     at android.app.Activity.performCreate(Activity.java:5231)
10-03 17:25:45.275 com.mycompany.myapp.debug W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-03 17:25:45.275 com.mycompany.myapp.debug W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-03 17:25:45.275 com.mycompany.myapp.debug W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-03 17:25:45.275 com.mycompany.myapp.debug W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-03 17:25:45.285 com.mycompany.myapp.debug W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-03 17:25:45.285 com.mycompany.myapp.debug W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-03 17:25:45.285 com.mycompany.myapp.debug W/System.err:     at android.os.Looper.loop(Looper.java:136)
10-03 17:25:45.285 com.mycompany.myapp.debug W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-03 17:25:45.285 com.mycompany.myapp.debug W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 17:25:45.295 com.mycompany.myapp.debug W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
10-03 17:25:45.295 com.mycompany.myapp.debug W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-03 17:25:45.295 com.mycompany.myapp.debug W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-03 17:25:45.295 com.mycompany.myapp.debug W/System.err:     at dalvik.system.NativeStart.main(Native Method)
10-03 17:25:45.295 com.mycompany.myapp.debug E/MainActivity: GeneralSecurityException - Tink failed to register or could not generate a keyset handle: KeyGenerator AES implementation not found
10-03 17:25:45.295 com.mycompany.myapp.debug E/MainActivity: isMDMAPISupported: Exception ignored

1 个答案:

答案 0 :(得分:0)

当在Tink v1.2.0上遇到问题时,以下作为临时解决方法。对于23以下的Android SDK,我们在构建keysetManager时跳过使用密钥库。

但是,这应该不再是Tink v1.2.1(发行时)的问题,因为我已经测试了最新的HEAD-SNAPSHOT并且没有遇到崩溃。

var outerGridApi = null;
            this.gridOptions = {
                showHeader: true,
                showColumnFooter: true,
                useExternalSorting: false,
                exporterFieldCallback: storesalesPdfFormatter.exporterFieldCallback,
                multiSelect: true,
                enableRowSelection: true,
                enableRowHeaderSelection: false,
                exporterPdfCustomFormatter: function (docDefinition) {
                    return exportGridFactory.exportTitle(docDefinition, title,
                        this.filterOptions.dateRange.startDate, this.filterOptions.dateRange.endDate);
                }.bind(this)
            };

            this.gridOptions.deferredRegisterApi = $q.defer();
            this.gridOptions.deferredRegisterApi.promise.then(function (gridApi) {
                outerGridApi = gridApi;
            });

    var setSumGridOptions = function () {
                    this.gridOptions.useExternalPagination = true;
                    this.gridOptions.enableSorting = true;
                    this.gridOptions.treeRowHeaderAlwaysVisible = true;
                    this.gridOptions.columnDefs = storesalesGridColumns.salesSumGridCols;
                }.bind(this);

                var setMainGridOptions = function (cols) {
                    this.gridOptions = {};
                    this.gridOptions.columnDefs = cols;
                    this.gridOptions.showColumnFooter = true;
                    this.gridOptions.onRegisterApi = function (gridApi) {
                        this.outerGridApi = gridApi;
                    }.
                    bind(this);

                }.bind(this);

    var refreshUi = function (reportType) {
                    switch (reportType) {
                        case this.reportType.salesAnalysis:
                            {
                                setMainGridOptions(storesalesGridColumns.salesAnalCols);

                                storeSalesService.getData(this.filterOptions, true).then(function (reportData) {
                                    var treeRoots = reportData.tableData.filter(function (x) { return x.$$treeLevel !== undefined }).length;
                                    if (treeRoots === 1) {
                                        this.showTreeViewGrid = false;
                                    }

                                    if (reportData.tableData[0].Name === 'Unnamed' && (treeRoots === 1)) {
                                        reportData.tableData.shift();
                                    }
                                    this.gridOptions.data = reportData.tableData;

                                    storesalesGridColumns.salesAnalCols[1].salesServerAggregation = reportData.aggregations.salesAggregation;
                                    storesalesGridColumns.salesAnalCols[2].lastYearSalesServerAggregation = reportData.aggregations.lastYearSalesAggregation;
                                    storesalesGridColumns.salesAnalCols[3].percentServerAggregation = reportData.aggregations.percentAg;
                                    storesalesGridColumns.salesAnalCols[4].ItemsServerAggregation = reportData.aggregations.itemsAg;
                                    storesalesGridColumns.salesAnalCols[5].averagePriceServerAggregations = reportData.aggregations.avgPriceAg;

                                    if (treeRoots !== 1) {
                                        this.outerGridApi.treeBase.collapseAllRows();
                                    }

                                    //this.chartData = chartsDataParser.getSalesAnalysisChartData(this.gridOptions.data);
                                    //handleChartResize();
                                }.bind(this));
                                title = 'Sales Analysis Report';
                                break;
                            }
                        case this.reportType.monthlySummary:
                            {
                                setMainGridOptions(storesalesGridColumns.monthSumCols);
                                this.gridOptions.data = monthlyData;
                                this.chartData = chartsDataParser.getMonthlySummaryChartData(this.gridOptions.data);
                                title = 'Monthly Summary Report';
                                handleChartResize();
                                break;
                            }
                        case this.reportType.salesSummary:
                            {
                                setSumGridOptions();
                                refreshSalesSummary(true);
                                title = 'Sales Summary Report';
                                break;
                            }
                        default: break;


             };