⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crypt.java

📁 一个java开发的非常全面的关于证书发放
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }

            cStr = new CipherOutputStream(daos, cipher);

            // first, write the IV to the file
            if (iv != null)
                daos.write(iv);

            // Read input bytes into buffer and run them through the
            // cipher stream.
            byte[] buffer = new byte[bufferlength];
            int length = 0;
            while ((length = is.read(buffer)) != -1)
                cStr.write(buffer, 0, length);

        } catch (IOException ioe) {
            ioe.printStackTrace();
            throw new IOException(ioe.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new CryptoException(ex.getMessage());
        } finally {
            if (cStr != null) {
                try {
                    cStr.close();
                } catch (IOException ioe) {
                    ;
                }
            }
        }
    }

    /**
     * Decrypts a message with a symmetric key
     *
     * @param text the message to decrypt in BASE64 format
     * @param keyfile keyfile(name)
     * @param passphrase the passphrase for the keystore
     * @param algorithm encryption algorithm (e.g. "Rijndael")
     * @param mode encryption mode (e.g. "CBC")
     * @param padding padding scheme (e.g."PKCS7Padding")
     * @return deciphered message
     * @throws CryptoException for encryption errors
     * @throws KeystoreException when keystore could not be loaded
     */
    public static StringBuffer decrypt(StringBuffer text
                                       , String keyfile
                                       , StringBuffer passphrase
                                       , String algorithm
                                       , String mode
                                       , String padding) throws CryptoException, KeystoreException {

        // read secret key
        Key secretKey = Keystore.loadKey(algorithm, keyfile, passphrase);

        return decrypt(text, secretKey, algorithm, mode, padding);
    }

    /**
     * Decrypts a message with a symmetric key
     *
     * @param text the message to decrypt in BASE64 format
     * @param secretKey the secret key
     * @param algorithm encryption algorithm (e.g. "Rijndael")
     * @param mode encryption mode (e.g. "CBC")
     * @param padding padding scheme (e.g."PKCS7Padding")
     * @return deciphered message
     * @throws CryptoException for encryption errors
     */
    public static StringBuffer decrypt(StringBuffer text
                                       , Key secretKey
                                       , String algorithm
                                       , String mode
                                       , String padding) throws CryptoException {

        ByteArrayOutputStream bao = null;
        DataOutputStream dao = null;

        try {
            bao = new ByteArrayOutputStream();
            dao = new DataOutputStream(bao);

            // decrypt
            decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, secretKey, algorithm, mode, padding, BUFFERSIZE_TEXT);

            return new StringBuffer(new String(bao.toByteArray()));

        } catch (IOException ioe) {
            ioe.printStackTrace();
            throw new CryptoException(ioe.getMessage());
        } finally {
            if (dao != null) {
                // close outputstream
                try {
                    dao.close();
                } catch (IOException e) {
                    ;
                }
            }
        }
    }

    /**
     * Decrypts a file with a symmetric key
     *
     * @param file the file to decrypt
     * @param newfile the deciphered file
     * @param keyfile keyfile(name)
     * @param passphrase the passphrase for the keystore
     * @param algorithm encryption algorithm (e.g. "Rijndael")
     * @param mode encryption mode (e.g. "CBC")
     * @param padding padding scheme (e.g."PKCS7Padding")
     * @throws CryptoException encryption errors
     * @throws IOException I/O errors
     */
    public static void decryptFile(String file
                                   , String newfile
                                   , String keyfile
                                   , StringBuffer passphrase
                                   , String algorithm
                                   , String mode
                                   , String padding) throws CryptoException, KeystoreException, IOException {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        DataOutputStream dao = null;

        try {
            fis = new FileInputStream(file);

            fos = new FileOutputStream(newfile);
            dao = new DataOutputStream(fos);

            // read secret key
            Key secretKey = Keystore.loadKey(algorithm, keyfile, passphrase);

            // decrypt file
            decrypt(fis, dao, secretKey, algorithm, mode, padding, BUFFERSIZE_FILE);

        } catch (IOException ioe) {
            ioe.printStackTrace();
            throw new IOException(ioe.getMessage());
        } finally {
            if (dao != null) {
                // close outputstream
                try {
                    dao.close();
                } catch (IOException e) {
                    ;
                }
            }
            if (fis != null) {
                // close inputstream
                try {
                    fis.close();
                } catch (IOException e) {
                    ;
                }
            }
        }
    }

    /**
     * Decrypts any inputstream with a symmetric key
     *
     * @param is the inputstream to decrypt
     * @param daos the deciphered outputstream
     * @param secretKey the secret key
     * @param algorithm String encryption algorithm (e.g. "Rijndael")
     * @param mode String encryption mode (e.g. "CBC")
     * @param padding String padding scheme (e.g."PKCS7Padding")
     * @param bufferlength buffer length in bytes
     * @exception CryptoException for encryption errors
     * @exception IOException I/O errors
     **/
    public static void decrypt(InputStream is
                               , DataOutputStream daos
                               , Key secretKey
                               , String algorithm
                               , String mode
                               , String padding
                               , int bufferlength)
            throws CryptoException, IOException {

        Cipher cipher = null;
        IvParameterSpec spec = null; // initialization vector
        byte[] iv = null; // initialization vector as byte[]
        //Key secretKey = null;
        CipherInputStream cStr = null;

        try {
            Security.addProvider(new BouncyCastleProvider());

            if (algorithm.equalsIgnoreCase("RC4")) {
                // create a stream cipher object (ignore mode & padding)
                cipher = Cipher.getInstance("RC4");
            } else {
                // create a block cipher object: ("algorithm/mode/padding", provider)
                cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + padding, "BC");
            }

            if (mode.equalsIgnoreCase("ECB") || algorithm.equalsIgnoreCase("RC4")) {
                cipher.init(Cipher.DECRYPT_MODE, secretKey);
            } else {
                // read IV.
                iv = new byte[cipher.getBlockSize()];
                is.read(iv);

                spec = new IvParameterSpec(iv);
                cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
            }

            cStr = new CipherInputStream(is, cipher);

            // Read bytes and run them through cipher and store them to
            // file back again.
            byte[] buffer = new byte[bufferlength];
            int length = 0;
            while ((length = cStr.read(buffer)) != -1)
                daos.write(buffer, 0, length);

        } catch (IOException ioe) {
            ioe.printStackTrace();
            throw new IOException(ioe.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new CryptoException(ex.getMessage());
        } finally {
            if (cStr != null) {
                try {
                    cStr.close();
                } catch (IOException ioe) {
                    ;
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -