Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkRsa.pb"

Procedure ChilkatExample()

    success.i = 0

    rsa.i = CkRsa::ckCreate()
    If rsa.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Load the .snk and return the private key as XML.
    xmlStr.s = CkRsa::ckSnkToXml(rsa,"./test.snk")
    If CkRsa::ckLastMethodSuccess(rsa) = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkRsa::ckDispose(rsa)
        ProcedureReturn
    EndIf

    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckLoadXml(privKey,xmlStr)
    If success = 0
        Debug CkPrivateKey::ckLastErrorText(privKey)
        CkRsa::ckDispose(rsa)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    success = CkRsa::ckUsePrivateKey(rsa,privKey)
    If success = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkRsa::ckDispose(rsa)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    ; Sign the SHA-256 hash of the utf-8 byte representation of the contents of sb
    ; Return the signature in base64 format.
    CkRsa::setCkEncodingMode(rsa, "base64")
    CkRsa::setCkCharset(rsa, "utf-8")
    sigBase64.s = CkRsa::ckSignStringENC(rsa,"This is the text to be hashed and signed.","sha256")
    If CkRsa::ckLastMethodSuccess(rsa) = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkRsa::ckDispose(rsa)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    Debug "RSA signature as base64: " + sigBase64


    CkRsa::ckDispose(rsa)
    CkPrivateKey::ckDispose(privKey)


    ProcedureReturn
EndProcedure