pub fn decrypt<K: Serialize + DeserializeOwned>(
        &self,
        key: &jwk::JWK<K>,
        cek_alg: KeyManagementAlgorithm,
        enc_alg: ContentEncryptionAlgorithm,
    ) -> Result<Self, Error> {
        match *self {
            Compact::Encrypted(ref encrypted) => {
                if encrypted.len() != 5 {
                    Err(DecodeError::PartsLengthError {
                        actual: encrypted.len(),
                        expected: 5,
                    })?
                }
                // RFC 7516 Section 5.2 describes the steps involved in decryption.
                // Steps 1-3
                let mut header: Header<H> = encrypted.part(0)?;
                let encrypted_cek: Vec<u8> = encrypted.part(1)?;
                let nonce: Vec<u8> = encrypted.part(2)?;
                let encrypted_payload: Vec<u8> = encrypted.part(3)?;
                let tag: Vec<u8> = encrypted.part(4)?;

                // Verify that the algorithms are expected
                if header.registered.cek_algorithm != cek_alg
                    || header.registered.enc_algorithm != enc_alg
                {
                    Err(Error::ValidationError(
                        ValidationError::WrongAlgorithmHeader,
                    ))?;
                }

                // TODO: Steps 4-5 not implemented at the moment.

                // Steps 6-13 involve the computation of the cek
                let cek_encryption_result = header.extract_cek_encryption_result(&encrypted_cek);
                let cek = header.registered.cek_algorithm.unwrap_key(
                    &cek_encryption_result,
                    header.registered.enc_algorithm,
                    key,
                )?;

                // Build encryption result as per steps 14-15
                let protected_header: Vec<u8> = encrypted.part(0)?;
                let encoded_protected_header = BASE64URL_NOPAD.encode(protected_header.as_ref());
                let encrypted_payload_result = EncryptionResult {
                    nonce,
                    tag,
                    encrypted: encrypted_payload,
                    additional_data: encoded_protected_header.as_bytes().to_vec(),
                };

                let payload = header
                    .registered
                    .enc_algorithm
                    .decrypt(&encrypted_payload_result, &cek)?;

                // Decompression is not supported at the moment
                if header.registered.compression_algorithm.is_some() {
                    Err(Error::UnsupportedOperation)?
                }

                let payload = T::from_bytes(&payload)?;

                Ok(Compact::new_decrypted(header, payload))
            }
            Compact::Decrypted { .. } => Err(Error::UnsupportedOperation),
        }
    }
/// Decrypt an encrypted JWE. Provide the expected algorithms to mitigate an attacker modifying the
/// fields
Decrypt an encrypted JWE. Provide the expected algorithms to mitigate an attacker modifying the
fields
Decrypt an encrypted JWE . Provide the expected algorithms to mitigate an attacker modifying the fields
pub fn decrypt<K: Serialize + DeserializeOwned>(
        &self,
        key: &jwk::JWK<K>,
        cek_alg: KeyManagementAlgorithm,
        enc_alg: ContentEncryptionAlgorithm,
    ) -> Result<Self, Error> {
        match *self {
            Compact::Encrypted(ref encrypted) => {
                if encrypted.len() != 5 {
                    Err(DecodeError::PartsLengthError {
                        actual: encrypted.len(),
                        expected: 5,
                    })?
                }
                // RFC 7516 Section 5.2 describes the steps involved in decryption.
                // Steps 1-3
                let mut header: Header<H> = encrypted.part(0)?;
                let encrypted_cek: Vec<u8> = encrypted.part(1)?;
                let nonce: Vec<u8> = encrypted.part(2)?;
                let encrypted_payload: Vec<u8> = encrypted.part(3)?;
                let tag: Vec<u8> = encrypted.part(4)?;
                // Verify that the algorithms are expected
                if header.registered.cek_algorithm != cek_alg
                    || header.registered.enc_algorithm != enc_alg
                {
                    Err(Error::ValidationError(
                        ValidationError::WrongAlgorithmHeader,
                    ))?;
                }
                // TODO: Steps 4-5 not implemented at the moment.
                // Steps 6-13 involve the computation of the cek
                let cek_encryption_result = header.extract_cek_encryption_result(&encrypted_cek);
                let cek = header.registered.cek_algorithm.unwrap_key(
                    &cek_encryption_result,
                    header.registered.enc_algorithm,
                    key,
                )?;
                // Build encryption result as per steps 14-15
                let protected_header: Vec<u8> = encrypted.part(0)?;
                let encoded_protected_header = BASE64URL_NOPAD.encode(protected_header.as_ref());
                let encrypted_payload_result = EncryptionResult {
                    nonce,
                    tag,
                    encrypted: encrypted_payload,
                    additional_data: encoded_protected_header.as_bytes().to_vec(),
                };
                let payload = header
                    .registered
                    .enc_algorithm
                    .decrypt(&encrypted_payload_result, &cek)?;
                // Decompression is not supported at the moment
                if header.registered.compression_algorithm.is_some() {
                    Err(Error::UnsupportedOperation)?
                }
                let payload = T::from_bytes(&payload)?;
                Ok(Compact::new_decrypted(header, payload))
            }
            Compact::Decrypted { .. } => Err(Error::UnsupportedOperation),
        }
    }
pub fn decrypt < K : Serialize + DeserializeOwned > ( & self , key : & jwk :: JWK < K > , cek_alg : KeyManagementAlgorithm , enc_alg : ContentEncryptionAlgorithm , ) -> Result < Self , Error > { match * self { Compact :: Encrypted ( ref encrypted ) => { if encrypted . len ( ) != 5 { Err ( DecodeError :: PartsLengthError { actual : encrypted . len ( ) , expected : 5 , } ) ? } // RFC 7516 Section 5.2 describes the steps involved in decryption. // Steps 1-3 let mut header : Header < H > = encrypted . part ( 0 ) ? ; let encrypted_cek : Vec < u8 > = encrypted . part ( 1 ) ? ; let nonce : Vec < u8 > = encrypted . part ( 2 ) ? ; let encrypted_payload : Vec < u8 > = encrypted . part ( 3 ) ? ; let tag : Vec < u8 > = encrypted . part ( 4 ) ? ; // Verify that the algorithms are expected if header . registered . cek_algorithm != cek_alg || header . registered . enc_algorithm != enc_alg { Err ( Error :: ValidationError ( ValidationError :: WrongAlgorithmHeader , ) ) ? ; } // TODO: Steps 4-5 not implemented at the moment. // Steps 6-13 involve the computation of the cek let cek_encryption_result = header . extract_cek_encryption_result ( & encrypted_cek ) ; let cek = header . registered . cek_algorithm . unwrap_key ( & cek_encryption_result , header . registered . enc_algorithm , key , ) ? ; // Build encryption result as per steps 14-15 let protected_header : Vec < u8 > = encrypted . part ( 0 ) ? ; let encoded_protected_header = BASE64URL_NOPAD . encode ( protected_header . as_ref ( ) ) ; let encrypted_payload_result = EncryptionResult { nonce , tag , encrypted : encrypted_payload , additional_data : encoded_protected_header . as_bytes ( ) . to_vec ( ) , } ; let payload = header . registered . enc_algorithm . decrypt ( & encrypted_payload_result , & cek ) ? ; // Decompression is not supported at the moment if header . registered . compression_algorithm . is_some ( ) { Err ( Error :: UnsupportedOperation ) ? } let payload = T :: from_bytes ( & payload ) ? ; Ok ( Compact :: new_decrypted ( header , payload ) ) } Compact :: Decrypted { .. } => Err ( Error :: UnsupportedOperation ) , } }
Decrypt an encrypted JWE.
Decrypt an encrypted JWE .
&self
key
&jwk::JWK<K>
cek_alg
KeyManagementAlgorithm
enc_alg
ContentEncryptionAlgorithm
&self
key
&jwk::JWK<K>
cek_alg
KeyManagementAlgorithm
enc_alg
ContentEncryptionAlgorithm