Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Unable to Sign Data Using RS256 Encryption Algorithm with our own Key.

Posted on by 6
Hi,
 
We have a requirement to create JWT Token using RS256 Encryption algorithm with our key on On-Cloud Environment.
We have tried to do this using the Codeunit RSACryptoServiceProvider, but as far as we could understand, it does not provide the option to use our own key, instead, it initializes its own key. 
We are unable to create an instance of RSACryptoServiceProvider with our key. Is there any option to use our key instead of initializing a new one?
Can anyone assist here?
 
Regards
Vishal Mittal
Categories:
  • VP-26091629-0 Profile Picture
    VP-26091629-0 4 on at
    Unable to Sign Data Using RS256 Encryption Algorithm with our own Key.
    I am running this test code and it is causing the session to crash. Is it known bug?
     
     
    pageextension 70100 CustomerListExt extends "Customer List"
    {
        actions
        {
            addlast(Processing)
            {
                action(RSATest)
                {
                    ApplicationArea = All;
     
                    trigger OnAction()
                    var
                        RSAKey: Text;
                    begin
                        RSAKey := '<RSAKeyValue><Modulus>secrettext</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>AwIDAQAB</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>';
                        Message(RSAEncrypt('TestTextToEncrypt', RSAKey));
                    end;
                }
     
            }
        }
     
        procedure RSAEncrypt(TextToEncrypt: Text; RsaXmlKey: SecretText): Text
        var
            RSACryptoProvider: Codeunit RSACryptoServiceProvider;
            Base64Convert: Codeunit "Base64 Convert";
            TempBlob: Codeunit "Temp Blob";
            PlainTextOutStream: OutStream;
            EncryptedTextInStream: InStream;
            EncryptedTextOutStream: OutStream;
            EncryptedTextBase64: Text;
        begin
            // Convert the text to an OutStream
            TempBlob.CreateOutStream(PlainTextOutStream, TEXTENCODING::UTF8);
            PlainTextOutStream.WriteText(TextToEncrypt);
     
            // Reset the TempBlob and prepare InStream for encryption
            TempBlob.CreateInStream(EncryptedTextInStream, TEXTENCODING::UTF8);
     
            // Prepare OutStream for receiving encrypted data
            TempBlob.CreateOutStream(EncryptedTextOutStream);
     
            // Encrypt the text
            RSACryptoProvider.Encrypt(RsaXmlKey, EncryptedTextInStream, true, EncryptedTextOutStream);
     
            // Reset the TempBlob and create an InStream to read the encrypted data
            TempBlob.CreateInStream(EncryptedTextInStream);
     
            // Convert the encrypted data to Base64
            EncryptedTextBase64 := Base64Convert.ToBase64(EncryptedTextInStream);
     
            exit(EncryptedTextBase64);
        end;
    }
  • VM-14050617-0 Profile Picture
    VM-14050617-0 6 on at
    Unable to Sign Data Using RS256 Encryption Algorithm with our own Key.
    Hi @gdrenteria
     
    Thank you for your quick response. I forgot to mention that we are doing it On-Cloud environment. Solution suggested by you will work only for On-Prem environment.
     
    Regards
    Vishal Mittal
     
  • Suggested answer
    gdrenteria Profile Picture
    gdrenteria 12,167 Most Valuable Professional on at
    Unable to Sign Data Using RS256 Encryption Algorithm with our own Key.
    Hi
    I hope I understood correctly, I hope this can help you or give you some ideas.
    BR
    GR

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans