Sample code for 30+ languages & platforms
Go Requires Chilkat v11.7.0+

Choose the PDF Encryption Algorithm: AES-256, AES-128 or RC4

See more PDF Protection Examples

Demonstrates the algorithmChoice option of the encrypt operation. aes-256 is the default and the one to use unless an older reader must open the file. aes-128 is for readers that predate AES-256. rc4-128 and rc4-40 are broken ciphers kept only for very old readers. Each algorithm needs a minimum PDF version, and the file header is raised to it when the PDF is saved, never lowered. A PDF saved with RC4 is written with a classic cross-reference table so that those old readers can parse it.

Chilkat Go Downloads

Go
    success := false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  The PDF used in this example is available at two_page_memo.pdf

    json := chilkat.NewJsonObject()
    jsonOut := chilkat.NewJsonObject()
    info := chilkat.NewJsonObject()

    //  AES-256, the default.  Saved as PDF 1.7 with Adobe extension level 8.
    pdf1 := chilkat.NewPdf()
    success = pdf1.LoadFile("qa_data/pdf/two_page_memo.pdf")
    if success == false {
        fmt.Println(pdf1.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        return
    }

    pdf1.SetUserPassword("user-secret")
    pdf1.SetOwnerPassword("owner-secret")
    json.UpdateString("algorithmChoice","aes-256")
    success = pdf1.Protection("encrypt",json,jsonOut)
    if success == false {
        fmt.Println(pdf1.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        return
    }

    fmt.Println("aes-256 will be saved as PDF ", *jsonOut.StringOf("pending.pdfVersion"), ", Adobe extension level ", jsonOut.IntOf("pending.adobeExtensionLevel"))
    success = pdf1.WriteFile("qa_output/memo_aes256.pdf")
    if success == false {
        fmt.Println(pdf1.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        return
    }

    success = pdf1.PreLoadCheck("qa_output/memo_aes256.pdf",info)
    if success == false {
        fmt.Println(pdf1.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        return
    }

    fmt.Println("  saved file: ", *info.StringOf("algorithm"), ", PDF ", *info.StringOf("pdfVersion"))

    //  AES-128, for readers that predate AES-256.  Saved as PDF 1.6.
    pdf2 := chilkat.NewPdf()
    success = pdf2.LoadFile("qa_data/pdf/two_page_memo.pdf")
    if success == false {
        fmt.Println(pdf2.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        return
    }

    pdf2.SetUserPassword("user-secret")
    pdf2.SetOwnerPassword("owner-secret")
    json.UpdateString("algorithmChoice","aes-128")
    success = pdf2.Protection("encrypt",json,jsonOut)
    if success == false {
        fmt.Println(pdf2.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        return
    }

    fmt.Println("aes-128 will be saved as PDF ", *jsonOut.StringOf("pending.pdfVersion"))
    success = pdf2.WriteFile("qa_output/memo_aes128.pdf")
    if success == false {
        fmt.Println(pdf2.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        return
    }

    success = pdf2.PreLoadCheck("qa_output/memo_aes128.pdf",info)
    if success == false {
        fmt.Println(pdf2.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        return
    }

    fmt.Println("  saved file: ", *info.StringOf("algorithm"), ", PDF ", *info.StringOf("pdfVersion"))

    //  RC4-128, a broken cipher, only for readers too old for AES.  Saved as PDF 1.4.
    pdf3 := chilkat.NewPdf()
    success = pdf3.LoadFile("qa_data/pdf/two_page_memo.pdf")
    if success == false {
        fmt.Println(pdf3.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        pdf3.DisposePdf()
        return
    }

    pdf3.SetUserPassword("user-secret")
    pdf3.SetOwnerPassword("owner-secret")
    json.UpdateString("algorithmChoice","rc4-128")
    success = pdf3.Protection("encrypt",json,jsonOut)
    if success == false {
        fmt.Println(pdf3.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        pdf3.DisposePdf()
        return
    }

    fmt.Println("rc4-128 will be saved as PDF ", *jsonOut.StringOf("pending.pdfVersion"))
    success = pdf3.WriteFile("qa_output/memo_rc4_128.pdf")
    if success == false {
        fmt.Println(pdf3.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        pdf3.DisposePdf()
        return
    }

    success = pdf3.PreLoadCheck("qa_output/memo_rc4_128.pdf",info)
    if success == false {
        fmt.Println(pdf3.LastErrorText())
        json.DisposeJsonObject()
        jsonOut.DisposeJsonObject()
        info.DisposeJsonObject()
        pdf1.DisposePdf()
        pdf2.DisposePdf()
        pdf3.DisposePdf()
        return
    }

    fmt.Println("  saved file: ", *info.StringOf("algorithm"), ", PDF ", *info.StringOf("pdfVersion"))

    //  Sample output:

    //  aes-256 will be saved as PDF 1.7, Adobe extension level 8
    //    saved file: aes-256, PDF 1.7
    //  aes-128 will be saved as PDF 1.6
    //    saved file: aes-128, PDF 1.6
    //  rc4-128 will be saved as PDF 1.4
    //    saved file: rc4-128, PDF 1.4

    json.DisposeJsonObject()
    jsonOut.DisposeJsonObject()
    info.DisposeJsonObject()
    pdf1.DisposePdf()
    pdf2.DisposePdf()
    pdf3.DisposePdf()