web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

RSA encryption is possible ? in business central

(1) ShareShare
ReportReport
Posted on by 2
Is anyone can help me with RSA encryption in BC (there is a codeunit 1475 "RSA")
i am unable implement the functionality perfectly . Can anyone help me out with a sample code how to intialize key into RSA encrypt
community question.jpg
I have the same question (1)
  • Suggested answer
    YUN ZHU Profile Picture
    95,739 Super User 2025 Season 2 on at
    Hi, I saw an example provided by a partner in the BC Yammer Group before, I hope it can give you some tips.
    namespace PC.RSASignging;
    
    using System.Utilities;
    using System.Security.Encryption;
    
    page 50102 SignTest
    {
        ApplicationArea = All;
        Caption = 'SignTest';
        PageType = Card;
        UsageCategory = Tasks;
        InsertAllowed = false;
        DeleteAllowed = false;
    
        layout
        {
            area(content)
            {
                group(General)
                {
                    Caption = 'General';
    
                    field(DataToSign; DataToSign)
                    {
                        Caption = 'Data To Sign';
                        ToolTip = 'Specifies the data to sign.';
                        Editable = false;
                    }
                    field(KeyPairCreated; IsKeyPairCreated())
                    {
                        Caption = 'Key Pair Created';
                        ToolTip = 'Specifies whether the key pair has been created.';
                        Editable = false;
                    }
                    field(SignatureCreated; Signature.HasValue())
                    {
                        Caption = 'Signature Created';
                        ToolTip = 'Specifies whether the signature has been created.';
                        Editable = false;
                    }
                }
            }
        }
        actions
        {
            area(Processing)
            {
                action(GenerateKeyPairAction)
                {
                    Caption = 'Generate Key Pair';
                    ToolTip = 'Generates a new key pair.';
                    Image = EncryptionKeys;
    
                    trigger OnAction()
                    begin
                        GenerateNewKeyPair();
                    end;
                }
                action(GenerateDataAction)
                {
                    Caption = 'Generate Data';
                    ToolTip = 'Generates data to sign.';
                    Image = New;
    
                    trigger OnAction()
                    begin
                        DataToSign := DelChr(CreateGuid(), '<>', '{}');
                        Clear(Signature);
                    end;
                }
                action(CreateSignatureAction)
                {
                    Caption = 'Create Signature';
                    ToolTip = 'Creates a signature for the data.';
                    Image = Signature;
    
                    trigger OnAction()
                    begin
                        CreateSignature();
                    end;
                }
                action(VerifySignatureAction)
                {
                    Caption = 'Verify Signature';
                    ToolTip = 'Verifies the signature for the data.';
                    Image = CheckRulesSyntax;
    
                    trigger OnAction()
                    begin
                        VerifySignature();
                    end;
                }
            }
            area(Promoted)
            {
                group(Category_Process)
                {
                    Caption = 'Process';
    
                    actionref(GenerateKeyPairAction_Promoted; GenerateKeyPairAction)
                    {
                    }
                    actionref(GenerateDataAction_Promoted; GenerateDataAction)
                    {
                    }
                    actionref(CreateSignatureAction_Promoted; CreateSignatureAction)
                    {
                    }
                    actionref(VerifySignatureAction_Promoted; VerifySignatureAction)
                    {
                    }
                }
            }
        }
    
        local procedure IsKeyPairCreated(): Boolean
        begin
            exit((not PrivateKey.IsEmpty()) and (PublicKey <> ''));
        end;
    
        local procedure GenerateNewKeyPair()
        begin
            RSACryptoServiceProvider.InitializeRSA(2048);
            PublicKey := RSACryptoServiceProvider.ToXmlString(true);
            PrivateKey := RSACryptoServiceProvider.ToSecretXmlString(true);
        end;
    
        local procedure CreateSignature()
        begin
            Signature.CreateOutStream(SignatureOutStream);
            CryptographyManagement.SignData(DataToSign, PrivateKey, GetDefaultHashAlgorithm(), SignatureOutStream);
        end;
    
        local procedure VerifySignature()
        var
            SignatureErr: Label 'No signature has been created.';
            VerifiedMsg: Label 'Signature verified.';
            NotVerifiedMsg: Label 'Signature not verified.';
            Verified: Boolean;
            Length: Integer;
        begin
            if not Signature.HasValue() then
                Error(SignatureErr);
    
            Signature.CreateInStream(SignatureInStream);
            Length := SignatureInStream.Length;
            if Length = 0 then
                Error(SignatureErr);
    
            Verified := CryptographyManagement.VerifyData(DataToSign, PublicKey, GetDefaultHashAlgorithm(), SignatureInStream);
            if Verified then
                Message(VerifiedMsg)
            else
                Message(NotVerifiedMsg);
        end;
    
        local procedure GetDefaultHashAlgorithm(): Enum "Hash Algorithm"
        begin
            exit(Enum::"Hash Algorithm"::SHA256);
        end;
    
        var
            CryptographyManagement: Codeunit "Cryptography Management";
            Signature: Codeunit "Temp Blob";
            RSACryptoServiceProvider: Codeunit RSACryptoServiceProvider;
            SignatureOutStream: OutStream;
            SignatureInStream: InStream;
            DataToSign: Text;
            PrivateKey: SecretText;
            PublicKey: Text;
    }
    
     
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,614 Super User 2025 Season 2 on at
    yes it seems possible. you can try with the code which Zhu suggested.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,135

#2
YUN ZHU Profile Picture

YUN ZHU 733 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 612

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans