Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette rubrique explique comment formater vos clés afin qu'elles puissent être importées par Cloud KMS en tant que nouvelles versions de clé.
Le format du matériel de votre clé varie selon si le matériel est importé dans une clé symétrique ou dans une clé asymétrique. Pour plus d'informations sur la différence entre les clés symétriques et asymétriques, consultez la page Objectifs et algorithmes des clés.
Formats de clé acceptés
Clés symétriques pour le chiffrement : Les clés symétriques importées doivent contenir 16 octets (pour le chiffrement symétrique brut uniquement) ou 32 octets de données binaires, et ne doivent pas être encodées.
Si votre clé est encodée en hexadécimal ou en base64, vous devez la décoder avant d'essayer de l'importer.
Clés symétriques pour la signature (clés MAC): les clés de signature HMAC importées doivent avoir une longueur égale à la longueur de sortie de la fonction de hachage cryptographique utilisée (par exemple, les clés HMAC-SHA256 doivent avoir une longueur de 32 octets), et ne doivent pas être encodées. Si votre clé est encodée en hexadécimal ou en base64, vous devez la décoder avant d'essayer de l'importer.
Clés asymétriques pour le chiffrement ou la signature: les clés asymétriques importées doivent être au format PKCS #8 et encodées au format DER. Le format PCKS #8 est défini dans la norme RFC 5208. L'encodage DER est défini dans la norme International Telecommunications Union X.680. Les clés asymétriques doivent utiliser l'une des combinaisons de longueur et d'algorithme acceptées par Cloud KMS.
.
Certains aspects d'une clé, tels que sa longueur, ne peuvent pas être modifiés après sa création. Dans ce cas, la clé ne peut pas être importée dans Cloud KMS.
Vérifier une clé symétrique
Utilisez la commande wc pour vérifier la longueur d'une clé symétrique.
wc -c /path/to/unwrapped-key
Vous ne pouvez pas importer une clé symétrique d'une longueur autre que 32.
Les clés de signature symétrique (clés MAC) doivent avoir une longueur égale à la longueur de sortie de la fonction de hachage cryptographique utilisée (par exemple, les clés HMAC-SHA256 doivent avoir une longueur de 32 octets).
Utilisez la commande file pour vérifier le format d'une clé.
file /path/to/unwrapped-key
Si le résultat est data, la clé est au bon format pour être importée.
Si le résultat est ASCII text, utilisez la commande cat pour afficher le contenu du fichier.
S'il s'agit d'une chaîne de lettres et de chiffres se terminant par un signe =, il peut s'agir d'un encodage en base64. Utilisez la commande base64 (Base64.exe sous Windows) pour la décoder. Voici un exemple de clé encodée en base64 :
THzArjassB+giKeNeT1Zr74OgV24t+Ep+37Ec6ojB3Y=
S'il contient une ou plusieurs lignes de nombres hexadécimaux, il peut s'agir d'un encodage en hexadécimal. Utilisez la commande xxd (ou la commande PowerShell Format-Hex sous Windows) pour le décodage. Voici un exemple de clé encodée en hexadécimal:
S'il contient tout autre texte, il peut s'agir d'une clé symétrique non valide.
Formater des clés asymétriques
Les clés asymétriques utilisant l'un des algorithmes compatibles peuvent être importées. En pratique, il est difficile de déterminer rétroactivement l'algorithme utilisé pour créer une clé asymétrique. Pour cette raison, nous vous recommandons d'exécuter les commandes suivantes sur chaque clé asymétrique avant d'essayer de l'importer dans Cloud KMS.
Utilisez la commande file pour vérifier le format d'une clé.
file /path/to/unwrapped-key
Si le résultat est PEM, la clé est au format PEM. S'il s'agit de ASCII text, il est probablement au format PEM. Dans les deux cas, exécutez la commande suivante pour le convertir au format DER PCKS #8 :
Si le résultat est data, la clé est susceptible d'être au format DER, mais elle peut ne pas être au format PKCS #8. Exécutez la commande suivante pour vous assurer que le format de la clé est correct. La commande n'a aucun effet si la clé est déjà au bon format. Dans ce cas, vous pouvez utiliser la commande diff pour vérifier que les fichiers d'entrée et de sortie sont identiques.
openssl pkcs8 -topk8 -nocrypt -inform DER -outform DER \
-in /path/to/asymmetric-key-der \
-out /path/to/formatted-key
Dépannage
Si vous exécutez les commandes ci-dessus et que vous pensez que la clé est dans un format approprié, mais que l'importation échoue toujours, recherchez des erreurs dans la console Google Cloud , puis consultez la section Résoudre les échecs d'importation.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/05 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/05 (UTC)."],[],[],null,["# Formatting keys for import\n\nThis topic describes how to format your keys so that they can be imported by\nCloud KMS as new key versions.\n\nThe correct format for your key material varies based on whether the key\nmaterial is being imported into a symmetric key, or into an asymmetric key. For\nmore information about the difference between symmetric and asymmetric keys, see\n[Key purposes and algorithms](/kms/docs/algorithms).\n\nSupported key formats\n---------------------\n\n- **Symmetric keys for encryption** : Imported symmetric keys must be 16 bytes (for [raw symmetric encryption](/kms/docs/raw-encryption) only) or 32 bytes of binary data, and must *not* be encoded. If your key is hex-encoded or base64-encoded, then you must decode it before attempting to import it.\n- **Symmetric keys for signing (MAC keys)** : Imported HMAC signing keys must have a length equal to the output length of the cryptographic hash function being used (for example, HMAC-SHA256 keys must have a length of 32 bytes), and must *not* be encoded. If your key is hex-encoded or base64-encoded, then you must decode it before attempting to import it.\n- **Asymmetric keys for encryption or signing** : Imported asymmetric keys must be in PKCS #8 format and must be DER-encoded. PCKS #8 format is defined in [RFC 5208](https://tools.ietf.org/html/rfc5208). DER encoding is defined in [International\n Telecommunications Union X.680](https://www.itu.int/rec/T-REC-X.680/en). Asymmetric keys must use one of the [length and algorithm combinations](/kms/docs/algorithms) supported by Cloud KMS.\n\n| **Important:** An RSA key's public exponent must be 65,537 or higher. This is a Digital Signature Standard (DSS) requirement noted in the Criteria for IFC Key Pairs section of [FIPS PUB 186-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf), Section B.3.1\n\nSome aspects of a key, such as the key's length, cannot be changed after the key\nis created. In these cases, the key cannot be imported into Cloud KMS.\n\nChecking a symmetric key\n------------------------\n\nUse the `wc` command to check a symmetric key's length. \n\n```\nwc -c /path/to/unwrapped-key\n```\n\n- You cannot import a symmetric encryption key with a length other than 32.\n\n- Symmetric signing keys (MAC keys) must have a length equal to the output\n length of the cryptographic hash function being used (e.g. HMAC-SHA256 keys\n must have a length of 32 bytes).\n\nUse the `file` command to check a key's format. \n\n```\nfile /path/to/unwrapped-key\n```\n\n- If the output is `data`, the key is in the correct format to be imported.\n\n- If the output is `ASCII text`, use the `cat` command to display the contents\n of the file.\n\n - If it is a string of letters and numbers ending in an `=` sign, it might\n be base64-encoded. Use the `base64` command (`Base64.exe` on Windows) to\n decode it. The following is an example of a base64-encoded key:\n\n ```\n THzArjassB+giKeNeT1Zr74OgV24t+Ep+37Ec6ojB3Y=\n ```\n - If it contains one or more lines of hexadecimal numbers, it might be\n hex-encoded. Use the `xxd` command (or the\n [`Format-Hex` PowerShell command](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-hex)\n on Windows) to decode it. The following is an example of a hex-encoded\n key:\n\n ```\n 00000000: 4c7c c0ae 36ac b01f a088 a78d 793d 59af L|..6.......y=Y.\n 00000010: be0e 815d b8b7 e129 fb7e c473 aa23 0776 ...]...).~.s.#.v\n ```\n - If it contains any other text, it may not be a valid symmetric key.\n\nFormatting asymmetric keys\n--------------------------\n\nAsymmetric keys using any of the supported [algorithms](/kms/docs/algorithms)\ncan be imported. In practice, it is difficult to retroactively determine the\nalgorithm used to create an asymmetric key. For that reason, we recommend that\nyou run the following commands on each asymmetric key before attempting to\nimport it into Cloud KMS.\n\n1. Use the `file` command to check a key's format.\n\n ```\n file /path/to/unwrapped-key\n ```\n - If the output is `PEM`, the key is in PEM format. If it is `ASCII text`,\n it is probably in PEM format. In either case, run the following command\n to convert it to PCKS#8 DER format:\n\n ```\n openssl pkcs8 -topk8 -nocrypt -inform PEM -outform DER \\\n -in /path/to/asymmetric-key-pem \\\n -out /path/to/formatted-key\n ```\n - If the output is `data`, the key is likely to be in DER format, but it\n may not be in PKCS #8 format. Run the following command to ensure that\n the key is in the correct format. The command has no effect if the key\n is already in the correct format. In that case, you can use the `diff`\n command to verify that the input and output file are identical.\n\n ```\n openssl pkcs8 -topk8 -nocrypt -inform DER -outform DER \\\n -in /path/to/asymmetric-key-der \\\n -out /path/to/formatted-key\n ```\n\nTroubleshooting\n---------------\n\nIf you run the commands above and you believe the key is in an appropriate\nformat, but the import still fails, check for errors in Google Cloud console, and\nthen see [Troubleshooting failed\nimports](/kms/docs/troubleshooting-failed-imports).\n\nWhat's next\n-----------\n\n- Continue to [Import a key version](/kms/docs/importing-a-key)\n- Learn about [key import](/kms/docs/key-import)"]]