javax.crypto.Cipher類提供加密和解密功能,該類是JCE框架的核心。
一,與所有的引擎類一樣,可以通過調用Cipher類中的getInstance靜態工廠方法得到Cipher對象。
public static Cipher getInstance(String transformation);
public static Cipher getInstance(String transformation,String provider);
參數transformation是一個字符串,它描述了由指定輸入產生輸出所進行的操作或操作集合。
參數transformation總是包含密碼學算法名稱,比如DES,也可以在后面包含模式和填充方式。
參數transformation可以是下列兩種形式之一:
“algorithm/mode/padding”
“algorithm”
例如下面的例子就是有效的transformation形式:
"DES/CBC/PKCS5Padding"
"DES"
如 果沒有指定模式或填充方式,就使用特定提供者指定的默認模式或默認填充方式。例如,SunJCE提供者使用ECB作為DES、DES-EDE和 Blowfish等Cipher的默認模式,並使用PKCS5Padding作為它們默認的填充方案。這意味着在SunJCE提供者中,下列形式的聲明是 等價的:Cipher c1=Cipher.getInstance("DES/ECB/PKCS5Padding");
Cipher c1=Cipher.getInstance("DES");
當 以流加密方式請求以塊划分的cipher時,可以在模式名后面跟上一次運算需要操作的bit數目,例如采用"DES/CFB8/NoPadding"和 "DES/OFB32/PKCS5Padding"形式的transformation參數。如果沒有指定數目,則使用提供者指定的默認值(例如 SunJCE提供者使用的默認值是64bit)。
getInstance工廠方法返回的對象沒有進行初始化,因此在使用前必須進行初始化。
通過getInstance得到的Cipher對象必須使用下列四個模式之一進行初始化,這四個模式在Cipher類中被定義為final integer常數,我們可以使用符號名來引用這些模式:
ENCRYPT_MODE,加密數據
DECRYPT_MODE,解密數據
WRAP_MODE,將一個Key封裝成字節,可以用來進行安全傳輸
UNWRAP_MODE,將前述已封裝的密鑰解開成java.security.Key對象
每個Cipher初始化方法使用一個模式參數opmod,並用此模式初始化Cipher對象。此外還有其他參數,包括密鑰key、包含密鑰的證書certificate、算法參數params和隨機源random。
我們可以調用以下的init方法之一來初始化Cipher對象:
public void init(int opmod,Key key);
public void init(int opmod,Certificate certificate);
public void init(int opmod,Key key,SecureRandom random);
public void init(int opmod,Certificate certificate,SecureRandom random);
public void init(int opmod,Key key,AlgorithmParameterSpec params);
public void init(int opmod,Key key,AlgorithmParameterSpec params,SecureRandom random);
public void init(int opmod,Key key,AlgorithmParameters params);
public void init(int opmod,Key key,AlgorithmParameters params,SecureRandom random);
必須指出的是,加密和解密必須使用相同的參數。當Cipher對象被初始化時,它將失去以前得到的所有狀態。即,初始化Cipher對象與新建一個Cipher實例然后將它初始化是等價的。
二,可以調用以下的doFinal()方法之一完成單步的加密或解密數據:
public byte[] doFinal(byte[] input);
public byte[] doFinal(byte[] input,int inputOffset,int inputLen);
public int doFinal(byte[] input,int inputOffset,int inputLen,byte[] output);
public int doFinal(byte[] input,int inputOffset,int inputLen,byte[] output,int outputOffset);
在多步加密或解密數據時,首先需要一次或多次調用update方法,用以提供加密或解密的所有數據:
public byte[] update(byte[] input);
public byte[] update(byte[] input,int inputOffset,int inputLen);
public int update(byte[] input,int inputOffset,int inputLen,byte[] output);
public int update(byte[] input,int inputOffset,int inputLen,byte[] output,int outputOffset);
如果還有輸入數據,多步操作可以使用前面提到的doFinal方法之一結束。如果沒有數據,多步操作可以使用下面的doFinal方法之一結束:
public byte[] doFinal();
public int doFinal(byte[] output,int outputOffset);
如果在transformation參數部分指定了padding或unpadding方式,則所有的doFinal方法都要注意所用的padding或unpadding方式。
調用doFinal方法將會重置Cipher對象到使用init進行初始化時的狀態,就是說,Cipher對象被重置,使得可以進行更多數據的加密或解密,至於這兩種模式,可以在調用init時進行指定。
三,包裹wrap密鑰必須先使用WRAP_MODE初始化Cipher對象,然后調用以下方法:
public final byte[] wrap(Key key);
如果將調用wrap方法的結果(wrap后的密鑰字節)提供給解包裹unwrap的人使用,必須給接收者發送以下額外信息:
(1)密鑰算法名稱:
密鑰算法名稱可以調用Key接口提供的getAlgorithm方法得到:
public String getAlgorithm();
(2)被包裹密鑰的類型(Cipher.SECRET_KEY,Cipher.PRIVATE_KEY,Cipher.PUBLIC_KEY)
sourcelink: http://bbs.sdu.edu.cn/pc/pccon.php?id=1292&nid=41716&order=&tid=
為了對調用wrap方法返回的字節進行解包,必須先使用UNWRAP_MODE模式初始化Cipher對象,然后調用以下方法 :
public final Key unwrap(byte[] wrappedKey,String wrappedKeyAlgorithm,int wrappedKeyType));
其 中,參數wrappedKey是調用wrap方法返回的字節,參數wrappedKeyAlgorithm是用來包裹密鑰的算法,參數 wrappedKeyType是被包裹密鑰的類型,該類型必須是Cipher.SECRET_KEY,Cipher.PRIVATE_KEY, Cipher.PUBLIC_KEY三者之一。
四,SunJCE提供者實現的cipher算法使用如下參數:
(1)采用CBC、CFB、OFB、PCBC模式的DES、DES-EDE和Blowfish算法。,它們使用初始化向量IV作為參數。可以使用javax.crypto.spec.IvParameterSpec類並使用給定的IV參數來初始化Cipher對象。
(2)PBEWithMD5AndDES使用的參數是一個由鹽值和迭代次數組成的參數集合。可以使用javax.crypto.spec.PBEParameterSpec類並利用給定鹽值和迭代次數來初始化Cipher對象。
注意:如果使用SealedObject類,就不必為解密運算參數的傳遞和保存擔心。這個類在加密對象內容中附帶了密封和加密的參數,可以使用相同的參數對其進行解封和解密。
Cipher 中的某些update和doFinal方法允許調用者指定加密或解密數據的輸出緩存。此時,保證指定的緩存足夠大以容納加密或解密運算的結果是非常重要 的,可以使用Cipher的以下方法來決定輸出緩存應該有多大:public int getOutputSize(int inputLen)
實例