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; }
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Rishabh Kanaskar 2,568
Sumit Singh 2,536
YUN ZHU 2,015 Super User 2025 Season 2