← 返回首页
SubtleCrypto: exportKey() メソッド - Web API | MDN

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

SubtleCrypto: exportKey() メソッド

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年1月.

* Some parts of this feature may have varying levels of support.

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。

exportKey()SubtleCrypto インターフェイスのメソッドで、キーをエクスポートします。すなわち、これは CryptoKey オブジェクトを入力として取り、その鍵を外部のポータブルな形式で表します。

鍵をエクスポートするには、その鍵の CryptoKey.extractable が true に設定れている必要があります。

鍵はいくつかの書式でエクスポートすることができます。詳細は SubtleCrypto.importKey() ページの対応する形式を参照してください。

鍵は暗号化された形式ではエクスポートされません。鍵をエクスポートする際に暗号化するには、代わりに SubtleCrypto.wrapKey() API を使用してください。

In this article

構文

js
exportKey(format, key)

引数

format

キーをエクスポートするデータ形式を表す文字列値。以下のいずれかを指定します。

key

エクスポートする CryptoKey

返値

プロミス (Promise) です。

  • format が jwk であった場合、プロミスはキーを含む JSON オブジェクトで履行されます。
  • そうでなければ、キーを格納した ArrayBuffer でプロミスが履行されます。

例外

以下の例外が発生した場合、プロミスは拒否されます。

InvalidAccessError DOMException

抽出不可能なキーをエクスポートしようとしたときに発生します。

NotSupported DOMException

不明な形式でエクスポートしようとしたときに発生します。

TypeError

無効な書式を使用しようとしたときに派生します。

メモ: GitHub 上の動作例を試すことができます。

Raw エクスポート

この例では、AES 鍵を、鍵のバイトを格納した ArrayBuffer としてエクスポートします。完全なコードは GitHub で参照してください

js
/* Export the given key and write it into the "exported-key" space. */ async function exportCryptoKey(key) { const exported = await window.crypto.subtle.exportKey("raw", key); const exportedKeyBuffer = new Uint8Array(exported); const exportKeyOutput = document.querySelector(".exported-key"); exportKeyOutput.textContent = `[${exportedKeyBuffer}]`; } /* Generate an encrypt/decrypt secret key, then set up an event listener on the "Export" button. */ window.crypto.subtle .generateKey( { name: "AES-GCM", length: 256, }, true, ["encrypt", "decrypt"], ) .then((key) => { const exportButton = document.querySelector(".raw"); exportButton.addEventListener("click", () => { exportCryptoKey(key); }); });

PKCS #8 エクスポート

この例では、 RSA 秘密署名鍵を PKCS #8 オブジェクトとしてエクスポートします。エクスポートされた鍵は PEM エンコードされます。完全なコードは GitHub で参照してください

js
/* Convert an ArrayBuffer into a string from https://developer.chrome.com/blog/how-to-convert-arraybuffer-to-and-from-string/ */ function ab2str(buf) { return String.fromCharCode.apply(null, new Uint8Array(buf)); } /* Export the given key and write it into the "exported-key" space. */ async function exportCryptoKey(key) { const exported = await window.crypto.subtle.exportKey("pkcs8", key); const exportedAsString = ab2str(exported); const exportedAsBase64 = window.btoa(exportedAsString); const pemExported = `-----BEGIN PRIVATE KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`; const exportKeyOutput = document.querySelector(".exported-key"); exportKeyOutput.textContent = pemExported; } /* Generate a sign/verify key pair, then set up an event listener on the "Export" button. */ window.crypto.subtle .generateKey( { name: "RSA-PSS", // Consider using a 4096-bit key for systems that require long-term security modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256", }, true, ["sign", "verify"], ) .then((keyPair) => { const exportButton = document.querySelector(".pkcs8"); exportButton.addEventListener("click", () => { exportCryptoKey(keyPair.privateKey); }); });

SubjectPublicKeyInfo エクスポート

この例は、RSA 公開暗号化鍵を PEM エンコードされた SubjectPublicKeyInfo オブジェクトとしてエクスポートします。完全なコードは GitHub で参照してください.

js
/* Convert an ArrayBuffer into a string from https://developer.chrome.com/blog/how-to-convert-arraybuffer-to-and-from-string/ */ function ab2str(buf) { return String.fromCharCode.apply(null, new Uint8Array(buf)); } /* Export the given key and write it into the "exported-key" space. */ async function exportCryptoKey(key) { const exported = await window.crypto.subtle.exportKey("spki", key); const exportedAsString = ab2str(exported); const exportedAsBase64 = window.btoa(exportedAsString); const pemExported = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`; const exportKeyOutput = document.querySelector(".exported-key"); exportKeyOutput.textContent = pemExported; } /* Generate an encrypt/decrypt key pair, then set up an event listener on the "Export" button. */ window.crypto.subtle .generateKey( { name: "RSA-OAEP", // Consider using a 4096-bit key for systems that require long-term security modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256", }, true, ["encrypt", "decrypt"], ) .then((keyPair) => { const exportButton = document.querySelector(".spki"); exportButton.addEventListener("click", () => { exportCryptoKey(keyPair.publicKey); }); });

JSON Web Key エクスポート

この例では、ECDSA 秘密署名鍵を JSON ウェブ鍵オブジェクトとしてエクスポートします。完全なコードは GitHub で参照してください.

js
/* Export the given key and write it into the "exported-key" space. */ async function exportCryptoKey(key) { const exported = await window.crypto.subtle.exportKey("jwk", key); const exportKeyOutput = document.querySelector(".exported-key"); exportKeyOutput.textContent = JSON.stringify(exported, null, " "); } /* Generate a sign/verify key pair, then set up an event listener on the "Export" button. */ window.crypto.subtle .generateKey( { name: "ECDSA", namedCurve: "P-384", }, true, ["sign", "verify"], ) .then((keyPair) => { const exportButton = document.querySelector(".jwk"); exportButton.addEventListener("click", () => { exportCryptoKey(keyPair.privateKey); }); });

仕様書

Specification
Web Cryptography Level 2
# SubtleCrypto-method-exportKey

ブラウザーの互換性

Enable JavaScript to view this browser compatibility table.

関連情報