xmlenc: add support for decryption

This commit is contained in:
Ross Kinder
2015-11-29 21:40:07 -05:00
parent 83593a6168
commit 84a6b8b42b
3 changed files with 233 additions and 466 deletions

View File

@@ -24,45 +24,9 @@ A (partial) wrapper for [xmlsec](https://www.aleksey.com/xmlsec).
os.Exit(1)
}
# Encryption (xmlenc)
## Decrypting Example
## Encryption Example
ctx := xmlenc.Context{}
cert, _ := ioutil.ReadFile("saml.cert.pem")
err := ctx.AddCert(cert)
tmplDoc := []byte(``<?xml version="1.0" encoding="UTF-8"?>
<!--
XML Security Library example: Original XML doc file before encryption (encrypt3 example).
-->
<Envelope xmlns="urn:envelope">
<Data>
Hello, World!
</Data>
<xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Element">
<xenc:EncryptionMethod xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<!--<ds:KeyName>aes</ds:KeyName>-->
<xenc:EncryptedKey Id="aes" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
<ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
</xenc:EncryptionMethod>
<xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:CipherValue></xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedKey>
</ds:KeyInfo>
<xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:CipherValue></xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</Envelope>`)
ciphertext, err := ctx.Encrypt(docStr, []byte("Hello, World!"))
## Decryption Example
ctx := xmlenc.Context{}
key, _ := ioutil.ReadFile("saml.key.pem")
err := ctx.AddKey(key)
plaintext, err := ctx.Decrypt(ciphertext)
key, _ := ioutil.ReadFile("saml.key")
doc, _ := ioutil.ReadAll(os.Stdin)
plaintextDoc, err := xmlenc.Decrypt(key, doc)
os.Stdout.Write(plaintextDoc)