- C
-
若要在 中建立原始 RSA keyring 適用於 C 的 AWS Encryption SDK,請使用 aws_cryptosdk_raw_rsa_keyring_new
。
在 中建構原始 RSA keyring 時 適用於 C 的 AWS Encryption SDK,請務必提供 PEM 檔案的內容,其中包含每個金鑰做為 null 終止的 C 字串,而不是路徑或檔案名稱。如需完整範例,請參閱 raw_rsa_keyring.c。
struct aws_allocator *alloc = aws_default_allocator();
AWS_STATIC_STRING_FROM_LITERAL(key_namespace, "HSM_01");
AWS_STATIC_STRING_FROM_LITERAL(key_name, "RSA_2048_06
");
struct aws_cryptosdk_keyring *rawRsaKeyring = aws_cryptosdk_raw_rsa_keyring_new(
alloc,
key_namespace,
key_name,
private_key_from_pem,
public_key_from_pem,
AWS_CRYPTOSDK_RSA_OAEP_SHA256_MGF1);
- C# / .NET
-
若要在 AWS Encryption SDK 適用於 .NET 的 中執行個體化原始 RSA keyring,請使用 materialProviders.CreateRawRsaKeyring()
方法。如需完整範例,請參閱 RawRSAKeyringExample.cs。
下列範例使用適用於 .NET 的 4 AWS Encryption SDK .x 版。
// Instantiate the AWS Encryption SDK and material providers
var esdk = new ESDK(new AwsEncryptionSdkConfig());
var mpl = new MaterialProviders(new MaterialProvidersConfig());
var keyNamespace = "HSM_01";
var keyName = "RSA_2048_06
";
// Get public and private keys from PEM files
var publicKey = new MemoryStream(System.IO.File.ReadAllBytes("RSAKeyringExamplePublicKey.pem"));
var privateKey = new MemoryStream(System.IO.File.ReadAllBytes("RSAKeyringExamplePrivateKey.pem"));
// Create the keyring input
var createRawRsaKeyringInput = new CreateRawRsaKeyringInput
{
KeyNamespace = keyNamespace,
KeyName = keyName,
PaddingScheme = PaddingScheme.OAEP_SHA512_MGF1,
PublicKey = publicKey,
PrivateKey = privateKey
};
// Create the keyring
var rawRsaKeyring = materialProviders.CreateRawRsaKeyring(createRawRsaKeyringInput);
- JavaScript Browser
-
瀏覽器 適用於 JavaScript 的 AWS Encryption SDK 中的 會從 WebCrypto 程式庫取得密碼編譯基本概念。在建構 keyring 之前,您必須使用 importPublicKey()
和/或 importPrivateKey()
將原始金鑰材料匯入 WebCrypto 後端。這可確保即使對 WebCrypto 的所有呼叫都是非同步的, keyring 也是完整的。匯入方法採用的物件包含包裝演算法及其填充模式。
匯入金鑰材料之後,請使用 RawRsaKeyringWebCrypto()
方法來實例化 keyring。在 JavaScript 中建構原始 RSA keyring 時,請注意與其他語言實作的潛在不相容。
下列範例使用 buildClient
函數來指定預設承諾政策 REQUIRE_ENCRYPT_REQUIRE_DECRYPT
。您也可以使用 buildClient
來限制加密訊息中的加密資料金鑰數量。如需詳細資訊,請參閱限制加密的資料金鑰。
如需完整範例,請參閱 rsa_simple.ts (JavaScript 瀏覽器)。
import {
RsaImportableKey,
RawRsaKeyringWebCrypto,
buildClient,
CommitmentPolicy,
} from '@aws-crypto/client-browser'
const { encrypt, decrypt } = buildClient(
CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
)
const privateKey = await RawRsaKeyringWebCrypto.importPrivateKey(
privateRsaJwKKey
)
const publicKey = await RawRsaKeyringWebCrypto.importPublicKey(
publicRsaJwKKey
)
const keyNamespace = 'HSM_01
'
const keyName = 'RSA_2048_06
'
const keyring = new RawRsaKeyringWebCrypto({
keyName,
keyNamespace,
publicKey,
privateKey,
})
- JavaScript Node.js
-
若要在 適用於 JavaScript 的 AWS Encryption SDK for Node.js 中執行個體化原始 RSA keyring,請建立新的 RawRsaKeyringNode
類別執行個體。wrapKey
參數會保留公有金鑰。unwrapKey
參數會保留私有金鑰。RawRsaKeyringNode
雖然您可以指定偏好的填充模式,但建構器會為您計算預設填充模式。
在 JavaScript 中建構原始 RSA keyring 時,請注意與其他語言實作的潛在不相容。
下列範例使用 buildClient
函數來指定預設承諾政策 REQUIRE_ENCRYPT_REQUIRE_DECRYPT
。您也可以使用 buildClient
來限制加密訊息中的加密資料金鑰數量。如需詳細資訊,請參閱限制加密的資料金鑰。
如需完整範例,請參閱 rsa_simple.ts (JavaScript Node.js)。
import {
RawRsaKeyringNode,
buildClient,
CommitmentPolicy,
} from '@aws-crypto/client-node'
const { encrypt, decrypt } = buildClient(
CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
)
const keyNamespace = 'HSM_01
'
const keyName = 'RSA_2048_06
'
const keyring = new RawRsaKeyringNode({ keyName, keyNamespace, rsaPublicKey, rsaPrivateKey})
- Java
-
final CreateRawRsaKeyringInput keyringInput = CreateRawRsaKeyringInput.builder()
.keyName("RSA_2048_06
")
.keyNamespace("HSM_01
")
.paddingScheme(PaddingScheme.OAEP_SHA256_MGF1
)
.publicKey(RSAPublicKey
)
.privateKey(RSAPrivateKey
)
.build();
final MaterialProviders matProv = MaterialProviders.builder()
.MaterialProvidersConfig(MaterialProvidersConfig.builder().build())
.build();
IKeyring rawRsaKeyring = matProv.CreateRawRsaKeyring(keyringInput);
- Python
-
下列範例會使用預設承諾政策 來執行個體化 AWS Encryption SDK 用戶端REQUIRE_ENCRYPT_REQUIRE_DECRYPT
。如需完整範例,請參閱 GitHub 中 適用於 Python 的 AWS Encryption SDK 儲存庫中的 raw_rsa_keyring_example.py。
# Define the key namespace and key name
key_name_space = "HSM_01
"
key_name = "RSA_2048_06
"
# Instantiate the material providers
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
config=MaterialProvidersConfig()
)
# Create Raw RSA keyring
keyring_input: CreateRawRsaKeyringInput = CreateRawRsaKeyringInput(
key_namespace=key_name_space,
key_name=key_name,
padding_scheme=PaddingScheme.OAEP_SHA256_MGF1
,
public_key=RSAPublicKey
,
private_key=RSAPrivateKey
)
raw_rsa_keyring: IKeyring = mat_prov.create_raw_rsa_keyring(
input=keyring_input
)
- Rust
-
// Instantiate the AWS Encryption SDK client
let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
// Optional: Create an encryption context
let encryption_context = HashMap::from([
("encryption".to_string(), "context".to_string()),
("is not".to_string(), "secret".to_string()),
("but adds".to_string(), "useful metadata".to_string()),
("that can help you".to_string(), "be confident that".to_string()),
("the data you are handling".to_string(), "is what you think it is".to_string()),
]);
// Define the key namespace and key name
let key_namespace: &str = "HSM_01
";
let key_name: &str = "RSA_2048_06
";
// Instantiate the material providers library
let mpl_config = MaterialProvidersConfig::builder().build()?;
let mpl = mpl_client::Client::from_conf(mpl_config)?;
// Create Raw RSA keyring
let raw_rsa_keyring = mpl
.create_raw_rsa_keyring()
.key_name(key_name)
.key_namespace(key_namespace)
.padding_scheme(PaddingScheme::OaepSha256Mgf1
)
.public_key(aws_smithy_types::Blob::new(RSAPublicKey
))
.private_key(aws_smithy_types::Blob::new(RSAPrivateKey
))
.send()
.await?;
- Go
-
// Instantiate the material providers library
matProv, err := awscryptographymaterialproviderssmithygenerated.NewClient(awscryptographymaterialproviderssmithygeneratedtypes.MaterialProvidersConfig{})
// Create Raw RSA keyring
rsaKeyRingInput := awscryptographymaterialproviderssmithygeneratedtypes.CreateRawRsaKeyringInput{
KeyName: "rsa",
KeyNamespace: "rsa-keyring",
PaddingScheme: awscryptographymaterialproviderssmithygeneratedtypes.PaddingSchemePkcs1,
PublicKey: pem.EncodeToMemory(publicKeyBlock),
PrivateKey: pem.EncodeToMemory(privateKeyBlock),
}
rsaKeyring, err := matProv.CreateRawRsaKeyring(context.Background(), rsaKeyRingInput)
- Go
-
import (
"context"
mpl "aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygenerated"
mpltypes "aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygeneratedtypes"
client "github.com/aws/aws-encryption-sdk/awscryptographyencryptionsdksmithygenerated"
esdktypes "github.com/aws/aws-encryption-sdk/awscryptographyencryptionsdksmithygeneratedtypes"
)
// Instantiate the AWS Encryption SDK client
encryptionClient, err := client.NewClient(esdktypes.AwsEncryptionSdkConfig{})
if err != nil {
panic(err)
}
// Optional: Create an encryption context
encryptionContext := map[string]string{
"encryption": "context",
"is not": "secret",
"but adds": "useful metadata",
"that can help you": "be confident that",
"the data you are handling": "is what you think it is",
}
// Define the key namespace and key name
var keyNamespace = "HSM_01
"
var keyName = "RSA_2048_06
"
// Instantiate the material providers library
matProv, err := mpl.NewClient(mpltypes.MaterialProvidersConfig{})
if err != nil {
panic(err)
}
// Create Raw RSA keyring
rsaKeyRingInput := mpltypes.CreateRawRsaKeyringInput{
KeyName: keyName,
KeyNamespace: keyNamespace,
PaddingScheme: mpltypes.PaddingSchemeOaepSha512Mgf1
,
PublicKey: (RSAPublicKey
),
PrivateKey: (RSAPrivateKey
),
}
rsaKeyring, err := matProv.CreateRawRsaKeyring(context.Background(), rsaKeyRingInput)
if err != nil {
panic(err)
}