Sample code for 30+ languages & platforms
DataFlex

RSA Signature using Private Key from .snk File

See more RSA Examples

A .snk file (Strong Name Key file) is a file format used in the .NET ecosystem to store an RSA public/private key pair. This key pair is typically used for strong-naming assemblies, which is a process of signing .NET assemblies to ensure their integrity and to uniquely identify them.

This example loads a private key from a .snk file and creates an RSA signature.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRsa
    String sXmlStr
    Variant vPrivKey
    Handle hoPrivKey
    String sSigBase64
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatRsa)) To hoRsa
    If (Not(IsComObjectCreated(hoRsa))) Begin
        Send CreateComObject of hoRsa
    End

    // Load the .snk and return the private key as XML.
    Get ComSnkToXml Of hoRsa "./test.snk" To sXmlStr
    Get ComLastMethodSuccess Of hoRsa To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get ComLoadXml Of hoPrivKey sXmlStr To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoPrivKey to vPrivKey
    Get ComUsePrivateKey Of hoRsa vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Sign the SHA-256 hash of the utf-8 byte representation of the contents of sb
    // Return the signature in base64 format.
    Set ComEncodingMode Of hoRsa To "base64"
    Set ComCharset Of hoRsa To "utf-8"
    Get ComSignStringENC Of hoRsa "This is the text to be hashed and signed." "sha256" To sSigBase64
    Get ComLastMethodSuccess Of hoRsa To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "RSA signature as base64: " sSigBase64


End_Procedure