Cipher類提供加密和解密的功能。
實(shí)例化
Cipher沒(méi)有公開(kāi)的構(gòu)造方法,所以只能調(diào)用其靜態(tài)方法getInstace進(jìn)行實(shí)現(xiàn)化。這個(gè)方法有多個(gè)重載如下:
public static final Cipher getInstance(String algorithm)
throws NoSuchAlgorithmException, NoSuchPaddingException;
public static final Cipher getInstance(String algorithm, String provider )
throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException;
public static final Cipher getInstance(String algorithm, Provider provider )
throws NoSuchAlgorithmException, NoSuchPaddingException
我們通常使用的是public static final Cipher getInstance(String algorithm);此方法需要一個(gè)字符串作為參數(shù),這個(gè)參數(shù)有以下兩種形式:
- "算法":使用
Provider給定的默認(rèn)“模式”和“填充”。 - "算法/模式/填充"
注:使用 CFB 和 OFB 之類的模式,Cipher 塊可以加密單元中小于該 Cipher 的實(shí)際塊大小的數(shù)據(jù)。請(qǐng)求這樣一個(gè)模式時(shí),可以指定一次處理的位數(shù)(可選):將此數(shù)添加到模式名稱中,正如 "DES/CFB8/NoPadding" 和 "DES/OFB32/PKCS5Padding" 轉(zhuǎn)換所示。如果未指定該數(shù),則將使用特定于提供者的默認(rèn)值。(例如,SunJCE 提供者對(duì) DES 使用默認(rèn)的 64 位)。因此,通過(guò)使用如 CFB8 或 OFB8 的 8 位模式,Cipher 塊可以被轉(zhuǎn)換為面向字節(jié)的 Cipher 流。
需要了解的靜態(tài)字段
public static final int ENCRYPT_MODE //用于將 Cipher 初始化為加密模式的常量。
public static final int DECRYPT_MODE //用于將 Cipher 初始化為解密模式的常量。
public static final int WRAP_MODE //用于將 Cipher 初始化為密鑰包裝模式的常量。
public static final int UNWRAP_MODE //用于將 Cipher 初始化為密鑰解包模式的常量。
public static final int PUBLIC_KEY //用于表示要解包的密鑰為“公鑰”的常量。
public static final int PRIVATE_KEY //用于表示要解包的密鑰為“私鑰”的常量。
public static final int SECRET_KEY //用于表示要解包的密鑰為“秘密密鑰”的常量。
初始化方法
// 僅使用密鑰進(jìn)行初始化
public final void init(int mode, Key key)
throws InvalidKeyException
// 用密鑰和隨機(jī)源初始化此 Cipher。
public final void init(int mode, Key key, SecureRandom random)
throws InvalidKeyException
//用密鑰和一組算法參數(shù)初始化此 Cipher。
public final void init(int mode, Key key,
AlgorithmParameterSpec paramSpcec)
throws InvalidKeyException, InvalidAlgorithmParameterException
// 用密鑰、一組算法參數(shù)和隨機(jī)源初始化此 Cipher。
public final void init(int mode, Key key,
AlgorithmParameterSpec paramSpec, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException
// 用密鑰和一組算法參數(shù)初始化此 Cipher。
public final void init(int mode, Key key, AlgorithmParameters param)
throws InvalidKeyException, InvalidAlgorithmParameterException
// 用密鑰、一組算法參數(shù)和隨機(jī)源初始化此 Cipher。
public final void init(int mode, Key key,
AlgorithmParameters param, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException
// 用取自給定證書的公鑰初始化此 Cipher。
public final void init(int mode, Certificate certificate)
throws InvalidKeyException
// 用取自給定證書的公鑰和隨機(jī)源初始化此 Cipher。
public final void init(int mode, Certificate certificate,
SecureRandom random)
throws InvalidKeyException
我們通常使用的是public final void init(int mode, Key key)。有四種mode可供選擇,分別對(duì)應(yīng)于Cipher中的四個(gè)常量如下:
- ENCRYPT_MODE 用于將 Cipher 初始化為加密模式。
- DECRYPT_MODE 用于將 Cipher 初始化為解密模式。
- WRAP_MODE 用于將 Cipher 初始化為密鑰包裝模式。
- UNWRAP_MODE 用于將 Cipher 初始化為密鑰解包模式。
如果此 Cipher 需要任何無(wú)法從給定 key 派生的算法參數(shù),則在為加密或密鑰包裝初始化時(shí),底層 Cipher 實(shí)現(xiàn)應(yīng)自己生成所需的參數(shù)(使用特定于提供者的默認(rèn)值或隨機(jī)值);在為解密或密鑰解包初始化時(shí),將引發(fā) InvalidKeyException??梢杂?getParameters 或 getIV 獲取生成的參數(shù)(如果該參數(shù)為 IV)。
如果此 Cipher(包括其底層反饋或填充方案)需要隨機(jī)字節(jié)(例如,用于參數(shù)生成),那么它將使用具有最高優(yōu)先級(jí)的已安裝提供者的 SecureRandom 實(shí)現(xiàn)作為隨機(jī)源獲取這些字節(jié)。(如果已安裝的提供者都不提供 SecureRandom 實(shí)現(xiàn),則將使用系統(tǒng)提供的隨機(jī)源)。
update 和 doFinal 方法
update方法有多個(gè)重載如下:
public final byte[] update(byte[] input)
public final byte[] update(byte[] input, int offset, int len)
public final int update(byte[] input, int offset, int len, byte[] output)
throws ShortBufferException
public final int update(byte[] input, int inOffset, int len, byte[] output, int outOffset)
throws ShortBufferException
public final int update(ByteBuffer input, ByteBuffer output)
throws ShortBufferException
// doFinal 方法除下面這個(gè)無(wú)參方法外,還有五個(gè)與update參數(shù)一致的重載方法,
// 它們相當(dāng)于使用同樣的參數(shù)調(diào)用update后再調(diào)用無(wú)參doFinal
public final byte[] doFinal()
throws IllegalBlockSizeException, BadPaddingException
update方法有于在多部分加密或解密操作中處理其他未完成加密或解密的部分?jǐn)?shù)據(jù)。doFinal則用于完成一次加密或解密操作,并將Cipher對(duì)象重置為上一次調(diào)用init方法時(shí)的狀態(tài)。
wrap 和 unwrap 方法
public final byte[] wrap(Key key)
throws IllegalBlockSizeException, InvalidKeyException
public final Key unwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)
throws InvalidKeyException, NoSuchAlgorithmException
wrap 方法用于包裝密鑰,unwrap方法用于解包一個(gè)以前包裝的密鑰。兩者為互逆操作。
在解包時(shí)參數(shù) wrappedKeyType 可以取三個(gè)值,它們都對(duì)應(yīng)于Cipher的靜態(tài)常量:
- PUBLIC_KEY 表示要解包的密鑰為“公鑰”。
- PRIVATE_KEY 表示要解包的密鑰為“私鑰”。
- SECRET_KEY 表示要解包的密鑰為“秘密密鑰”。
其他方法
-
public final int getBlockSize()返回塊的大?。ㄒ宰止?jié)為單位)。 -
public final int getOutputSize(int inputLen)根據(jù)給定的輸入長(zhǎng)度 inputLen(以字節(jié)為單位),返回保存下一個(gè) update 或 doFinal 操作結(jié)果所需的輸出緩沖區(qū)長(zhǎng)度(以字節(jié)為單位)。 -
public final byte[] getIV()返回新緩沖區(qū)中的初始化向量 (IV) -
public final AlgorithmParameters getParameters()返回此 Cipher 使用的參數(shù)。返回的參數(shù)可能與初始化此 Cipher 所使用的參數(shù)相同;如果此 Cipher 需要算法參數(shù)但卻未使用任何參數(shù)進(jìn)行初始化,則返回的參數(shù)將由默認(rèn)值和底層 Cipher 實(shí)現(xiàn)所使用的隨機(jī)參數(shù)值組成。
algorithm 參數(shù)支持的字符串:
- RSA
- DES
- DESede
- DESedeWrap
- PBEWithMD5AndDES
- PBEWithMD5AndTripleDES
- PBEWithSHA1AndDESede
- PBEWithSHA1AndRC2_40
- PBEWithSHA1AndRC2_128
- PBEWithSHA1AndRC4_40
- PBEWithSHA1AndRC4_128
- PBEWithHmacSHA1AndAES_128
- PBEWithHmacSHA224AndAES_128
- PBEWithHmacSHA256AndAES_128
- PBEWithHmacSHA384AndAES_128
- PBEWithHmacSHA512AndAES_128
- PBEWithHmacSHA1AndAES_256
- PBEWithHmacSHA224AndAES_256
- PBEWithHmacSHA256AndAES_256
- PBEWithHmacSHA384AndAES_256
- PBEWithHmacSHA512AndAES_256
- Blowfish
- AES
- AES_128/ECB/NoPadding
- AES_128/CBC/NoPadding
- AES_128/OFB/NoPadding
- AES_128/CFB/NoPadding
- AES_128/GCM/NoPadding
- AES_192/ECB/NoPadding
- AES_192/CBC/NoPadding
- AES_192/OFB/NoPadding
- AES_192/CFB/NoPadding
- AES_192/GCM/NoPadding
- AES_256/ECB/NoPadding
- AES_256/CBC/NoPadding
- AES_256/OFB/NoPadding
- AES_256/CFB/NoPadding
- AES_256/GCM/NoPadding
- AESWrap
- AESWrap_128
- AESWrap_192
- AESWrap_256
- RC2
- ARCFOUR
- Blowfish
- RSA/ECB/PKCS1Padding