Questo argomento fornisce informazioni sulla creazione e sull'utilizzo di una chiave per asimmetrica con una chiave RSA. Se vuoi utilizzare chiavi asimmetriche per creare e sulla convalida delle firme, consulta l'articolo Creazione e convalida delle firme digitali. Se vuoi utilizzare chiavi simmetriche per la crittografia e la decrittografia, consulta Crittografia e decriptazione dei dati.
La crittografia asimmetrica utilizza la parte di chiave pubblica della chiave asimmetrica la decrittografia utilizza la parte della chiave privata della chiave. Cloud Key Management Service fornisce funzionalità per recuperare la chiave pubblica e funzionalità per decriptare testo crittografato che è stato criptato con la chiave pubblica. Cloud KMS non consente l'accesso diretto alla chiave privata.
Prima di iniziare
Questo argomento fornisce esempi eseguiti dalla riga di comando. Per semplificare l'uso per visualizzare gli esempi, utilizza Cloud Shell. L'esempio di crittografia utilizza OpenSSL, preinstallato su Cloud Shell.
Crea una chiave asimmetrica con lo scopo della chiave
ASYMMETRIC_DECRYPT
. Per vedere quali algoritmi sono supportati per lo scopo chiaveASYMMETRIC_DECRYPT
, consulta Algoritmi di crittografia asimmetrici. Non puoi seguire questa procedura con una chiave con scopo diASYMMETRIC_SIGN
.Se vuoi utilizzare la riga di comando, installa OpenSSL se non usi ce l'hai già. Se utilizzi Cloud Shell, OpenSSL è già installato.
- Utenti macOS: La versione di OpenSSL installata su macOS non supporta i flag utilizzati per decriptare i dati in questo argomento. Per seguire questi passaggi su macOS: installa OpenSSL da Homebrew.
Controllo dell'accesso alla chiave
Per un utente o un servizio che recupererà la chiave pubblica, concedi il metodo Autorizzazione
cloudkms.cryptoKeyVersions.viewPublicKey
per la chiave asimmetrica. La per criptare i dati è necessaria una chiave pubblica.Per un utente o un servizio che decripta i dati criptati con chiave pubblica, concedi l'autorizzazione
cloudkms.cryptoKeyVersions.useToDecrypt
su la chiave asimmetrica.
Scopri di più su autorizzazioni e ruoli in Cloud KMS in Autorizzazioni e ruoli.
Criptare i dati
Per criptare i dati utilizzando una chiave di crittografia asimmetrica, recupera la chiave pubblica e e usare la chiave pubblica per criptare i dati,
gcloud
Questo esempio richiede che OpenSSL sia installato sul sistema locale.
Scarica chiave pubblica
Scarica la chiave pubblica:
gcloud kms keys versions get-public-key key-version \ --key key \ --keyring key-ring \ --location location \ --output-file public-key-path
Sostituisci key-version con la versione della chiave che include la chiave pubblica. Sostituisci key con il nome della chiave. Sostituisci key-ring con il nome del keyring in cui si trova la chiave. Sostituisci location con la località di Cloud KMS per l'account il keyring. Sostituisci public-key-path con la posizione in cui salvare il chiave pubblica sul sistema locale.
Cripta i dati
Cripta i dati utilizzando la chiave pubblica che hai appena scaricato e salva l'output in un file:
openssl pkeyutl -in cleartext-data-input-file \ -encrypt \ -pubin \ -inkey public-key-path \ -pkeyopt rsa_padding_mode:oaep \ -pkeyopt rsa_oaep_md:sha256 \ -pkeyopt rsa_mgf1_md:sha256 \ > encrypted-data-output-file
Sostituisci cleartext-data-input-file con il percorso e il nome del file in crittografare.
Sostituisci public-key-path con il percorso e il nome del file in cui scaricato la chiave pubblica.
Sostituisci encrypted-data-output-file con il percorso e il nome del file per salvare i dati criptati.
C#
Per eseguire questo codice, per prima cosa configura un ambiente di sviluppo C# e installare l'SDK Cloud KMS C#.
Go
Per utilizzare Cloud KMS nella riga di comando, devi prima Installa o esegui l'upgrade alla versione più recente di Google Cloud CLI.
Java
Per eseguire questo codice, per prima cosa configura un ambiente di sviluppo Java e installare l'SDK Java di Cloud KMS.
Node.js
Per eseguire questo codice, devi innanzitutto configurare un ambiente di sviluppo Node.js e installare l'SDK Node.js di Cloud KMS.
PHP
Per eseguire questo codice, devi innanzitutto scoprire come utilizzare PHP su Google Cloud e installare l'SDK PHP Cloud KMS.
Python
Per eseguire questo codice, configura prima un ambiente di sviluppo Python e installare l'SDK per Python di Cloud KMS.
Ruby
Per eseguire questo codice, prima configura un ambiente di sviluppo Ruby e installa l'SDK Ruby di Cloud KMS.
Decripta i dati
Utilizza Cloud KMS per eseguire la decriptazione.
gcloud
Per utilizzare Cloud KMS nella riga di comando, devi prima Installa o esegui l'upgrade alla versione più recente di Google Cloud CLI.
gcloud kms asymmetric-decrypt \ --version key-version \ --key key \ --keyring key-ring \ --location location \ --ciphertext-file file-path-with-encrypted-data \ --plaintext-file file-path-to-store-plaintext
Sostituisci key-version con la versione della chiave oppure ometti --version
per rilevare automaticamente la versione. Sostituisci key con il nome
della chiave da utilizzare per la decrittografia. Sostituisci key-ring con il nome di
nel keyring in cui si troverà la chiave. Sostituisci location con
Località Cloud KMS per il keyring. Sostituisci
file-path-with-encrypted-data e file-path-to-store-plaintext
con i percorsi dei file locali per la lettura dei dati criptati e il salvataggio dei file
come output.
Per informazioni su tutti i flag e i possibili valori, esegui il comando con il flag --help
.
Per visualizzare i contenuti del file decriptato, apri il file nell'editor o nel terminale. Ecco un esempio che mostra i contenuti del file utilizzando l'istruzione cat
:
cat ./my-file.txt
C#
Per eseguire questo codice, devi innanzitutto configurare un ambiente di sviluppo C# e installare l'SDK C# Cloud KMS.
Go
Per eseguire questo codice, devi prima configurare un ambiente di sviluppo Go e installa l'SDK Cloud KMS Go.
Java
Per eseguire questo codice, per prima cosa configura un ambiente di sviluppo Java e installare l'SDK Java di Cloud KMS.
Node.js
Per eseguire questo codice, devi prima configurare un ambiente di sviluppo Node.js e installare l'SDK Node.js di Cloud KMS.
PHP
Per eseguire questo codice, scopri innanzitutto come utilizzare PHP su Google Cloud e installa l'SDK PHP di Cloud KMS.
Python
Per eseguire questo codice, configura prima un ambiente di sviluppo Python e installare l'SDK per Python di Cloud KMS.
Ruby
Per eseguire questo codice, devi innanzitutto configurare un ambiente di sviluppo Ruby e installare l'SDK Ruby Cloud KMS.
API
In questi esempi viene utilizzato curl come client HTTP per dimostrare l'uso dell'API. Per ulteriori informazioni sul controllo dell'accesso, consulta Accedere all'API Cloud KMS.
Utilizza il metodo
CryptoKeyVersions.asymmetricDecrypt
.
Risoluzione dei problemi
incorrect key purpose: ASYMMETRIC_SIGN
Puoi decriptare i dati solo con una chiave con lo scopo della chiave ASYMMETRIC_DECRYPT
.
invalid parameter
durante la decrittografia su macOS
La versione di OpenSSL installata su macOS non supporta i flag utilizzati per decriptare i dati in questo argomento. Per seguire questi passaggi su macOS: installa OpenSSL da Homebrew.
data too large for key size
La dimensione massima del payload per la decrittografia RSA dipende dalle dimensioni della chiave e dalla spaziatura interna
dell'algoritmo. Tutti i formati di crittografia RSA utilizzati da Cloud KMS
OAEP, standardizzato in
RFC 2437. Come riferimento rapido, i seguenti algoritmi supportano i seguenti dimensioni massime del payload (maxMLen
, in byte):
Algoritmo | Parametri | Lunghezza massima del messaggio |
---|---|---|
RSA_DECRYPT_OAEP_2048_SHA256 | k = 256; hLen = 32; | maxMLen = 190 |
RSA_DECRYPT_OAEP_3072_SHA256 | k = 384; hLen = 32; | maxMLen = 318 |
RSA_DECRYPT_OAEP_4096_SHA256 | k = 512; hLen = 32; | maxMLen = 446 |
RSA_DECRYPT_OAEP_4096_SHA512 | k = 512; hLen = 64; | maxMLen = 382 |
La crittografia asimmetrica non è consigliata per i messaggi di lunghezze diverse che possono essere superiori a questi limiti. Prendi in considerazione l'utilizzo della crittografia ibrida. Tink è una libreria crittografica che utilizza questo approccio.