📄 cipher.java
字号:
throw new IllegalArgumentException("IJCE does not support ciphers for which implPadding == true"); this.implBuffering = implBuffering; this.provider = provider; } /** * This constructor is identical to the previous one (with arguments * boolean, boolean, String), except that it does not have the redundant * <i>implPadding</i> parameter, and also allows the algorithm name * to be specified. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This constructor * is not supported in JavaSoft's version of JCE.</a></strong> * * @param implBuffering if true, this argument indicates that data * will always be passed from update/crypt to * engineUpdate/engineCrypt without modification. * @param provider the name of the provider of the underlying * cryptographic engine. * @param algorithm the name of this algorithm (optionally with * mode and padding, separated by '/'), as it is * configured in the properties file. */ protected Cipher(boolean implBuffering, String provider, String algorithm) { super("Cipher"); this.implBuffering = implBuffering; this.provider = provider; parseAlgorithm(algorithm); } private void parseAlgorithm(String algorithm) { int p = algorithm.indexOf('/'); if (p == -1) cipherName = algorithm; else { // "cipher/..." cipherName = algorithm.substring(0, p); int q = algorithm.indexOf('/', p+1); if (q == -1) // "cipher/mode" modeName = algorithm.substring(p+1); else { // "cipher/mode/padding" modeName = algorithm.substring(p+1, q); paddingName = algorithm.substring(q+1); } } } private void setNames(String cipherName, String modeName, String paddingName, String provider) { if (this.cipherName == null) this.cipherName = cipherName; if (this.modeName == null) this.modeName = modeName; if (this.paddingName == null) this.paddingName = paddingName; if (this.provider == null) this.provider = provider; } /** * Returns the object implementing padding for this cipher, or null if * there is no such object. */ protected final PaddingScheme getPaddingScheme() { return padding; } /** * Generates a Cipher object that implements the given algorithm. If the * default provider package contains a class implementing the algorithm, * an instance of that class is returned. If it is not available in the * default package, other packages are searched. * <p> * Any of the following formats can be used for the algorithm name: * <p> * <dl> * <dt> cipher "/" mode "/" padding * <dd> all algorithm components - cipher, mode, and padding - * are specified (these may be aliases). * <dt> cipher "/" mode * <dd> the padding is assumed to be "NONE". * <dt> cipher * <dd> the mode is assumed to be "ECB", and the padding to be "NONE". * </dl> * <p> * See <a href="../guide/ijce/Algorithms.html#Cipher"> * <cite>International JCE Standard Algorithm Names</cite></a> for a list * of Cipher algorithm names. * * @param algorithm the algorithm name, as described above. * @return the new Cipher object, in the UNINITIALIZED state. * @exception NoSuchAlgorithmException if the algorithm is * not available from any provider. */ public static Cipher getInstance(String algorithm) throws NoSuchAlgorithmException { try { return getInstance(algorithm, null); } catch (NoSuchProviderException e) { throw new NoSuchAlgorithmException(e.getMessage()); } } /** * Generates a Cipher object that implements the given cipher, from * the given provider. The format of the cipher specification is as * described for <code>getInstance(String algorithm)</code>. * <p> * If the algorithm is implemented using more than one component * (cipher, mode, and padding scheme), all of them must come from the * given provider. More flexibility can be obtained by using the * <code>getInstance(Cipher, Mode, PaddingScheme)</code> factory method. * For example, to request "DES" from the SUN provider, "CBC" from the * Cryptix provider, and "PKCS#5" from any provider, use: * <pre> * Cipher.getInstance( * Cipher.getInstance("DES", "SUN"), * Mode.getInstance("CBC", "Cryptix"), * PaddingScheme.getInstance("PKCS#5") * ) * </pre> * <p> * See <a href="../guide/ijce/Algorithms.html#Cipher"> * <cite>International JCE Standard Algorithm Names</cite></a> for a list * of Cipher algorithm names. * * @param algorithm the cipher specification. * @return the new Cipher object, in the UNINITIALIZED state. * @exception NoSuchAlgorithmException if the algorithm is not * available from the provider. * @exception NoSuchProviderException if the provider is not * available in the environment. */ public static Cipher getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { if (algorithm == null) throw new NullPointerException("algorithm == null"); // parse the algorithm spec String cipherName = algorithm; String modeName = "ECB"; String paddingName = "NONE"; int p = algorithm.indexOf('/'); if (p != -1) { // "cipher/..." cipherName = algorithm.substring(0, p); int q = algorithm.indexOf('/', p+1); if (q == -1) // "cipher/mode" modeName = algorithm.substring(p+1); else { // "cipher/mode/padding" modeName = algorithm.substring(p+1, q); paddingName = algorithm.substring(q+1); } } return getInstance(cipherName, modeName, paddingName, provider); } /** * Generates a Cipher object implementing the specified algorithm, mode, * and padding from the specified provider. * * @param algorithmName the string name of the algorithm. * @param modeName the string name of the mode. * @param paddingName the string name of the padding scheme. * @return the new Cipher object, in the UNINITIALIZED state. * @exception NoSuchAlgorithmException if the algorithm is not * available from the provider. * @exception NoSuchProviderException if the provider is not * available in the environment. */ private static Cipher getInstance(String cipherName, String modeName, String paddingName, String provider) throws NoSuchAlgorithmException, NoSuchProviderException {if (DEBUG && debuglevel >= 3) debug("Entered getInstance(\"" + cipherName + "\", \"" + modeName + "\", \"" + paddingName + "\", \"" + provider + "\")"); // Note: we use IJCE.getImplementation, not Security.getImpl, below, // because Security.getImpl is not guaranteed to exist in all // implementations of JCA. cipherName = IJCE.getStandardName(cipherName, "Cipher"); modeName = IJCE.getStandardName(modeName, "Mode"); paddingName = IJCE.getStandardName(paddingName, "PaddingScheme"); Cipher result, nested = null; PaddingScheme padding = null; try { // first check for a combined "cipher/mode/padding". result = (Cipher) (IJCE.getImplementation(cipherName + "/" + modeName + "/" + paddingName, provider, "Cipher")); } catch (NoSuchAlgorithmException e) { if (modeName.equals("ECB")) result = (Cipher) (IJCE.getImplementation(cipherName, provider, "Cipher")); else { try { // check for "cipher/mode". result = (Cipher) (IJCE.getImplementation(cipherName + "/" + modeName, provider, "Cipher")); } catch (NoSuchAlgorithmException e2) { // otherwise, the object returned to the user will be a // subclass of Mode. nested = (Cipher) (IJCE.getImplementation(cipherName, provider, "Cipher")); nested.setNames(cipherName, "ECB", "NONE", provider); result = (Cipher) (IJCE.getImplementation(modeName, provider, "Mode")); } } if (!paddingName.equals("NONE")) { padding = (PaddingScheme) (IJCE.getImplementation(paddingName, provider, "PaddingScheme")); } } result.setNames(cipherName, modeName, paddingName, provider); // We used to set the input block size for the padding scheme here, but // it might not be valid until the cipher is initialized, so set it in // initEncrypt/Decrypt instead. if (nested != null) ((Mode) result).engineSetCipher(nested); if (padding != null) result.engineSetPaddingScheme(padding);if (DEBUG && debuglevel >= 3) debug("Created cipher [1]: " + result); return result; } /** * Generates a new Cipher object by composing the given Cipher, Mode and * PaddingScheme objects. <i>mode</i> may be null (indicating ECB), * and <i>padding</i> may be null (indicating NONE). * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @exception NullPointerException if cipher == null */ public static Cipher getInstance(Cipher cipher, Mode mode, PaddingScheme padding) { if (cipher == null) throw new NullPointerException("cipher == null"); String cipherName = cipher.getAlgorithm(); String modeName = (mode == null) ? "ECB" : mode.getAlgorithm(); String paddingName = (padding == null) ? "NONE" : padding.getAlgorithm(); String provider = cipher.getProvider(); Cipher result, nested = null; if (mode == null) result = cipher; else { nested = cipher; result = mode; } result.setNames(cipherName, modeName, paddingName, provider); if (nested != null)((Mode) result).engineSetCipher(nested); if (padding != null) result.engineSetPaddingScheme(padding);if (DEBUG && debuglevel >= 3) debug("Created cipher [2]: " + result); return result; } /** * Returns the state of this Cipher object. Possible states are: * <p> * <dl> * <dt> UNINITIALIZED * <dd> The cipher has not been initialized. * <dt> ENCRYPT * <dd> The cipher has been initialized for encryption. It may be * used for encryption only. * <dt> DECRYPT * <dd> The cipher has been initialized for decryption. It may be * used for decryption only. * </dl> * * @return the state of this cipher object. * * @see #UNINITIALIZED * @see #ENCRYPT * @see #DECRYPT */ public final int getState() { return state; } /** * Returns this algorithm's standard cipher name (<em>not</em> including * mode and padding). * <p> * See <a href="../guide/ijce/Algorithms.html#Cipher"> * <cite>International JCE Standard Algorithm Names</cite></a> for a list * of Cipher algorithm names. * * @return the standard cipher name (such as "DES"). */ public final String getAlgorithm() { return cipherName; } /** * Returns this algorithm's standard mode name. * <p> * See <a href="../guide/ijce/Algorithms.html#Mode"> * <cite>International JCE Standard Algorithm Names</cite></a> for a list * of Mode algorithm names. * * @return the algorithm's standard mode name (such as "CBC") */ public final String getMode() { return (modeName == null) ? "ECB" : modeName; } /** * Returns this algorithm's standard padding scheme name.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -