似乎不存在 C# 的示例代码(目前),但是有 some sample C++ code 可以演示您想要的:
void Samples::RsaEncryptDecrypt()
{
Announce("RsaEncryptDecrypt");
// This sample demostrates the use of the TPM for RSA operations.
// We will make a key in the "null hierarchy".
TPMT_PUBLIC primTempl(TPM_ALG_ID::SHA1,
TPMA_OBJECT::decrypt | TPMA_OBJECT::userWithAuth | TPMA_OBJECT::sensitiveDataOrigin,
null, // No policy
TPMS_RSA_PARMS(null, TPMS_SCHEME_OAEP(TPM_ALG_ID::SHA1), 2048, 65537),
TPM2B_PUBLIC_KEY_RSA());
// Create the key
auto storagePrimary = tpm.CreatePrimary(TPM_RH_NULL, null, primTempl, null, null);
TPM_HANDLE& keyHandle = storagePrimary.handle;
ByteVec dataToEncrypt = TPM_HASH::FromHashOfString(TPM_ALG_ID::SHA1, "secret");
cout << "Data to encrypt: " << dataToEncrypt << endl;
auto enc = tpm.RSA_Encrypt(keyHandle, dataToEncrypt, TPMS_NULL_ASYM_SCHEME(), null);
cout << "RSA-encrypted data: " << enc << endl;
auto dec = tpm.RSA_Decrypt(keyHandle, enc, TPMS_NULL_ASYM_SCHEME(), null);
cout << "decrypted data: " << dec << endl;
if (dec == dataToEncrypt)
cout << "Decryption worked" << endl;
_ASSERT(dataToEncrypt == dec);
// Now encrypt using TSS.C++ library functions
ByteVec mySecret = tpm._GetRandLocal(20);
enc = storagePrimary.outPublic.Encrypt(mySecret, null);
dec = tpm.RSA_Decrypt(keyHandle, enc, TPMS_NULL_ASYM_SCHEME(), null);
cout << "My secret: " << mySecret << endl;
cout << "My decrypted secret: " << dec << endl;
_ASSERT(mySecret == dec);
// Now with padding
ByteVec pad { 1, 2, 3, 4, 5, 6, 0 };
enc = storagePrimary.outPublic.Encrypt(mySecret, pad);
dec = tpm.RSA_Decrypt(keyHandle, enc, TPMS_NULL_ASYM_SCHEME(), pad);
cout << "My secret: " << mySecret << endl;
cout << "My decrypted secret: " << dec << endl;
_ASSERT(mySecret == dec);
tpm.FlushContext(keyHandle);
} // RsaEncryptDecrypt()
当然,您可能希望使用 SRK 或子键,而不是创建新的主键。