content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.shade.safety.provider;
  
  import java.io.*;
  
  import javax.crypto.Cipher;
  import javax.crypto.CipherInputStream;
  import javax.crypto.CipherOutputStream;
  import javax.crypto.spec.SecretKeySpec;
  
  import org.miaixz.bus.shade.safety.Builder;
  import org.miaixz.bus.shade.safety.algorithm.Key;
  
  /**
   * JDK内置加密算法的加密器
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  public class JdkEncryptorProvider implements EncryptorProvider {
  
      private final String algorithm;
  
      public JdkEncryptorProvider(String algorithm) {
          this.algorithm = algorithm;
      }
  
      @Override
      public void encrypt(Key key, File src, File dest) throws IOException {
          if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) {
              throw new IOException("could not make directory: " + dest.getParentFile());
          }
          try (InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)) {
              encrypt(key, in, out);
          }
      }
  
      @Override
      public void encrypt(Key key, InputStream in, OutputStream out) throws IOException {
          CipherInputStream cis = null;
          try {
              Cipher cipher = Cipher.getInstance(algorithm);
              cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getEncryptKey(), algorithm));
              cis = new CipherInputStream(in, cipher);
              Builder.transfer(cis, out);
          } catch (Exception e) {
              throw new IOException(e);
          } finally {
              Builder.close(cis);
          }
      }
  
      @Override
      public InputStream encrypt(Key key, InputStream in) throws IOException {
          try {
              Cipher cipher = Cipher.getInstance(algorithm);
              cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getEncryptKey(), algorithm));
              return new CipherInputStream(in, cipher);
          } catch (Exception e) {
              throw new IOException(e);
          }
      }
  
      @Override
      public OutputStream encrypt(Key key, OutputStream out) throws IOException {
          try {
              Cipher cipher = Cipher.getInstance(algorithm);
              cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getEncryptKey(), algorithm));
              return new CipherOutputStream(out, cipher);
          } catch (Exception e) {
              throw new IOException(e);
          }
      }
  
  }
  