1、Signature
签名对象
Only the owner of a private/public key pair is able to create a signature. It should be computationally infeasible for anyone having a public key to recover the private key.
只有拥有公私钥对,才能创建一个签名对象。使用私钥签名,公钥验签。
Even though a signature seems similar to a message digest, they have very different purposes in the type of protection they provide. In fact, algorithms such as "SHA1WithRSA" use the message digest "SHA1" to initially "compress" the large data sets into a more manageable form, then sign the resulting 20 byte message digest with the "RSA" algorithm.
尽管签名和摘要有些类似,但是在保护的方式上它们不同。比如说签名算法SHA1WithRSA 使用sha1将大数据压缩成一个比较小的数据,然后用RSA算法进行签名。
签名对象是有状态的,分为三种:
UNINITIALIZED
SIGN
VERIFY
2、初始化一个签名对象
如果签名对象用于数据签名,那么应该使用
final void initSign(PrivateKey privateKey)
这样,签名对象的状态就是SIGN。
如果签名对象用于验签,那么应该使用下面的两种之一
final void initVerify(PublicKey publicKey)
final void initVerify(Certificate certificate)
这样签名对象的状态就是VERIFY
3、签名操作
第一步:调用下面的方法之一:
final void update(byte b)
final void update(byte[] data)
final void update(byte[] data, int off, int len)
Calls to the update
method(s) should be made until all the data to be signed has been supplied to theSignature
object.
直到所有的数据都传入update方法后,
第二步:
final byte[] sign() final int sign(byte[] outbuf, int offset, int len)
产生一个签名字节数组
A call to a
sign
method resets the signature object to the state it was in when previously initialized for signing via a call toinitSign
. That is, the object is reset and available to generate another signature with the same private key, if desired, via new calls toupdate
andsign
.调用完sign方法后,签名对象的状态被重置,可以重新调用update和sign方法并且使用原来的私钥。
2、加密令牌对象Clipher
The
Cipher
class provides the functionality of a cryptographic cipher used for encryption and decryption提供对数据的加密和解密功能。
分为流加密和分组加密
There are two major types of ciphers: block and stream. Block ciphers process entire blocks at a time, usually many bytes in length. If there is not enough data to make a complete input block, the data must be padded: that is, before encryption, dummy bytes must be added to make a multiple of the cipher's block size. These bytes are then stripped off during the decryption phase. The padding can either be done by the application, or by initializing a cipher to use a padding type such as "PKCS5PADDING". In contrast, stream ciphers process incoming data one small unit (typically a byte or even a bit) at a time. This allows for ciphers to process an arbitrary amount of data without padding.分组加密一次处理整个数据块,如果没有足够的数据来提供一个输入块,这个数据会被填充。在加密的时候,将会添加虚拟字节用来组成加密块大小的倍数。
在解密阶段,这些虚拟自己将会被脱去。应用可以提供数据填充,或者初始化一个加密令牌以用来填充。流加密处理每个字节。
Modes Of Operation
When encrypting using a simple block cipher, two identical blocks of plaintext will always produce an identical block of cipher text. Cryptanalysts trying to break the ciphertext will have an easier job if they note blocks of repeating text. In order to add more complexity to the text, feedback modes use the previous block of output to alter the input blocks before applying the encryption algorithm. The first block will need an initial value, and this value is called the initialization vector (IV). Since the IV simply alters the data before any encryption, the IV should be random but does not necessarily need to be kept secret. There are a variety of modes, such as CBC (Cipher Block Chaining), CFB (Cipher Feedback Mode), and OFB (Output Feedback Mode). ECB (Electronic Cookbook Mode) is a mode with no feedback.
If no mode or padding is specified, provider-specific default values for the mode and padding scheme are used. For example, the
SunJCE
provider usesECB
as the default mode, andPKCS5Padding
as the default padding scheme forDES
,DES-EDE
andBlowfish
ciphers. This means that in the case of theSunJCE
provider:Cipher c1 = Cipher.getInstance("DES/ECB/PKCS5Padding");and
Cipher c1 = Cipher.getInstance("DES");are equivalent statements.
SunJce提供商默认使用ECB的Model格式,使用默认的
PKCS5Padding padding格式对DES、DES-EDE和Blowfish
Using modes such as CFB and OFB, block ciphers can encrypt data in units smaller than the cipher's actual block size. When requesting such a mode, you may optionally specify the number of bits to be processed at a time by appending this number to the mode name as shown in the "DES/CFB8/NoPadding" and "DES/OFB32/PKCS5Padding" transformations. If no such number is specified, a provider-specific default is used. (For example, the
SunJCE
provider uses a default of 64 bits for DES.) Thus, block ciphers can be turned into byte-oriented stream ciphers by using an 8 bit mode such as CFB8 or OFB8.