|
| 1 | +h1: Encrypting and decrypting data with an asymmetric key |
| 2 | +paragraph: Learn how to encrypt and decrypt data with an asymmetric key using Key Manager with Scaleway Go SDK. |
| 3 | +tags: key sensitive-data encrypt decrypt asymmetric digest |
| 4 | +dates: |
| 5 | +validation: 2025-02-06 |
| 6 | +posted: 2025-02-06 |
| 7 | +--- |
| 8 | + |
| 9 | +Scaleway's Key Manager provides a secure way to manage asymmetric keys, allowing you to offload sensitive cryptographic |
| 10 | +operations to a managed service. In this guide, you'll learn how to integrate the Scaleway Go SDK to encrypt and decrypt |
| 11 | +data using an rsa_oaep_3072_sha256 key directly through the Key Manager API. |
| 12 | + |
| 13 | + |
| 14 | +<Message type="warning"> |
| 15 | + Please note that we do not recommend using asymmetric encryption for anything other than key encryption. |
| 16 | + For all other purposes (eg. encrypting large data or files), we recommend using Tink with Scaleway's Key Manager as explained [here.](/key-manager/api-cli/encrypt-decrypt-data-with-km-dek/) |
| 17 | +</Message> |
| 18 | + |
| 19 | +## Configuring your environment variables |
| 20 | + |
| 21 | +Configuring your environment variables allows the Go application to authenticate and use Scaleway's API and Key Manager. |
| 22 | + |
| 23 | +Open a terminal and paste the following commands to export your environment variables. Make sure that you add your **own variables**. You can also use a Scaleway configuration file. |
| 24 | + |
| 25 | + ```bash |
| 26 | + export SCW_ACCESS_KEY="<API-ACCESS-KEY>" |
| 27 | + export SCW_SECRET_KEY="<API-SECRET-KEY>" |
| 28 | + export SCW_DEFAULT_ORGANIZATION_ID="<Scaleway-Organization-ID>" |
| 29 | + export SCW_DEFAULT_REGION="<region>" |
| 30 | + export SCW_API_URL="<api-URL>" |
| 31 | + ``` |
| 32 | + |
| 33 | +## Encrypt data |
| 34 | + |
| 35 | +This operation takes place locally, ensuring the plaintext message never leaves your environment unprotected. |
| 36 | +The public key can be fetched using the Key Manager API, parsed, and used to encrypt data with RSA-OAEP and SHA-256 padding. |
| 37 | + |
| 38 | +```golang |
| 39 | +// encryptAsymmetric encrypts data on your local machine using an 'rsa_oaep_3072_sha256' key retrieved from Scaleway KMS. |
| 40 | +// |
| 41 | +// Parameters: |
| 42 | +// - keyID: The unique identifier of the asymmetric key stored in Scaleway KMS. |
| 43 | +// - message: The plaintext message that needs to be encrypted. |
| 44 | +// |
| 45 | +// Returns: |
| 46 | +// - error: An error if the encryption process fails, otherwise nil. |
| 47 | +func encryptAsymmetric(keyID string, message string) error { |
| 48 | + // Initialize the Scaleway client |
| 49 | + client, err := scw.NewClient(scw.WithEnv()) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + kmsApi := key_manager.NewAPI(client) |
| 54 | + |
| 55 | + // Retrieve the public key from Scaleway KMS. |
| 56 | + response, err := kmsApi.GetPublicKey(&key_manager.GetPublicKeyRequest{ |
| 57 | + KeyID: keyID, |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to get public key: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Parse the public key. Note, this example assumes the public key is in the |
| 64 | + // RSA format. |
| 65 | + block, _ := pem.Decode([]byte(response.Pem)) |
| 66 | + publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("failed to parse public key: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + // Convert the message into bytes. Cryptographic plaintexts and |
| 72 | + // ciphertexts are always byte arrays. |
| 73 | + plaintext := []byte(message) |
| 74 | + |
| 75 | + // Encrypt data using the RSA public key. |
| 76 | + ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintext, nil) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("rsa.EncryptOAEP: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + fmt.Printf("Encrypted ciphertext: %s\n", ciphertext) |
| 82 | + return nil |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +<Message type="note"> |
| 87 | + Please note that : |
| 88 | + - Encryption can also be performed using the Scaleway's Key Manager Encrypt method. |
| 89 | + - In the case of asymmetric encryption, the maximum payload size allowed depends on the key algo (190 bytes for `RSA_OAEP_2048_SHA256`, 318 bytes for `RSA_OAEP_3072_SHA256` and 446 bytes for `RSA_OAEP_4096_SHA256`). |
| 90 | +</Message> |
| 91 | + |
| 92 | +## Decrypt data |
| 93 | + |
| 94 | +To retrieve the original message, you must send the encrypted ciphertext to Scaleway Key Manager, |
| 95 | +which uses the private portion of the asymmetric key to decrypt it. This ensures your private key remains secure within |
| 96 | +Scaleway’s infrastructure. |
| 97 | + |
| 98 | +```golang |
| 99 | + |
| 100 | +// decryptAsymmetric attempts to decrypt a given ciphertext using an 'rsa_oaep_3072_sha256' key from Scaleway KMS. |
| 101 | +// |
| 102 | +// Parameters: |
| 103 | +// - keyID: The unique identifier of the asymmetric key stored in Scaleway KMS. |
| 104 | +// - ciphertext: The encrypted data that needs to be decrypted. |
| 105 | +// |
| 106 | +// Returns: |
| 107 | +// - error: An error if the decryption process fails, otherwise nil. |
| 108 | +func decryptAsymmetric(keyID string, ciphertext []byte) error { |
| 109 | + // Initialize the Scaleway client |
| 110 | + client, err := scw.NewClient(scw.WithEnv()) |
| 111 | + if err != nil { |
| 112 | + panic(err) |
| 113 | + } |
| 114 | + kmsApi := key_manager.NewAPI(client) |
| 115 | + |
| 116 | + // Build the request. |
| 117 | + req := &key_manager.DecryptRequest{ |
| 118 | + KeyID: keyID, |
| 119 | + Ciphertext: ciphertext, |
| 120 | + } |
| 121 | + |
| 122 | + // Call the API. |
| 123 | + result, err := kmsApi.Decrypt(req) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("failed to decrypt ciphertext: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + fmt.Printf("Decrypted plaintext: %s", result.Plaintext) |
| 129 | + return nil |
| 130 | +} |
| 131 | +``` |
0 commit comments