encryptData() public method

Encrypts the given data (string) using the regarding php-extension, depending on the library assigned to algorithm in the contructor.
public encryptData ( string $data ) : mixed | string
$data string
return mixed | string
Example #1
0
 /**
  * Encrypt the XMLSecurityKey
  *
  * @param XMLSecurityKey $srcKey
  * @param XMLSecurityKey $rawKey
  * @param bool $append
  * @throws Exception
  */
 public function encryptKey($srcKey, $rawKey, $append = true)
 {
     if (!$srcKey instanceof XMLSecurityKey || !$rawKey instanceof XMLSecurityKey) {
         throw new Exception('Invalid Key');
     }
     $strEncKey = base64_encode($srcKey->encryptData($rawKey->key));
     $root = $this->encdoc->documentElement;
     $encKey = $this->encdoc->createElementNS(self::XMLENCNS, 'xenc:EncryptedKey');
     if ($append) {
         $keyInfo = $root->insertBefore($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyInfo'), $root->firstChild);
         $keyInfo->appendChild($encKey);
     } else {
         $this->encKey = $encKey;
     }
     $encMethod = $encKey->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:EncryptionMethod'));
     $encMethod->setAttribute('Algorithm', $srcKey->getAlgorith());
     if (!empty($srcKey->name)) {
         $keyInfo = $encKey->appendChild($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyInfo'));
         $keyInfo->appendChild($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyName', $srcKey->name));
     }
     $cipherData = $encKey->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:CipherData'));
     $cipherData->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:CipherValue', $strEncKey));
     if (is_array($this->references) && count($this->references) > 0) {
         $refList = $encKey->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:ReferenceList'));
         foreach ($this->references as $name => $reference) {
             $refuri = $reference["refuri"];
             $dataRef = $refList->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:DataReference'));
             $dataRef->setAttribute("URI", '#' . $refuri);
         }
     }
     return;
 }
Example #2
0
 /**
  * Encrypt the selected node with the given key.
  *
  * @param XMLSecurityKey $objKey  The encryption key and algorithm.
  * @param bool           $replace Whether the encrypted node should be replaced in the original tree. Default is true.
  *
  * @return DOMElement  The <xenc:EncryptedData>-element.
  */
 public function encryptNode($objKey, $replace = true)
 {
     $data = '';
     if (empty($this->rawNode)) {
         throw new Exception('Node to encrypt has not been set');
     }
     if (!$objKey instanceof XMLSecurityKey) {
         throw new Exception('Invalid Key');
     }
     $doc = $this->rawNode->ownerDocument;
     $xPath = new DOMXPath($this->encdoc);
     $objList = $xPath->query('/xenc:EncryptedData/xenc:CipherData/xenc:CipherValue');
     $cipherValue = $objList->item(0);
     if ($cipherValue == null) {
         throw new Exception('Error locating CipherValue element within template');
     }
     switch ($this->type) {
         case self::Element:
             $data = $doc->saveXML($this->rawNode);
             $this->encdoc->documentElement->setAttribute('Type', self::Element);
             break;
         case self::Content:
             $children = $this->rawNode->childNodes;
             foreach ($children as $child) {
                 $data .= $doc->saveXML($child);
             }
             $this->encdoc->documentElement->setAttribute('Type', self::Content);
             break;
         default:
             throw new Exception('Type is currently not supported');
     }
     $encMethod = $this->encdoc->documentElement->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:EncryptionMethod'));
     $encMethod->setAttribute('Algorithm', $objKey->getAlgorith());
     $cipherValue->parentNode->parentNode->insertBefore($encMethod, $cipherValue->parentNode->parentNode->firstChild);
     $strEncrypt = base64_encode($objKey->encryptData($data));
     $value = $this->encdoc->createTextNode($strEncrypt);
     $cipherValue->appendChild($value);
     if ($replace) {
         switch ($this->type) {
             case self::Element:
                 if ($this->rawNode->nodeType == XML_DOCUMENT_NODE) {
                     return $this->encdoc;
                 }
                 $importEnc = $this->rawNode->ownerDocument->importNode($this->encdoc->documentElement, true);
                 $this->rawNode->parentNode->replaceChild($importEnc, $this->rawNode);
                 return $importEnc;
             case self::Content:
                 $importEnc = $this->rawNode->ownerDocument->importNode($this->encdoc->documentElement, true);
                 while ($this->rawNode->firstChild) {
                     $this->rawNode->removeChild($this->rawNode->firstChild);
                 }
                 $this->rawNode->appendChild($importEnc);
                 return $importEnc;
         }
     } else {
         return $this->encdoc->documentElement;
     }
 }