Mulai cepat: Mengenkripsi/Mendekripsi teks menggunakan MIP SDK (C++)
Mulai Cepat ini menunjukkan kepada Anda cara menggunakan lebih banyak SDK Perlindungan MIP. Menggunakan salah satu templat proteksi yang Anda cantumkan di Mulai Cepat sebelumnya, Anda menggunakan handler Perlindungan untuk Mengenkripsi teks ad hoc. Kelas Handler Perlindungan mengekspos berbagai operasi untuk menerapkan/menghapus perlindungan.
Prasyarat
Jika Anda belum melakukannya, pastikan untuk menyelesaikan prasyarat berikut sebelum melanjutkan:
- Mulai Cepat Lengkap : Mencantumkan templat perlindungan (C++) terlebih dahulu, yang membangun solusi Visual Studio pemula, untuk mencantumkan templat perlindungan yang tersedia untuk pengguna terautentikasi. Mulai Cepat "Enkripsi/Dekripsi teks" ini dibangun pada yang sebelumnya.
- Secara opsional: Tinjau Penangan perlindungan dalam konsep MIP SDK .
Menerapkan kelas pengamat untuk memantau objek handler Perlindungan
Mirip dengan pengamat yang Anda terapkan (untuk profil dan mesin Perlindungan) dalam Mulai Cepat inisialisasi Aplikasi, sekarang Anda menerapkan kelas pengamat untuk objek handler Perlindungan.
Buat implementasi dasar untuk pengamat handler Perlindungan, dengan memperluas kelas SDK mip::ProtectionHandler::Observer
. Pengamat diinstansiasi dan digunakan nanti, untuk memantau operasi handler Perlindungan.
Buka solusi Visual Studio yang Anda kerjakan di artikel "Mulai Cepat: Daftar templat perlindungan (C++)" sebelumnya.
Tambahkan kelas baru ke proyek Anda, yang menghasilkan file header/.h dan implementasi/.cpp untuk Anda:
- Di Penjelajah Solusi, klik kanan simpul proyek lagi, pilih Tambahkan, lalu pilih Kelas.
- Pada dialog Tambahkan Kelas:
- Di bidang Nama Kelas, masukkan "handler_observer". Perhatikan bahwa bidang file .h dan file .cpp diisi secara otomatis, berdasarkan nama yang Anda masukkan.
- Setelah selesai, klik tombol OK .
Setelah membuat file .h dan .cpp untuk kelas , kedua file dibuka di tab Grup Editor. Sekarang perbarui setiap file untuk mengimplementasikan kelas pengamat baru Anda:
Perbarui "handler_observer.h", dengan memilih/menghapus kelas yang dihasilkan
handler_observer
. Jangan hapus arahan preproscessor yang dihasilkan oleh langkah sebelumnya (#pragma, #include). Kemudian salin/tempel sumber berikut ke dalam file, setelah direktif praprosesor yang ada:#include <memory> #include "mip/protection/protection_engine.h" using std::shared_ptr; using std::exception_ptr; class ProtectionHandlerObserver final : public mip::ProtectionHandler::Observer { public: ProtectionHandlerObserver() { } void OnCreateProtectionHandlerSuccess(const shared_ptr<mip::ProtectionHandler>& protectionHandler, const shared_ptr<void>& context) override; void OnCreateProtectionHandlerFailure(const exception_ptr& Failure, const shared_ptr<void>& context) override; };
Perbarui "handler_observer.cpp", dengan memilih/menghapus implementasi kelas yang dihasilkan
handler_observer
. Jangan hapus arahan preproscessor yang dihasilkan oleh langkah sebelumnya (#pragma, #include). Kemudian salin/tempel sumber berikut ke dalam file, setelah direktif praprosesor yang ada:#include "handler_observer.h" using std::shared_ptr; using std::promise; using std::exception_ptr; void ProtectionHandlerObserver::OnCreateProtectionHandlerSuccess( const shared_ptr<mip::ProtectionHandler>& protectionHandler,const shared_ptr<void>& context) { auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get()); createProtectionHandlerPromise->set_value(protectionHandler); }; void ProtectionHandlerObserver::OnCreateProtectionHandlerFailure( const exception_ptr& Failure, const shared_ptr<void>& context) { auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get()) createProtectionHandlerPromise->set_exception(Failure); };
Secara opsional, gunakan Ctrl+Shift+B (Build Solution) untuk menjalankan kompilasi/tautan pengujian solusi Anda, untuk memastikannya berhasil dibangun sebelum melanjutkan.
Menambahkan logika untuk mengenkripsi dan mendekripsi teks ad hoc
Tambahkan logika untuk mengenkripsi dan mendekripsi teks ad hoc, menggunakan objek Mesin perlindungan.
Dengan menggunakan Penjelajah Solusi, buka file .cpp dalam proyek Anda yang berisi implementasi
main()
metode .Tambahkan #include berikut dan gunakan arahan, di bawah arahan yang ada yang sesuai, di bagian atas file:
#include "mip/protection/protection_descriptor_builder.h" #include "mip/protection_descriptor.h" #include "handler_observer.h" using mip::ProtectionDescriptor; using mip::ProtectionDescriptorBuilder; using mip::ProtectionHandler;
Menjelang akhir isi
Main()
, tempat Anda meninggalkannya di Mulai Cepat sebelumnya, sisipkan kode berikut://Encrypt/Decrypt text: string templateId = "<Template-ID>";//Template ID from previous QuickStart e.g. "bb7ed207-046a-4caf-9826-647cff56b990" string inputText = "<Sample-Text>";//Sample Text //Refer to ProtectionDescriptor docs for details on creating the descriptor auto descriptorBuilder = mip::ProtectionDescriptorBuilder::CreateFromTemplate(templateId); const std::shared_ptr<mip::ProtectionDescriptor>& descriptor = descriptorBuilder->Build(); //Create Publishing settings using a descriptor mip::ProtectionHandler::PublishingSettings publishingSettings = mip::ProtectionHandler::PublishingSettings(descriptor); //Create a publishing protection handler using Protection Descriptor auto handlerObserver = std::make_shared<ProtectionHandlerObserver>(); engine->CreateProtectionHandlerForPublishingAsync(publishingSettings, handlerObserver, pHandlerPromise); auto publishingHandler = pHandlerFuture.get(); std::vector<uint8_t> inputBuffer(inputText.begin(), inputText.end()); //Show action plan cout << "Applying Template ID " + templateId + " to: " << endl << inputText << endl; //Encrypt buffer using Publishing Handler std::vector<uint8_t> encryptedBuffer; encryptedBuffer.resize(static_cast<size_t>(publishingHandler->GetProtectedContentLength(inputText.size(), true))); publishingHandler->EncryptBuffer(0, &inputBuffer[0], static_cast<int64_t>(inputBuffer.size()), &encryptedBuffer[0], static_cast<int64_t>(encryptedBuffer.size()), true); std::string encryptedText(encryptedBuffer.begin(), encryptedBuffer.end()); cout << "Encrypted Text :" + encryptedText; //Show action plan cout << endl << "Decrypting string: " << endl << endl; //Generate publishing licence, so it can be used later to decrypt text. auto serializedPublishingLicense = publishingHandler->GetSerializedPublishingLicense(); //Use same PL to decrypt the encryptedText. auto cHandlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>(); auto cHandlerFuture = cHandlerPromise->get_future(); shared_ptr<ProtectionHandlerObserver> cHandlerObserver = std::make_shared<ProtectionHandlerObserver>(); //Create consumption settings using serialised publishing licence. mip::ProtectionHandler::ConsumptionSettings consumptionSettings = mip::ProtectionHandler::ConsumptionSettings(serializedPublishingLicense); engine->CreateProtectionHandlerForConsumptionAsync(consumptionSettings, cHandlerObserver, cHandlerPromise); auto consumptionHandler = cHandlerFuture.get(); //Use consumption handler to decrypt the text. std::vector<uint8_t> decryptedBuffer(static_cast<size_t>(encryptedText.size())); int64_t decryptedSize = consumptionHandler->DecryptBuffer( 0, &encryptedBuffer[0], static_cast<int64_t>(encryptedBuffer.size()), &decryptedBuffer[0], static_cast<int64_t>(decryptedBuffer.size()), true); decryptedBuffer.resize(static_cast<size_t>(decryptedSize)); std::string decryptedText(decryptedBuffer.begin(), decryptedBuffer.end()); // Output decrypted content. Should match original input text. cout << "Decrypted Text :" + decryptedText << endl;
Menjelang akhir
main()
menemukan blok matikan aplikasi yang dibuat di mulai cepat pertama dan tambahkan baris di bawah ini untuk merilis sumber daya handler:publishingHandler = nullptr; consumptionHandler = nullptr;
Ganti nilai tempat penampung dalam kode sumber yang Anda sebagai berikut, menggunakan konstanta string:
Placeholder Nilai <teks sampel> Teks sampel yang ingin Anda lindungi, misalnya: "cipher text"
.<Id Templat> Id Templat yang ingin Anda gunakan untuk melindungi teks. Misalnya: "bb7ed207-046a-4caf-9826-647cff56b990"
Membangun dan menguji aplikasi
Buat dan uji aplikasi klien Anda.
Gunakan Ctrl+Shift+B (Build Solution) untuk membangun aplikasi klien Anda. Jika Anda tidak memiliki kesalahan build, gunakan F5 (Mulai penelusuran kesalahan) untuk menjalankan aplikasi Anda.
Jika proyek Anda berhasil membangun dan berjalan, aplikasi meminta token akses, setiap kali SDK memanggil metode Anda
AcquireOAuth2Token()
. Seperti yang Anda lakukan sebelumnya di Mulai Cepat "Daftar templat perlindungan", jalankan skrip PowerShell Anda untuk memperoleh token setiap kali, menggunakan nilai yang disediakan untuk $authority dan $resourceUrl.*** Template List: Name: Confidential \ All Employees : a74f5027-f3e3-4c55-abcd-74c2ee41b607 Name: Highly Confidential \ All Employees : bb7ed207-046a-4caf-9826-647cff56b990 Name: Confidential : 174bc02a-6e22-4cf2-9309-cb3d47142b05 Name: Contoso Employees Only : 667466bf-a01b-4b0a-8bbf-a79a3d96f720 Applying Template ID bb7ed207-046a-4caf-9826-647cff56b990 to: <Sample-Text> Encrypted Text :y¬╩$Ops7Γ╢╖¢t Decrypting string: Run the PowerShell script to generate an access token using the following values, then copy/paste it below: Set $authority to: https://login.windows.net/common/oauth2/authorize Set $resourceUrl to: https://aadrm.com Sign in with user account: user1@tenant.onmicrosoft.com Enter access token: <paste-access-token-here> Press any key to continue . . . Run the PowerShell script to generate an access token using the following values, then copy/paste it below: Set $authority to: https://login.windows.net/94f69844-8d34-4794-bde4-3ac89ad2b664/oauth2/authorize Set $resourceUrl to: https://aadrm.com Sign in with user account: user1@tenant.onmicrosoft.com Enter access token: <paste-access-token-here> Press any key to continue . . . Decrypted Text :<Sample-Text> C:\MIP Sample Apps\ProtectionQS\Debug\ProtectionQS.exe (process 8252) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk