Setelah menginisialisasi SDK client instance KMS, Anda dapat menggunakannya untuk memanggil API Encrypt dan Decrypt guna melakukan enkripsi dan dekripsi data. Topik ini menyediakan contoh kode untuk keperluan tersebut.
Contoh lengkap
Penjelasan contoh
Inisialisasi client
import com.aliyun.dkms.gcs.openapi.models.Config;
import com.aliyun.dkms.gcs.sdk.Client;
public static void initClient() throws Exception {
// Protokol koneksi. Setel nilainya menjadi https. Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
Config config = new Config();
config.setProtocol("https");
// Client key.
config.setClientKeyFile("<CLIENT_KEY_FILE>");
// Password client key.
config.setPassword("<PASSWORD>");
// Titik akhir instance KMS Anda. Setel nilainya dalam format berikut: <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com.
config.setEndpoint("<ENDPOINT>");
// Sertifikat otoritas sertifikat (CA) instance KMS. Anda dapat menentukan path ke file sertifikat CA atau memasukkan isi dari sertifikat CA.
config.setCaFilePath("<CA_CERTIFICATE_PATH>");
// Sebagai alternatif, setel isi sertifikat CA instance KMS.
//config.setCa("<CA_CERTIFICATE_CONTENT");
client = new Client(config);
}Panggil API Encrypt untuk mengenkripsi data menggunakan kunci simetris
Saat menggunakan Encrypt untuk mengamankan data, pastikan menyimpan ciphertext yang dihasilkan (CiphertextBlob), ID kunci (KeyId), Iv, dan parameter algoritma enkripsi (Algorithm).
// Contoh enkripsi.
private static AesEncryptContext encryptSample(String keyId, String plaintext) {
// Konstruksi permintaan enkripsi.
EncryptRequest encryptRequest = new EncryptRequest();
encryptRequest.setKeyId(keyId);
encryptRequest.setPlaintext(plaintext.getBytes(StandardCharsets.UTF_8));
try {
// Panggil antarmuka enkripsi untuk mengenkripsi.
// Untuk mengabaikan sertifikat server, Anda dapat menggunakan kode yang dikomentari di sini untuk memanggil.
//RuntimeOptions runtimeOptions = new RuntimeOptions();
//runtimeOptions.setIgnoreSSL(true);
//EncryptResponse encryptResponse = client.encryptWithOptions(encryptRequest, runtimeOptions);
EncryptResponse encryptResponse = client.encrypt(encryptRequest);
System.out.printf("KeyId: %s%n", encryptResponse.getKeyId());
System.out.printf("CiphertextBlob: %s%n", Arrays.toString(encryptResponse.getCiphertextBlob()));
System.out.printf("Iv: %s%n", Arrays.toString(encryptResponse.getIv()));
return new AesEncryptContext(encryptResponse.getKeyId(), encryptResponse.getCiphertextBlob(), encryptResponse.getIv(), encryptResponse.getAlgorithm());
} catch (TeaException e) {
System.out.printf("code: %s%n", ((TeaException) e).getCode());
System.out.printf("message: %s%n", e.getMessage());
System.out.printf("requestId: %s%n", ((TeaException) e).getData().get("requestId"));
e.printStackTrace();
throw new RuntimeException(e);
} catch (Exception e) {
System.out.printf("err enkripsi: %s%n", e.getMessage());
e.printStackTrace();
throw new RuntimeException(e);
}
}Panggil Decrypt API untuk mendekripsi ciphertext menggunakan kunci simetris
// Contoh dekripsi.
private static String decryptSample(final AesEncryptContext aesEncryptContext) {
// Konstruksi objek permintaan dekripsi.
DecryptRequest decryptRequest = new DecryptRequest();
decryptRequest.setKeyId(aesEncryptContext.getKeyId());
decryptRequest.setCiphertextBlob(aesEncryptContext.getCiphertextBlob());
decryptRequest.setAlgorithm(aesEncryptContext.getAlgorithm());
decryptRequest.setIv(aesEncryptContext.getIv());
try {
// Panggil antarmuka dekripsi untuk mendekripsi.
// Untuk mengabaikan sertifikat server, Anda dapat menggunakan kode yang dikomentari di sini untuk memanggil.
//RuntimeOptions runtimeOptions = new RuntimeOptions();
//runtimeOptions.setIgnoreSSL(true);
//DecryptResponse decryptResponse = client.decryptWithOptions(decryptRequest, runtimeOptions);
DecryptResponse decryptResponse = client.decrypt(decryptRequest);
System.out.printf("KeyId: %s%n", decryptResponse.getKeyId());
System.out.printf("Plaintext: %s%n", new String(decryptResponse.getPlaintext()));
System.out.printf("RequestId: %s%n", decryptResponse.getRequestId());
return new String(decryptResponse.getPlaintext());
} catch (TeaException e) {
System.out.printf("code: %s%n", ((TeaException) e).getCode());
System.out.printf("message: %s%n", e.getMessage());
System.out.printf("requestId: %s%n", ((TeaException) e).getData().get("requestId"));
e.printStackTrace();
throw new RuntimeException(e);
} catch (Exception e) {
System.out.printf("err dekripsi: %s%n", e.getMessage());
e.printStackTrace();
throw new RuntimeException(e);
}
}