Cosminexus V9 アプリケーションサーバ Cosminexus XML Security - Core ユーザーズガイド
この項では,鍵データを復号化するアプリケーションの開発の流れを,コーディング例を示して説明します。
JAXPのDocumentBuilderを使用して暗号化された鍵データを含むXML文書を読み込み,Documentオブジェクトを取得します。Documentオブジェクトを取得するときに,名前空間が有効になるように設定してください。コーディングの例を次に示します。
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(input);
DocumentオブジェクトからEncryptedKey要素を取得し,EncryptedKeyオブジェクトを生成します。Document要素がEncryptedKey要素の場合のコーディングの例を次に示します。
Element encelem = doc.getDocumentElement(); if (!(DOMUtils.matchesName(encelem, XMLEncryption.XMLNS, "EncryptedKey"))) { throw new Exception("missing xenc:EncryptedKey");
XMLEncryptionFactoryクラスを使用して,EncryptedKey要素以下の構文に対応するオブジェクトを生成します。なお,オブジェクトを生成する前に,復号化に必要なコンテキストを設定しておく必要があります。コンテキストの処理モードは,「復号化」を指定してください。
オブジェクトを生成したら,復号化に必要な暗号アルゴリズムを指定します。このとき,復号化に使用する鍵を取得し,コンテキストに設定します。
コーディングの例を次に示します。
XMLEncryptionFactory xef = XMLEncryptionFactory.newInstance(); XMLSecurityContext context = new XMLSecurityContext( XMLSecurityContext.Mode.DECRYPT, doc); EncryptedKey ek = xef.newEncryptedKey(context, encelem); XMLEncryption xenc = xef.newXMLEncryption(ek.getEncryptionMethod(), ek.getKeyInfo()); context.setKeyResolver( new AdhocKeyResolver(Utilities.getSecretKey()));
XMLEncryptionクラスのdecryptメソッドを使用して,暗号化した鍵データを復号化します。コーディングの例を次に示します。
DataContainer decrypted = xenc.decrypt(context, ek);
「(4) 鍵データの復号化」で生成したDataContainerオブジェクトから復号化結果をバイト配列として取得し,鍵オブジェクトを生成します。コーディングの例を次に示します。
byte[] bytes = decrypted.getAsByteArray(); SecretKey wk = new SecretKeySpec(bytes, "DESede"); System.out.println("wrapped key=" + Utilities.toHexString( wk.getEncoded()));
All Rights Reserved. Copyright (C) 2012, 2015, Hitachi, Ltd.