自己作了一个简单的加密provider
package security.cipher;
import java.security.*;
public final class Rot13Provider extends Provider {
public Rot13Provider(){
super( "Rot13Provider ", 1.0,
"Rot13Provider 1.0 provides a custom ROT13 implementation. ");
//set provider properties
put( "Cipher.ROT13 ", "security.cipher.Rot13Cipher ");
}
}
其中security.cipher.Rot13Cipher扩展了CipherSpi
package security.cipher;
public class Rot13Cipher extends CipherSpi {
private int opmode = Cipher.ENCRYPT_MODE;
private int encryptionKey = 13;
private int decryptionKey = 243;
public Rot13Cipher(){
}
//Implement the cipher
private byte cipher(byte b){
int key = encryptionKey;
if(opmode == Cipher.DECRYPT_MODE){
key = decryptionKey;
}
return (byte)((b + key) % 256);
}
@Override
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
throws IllegalBlockSizeException, BadPaddingException {
byte[] b = new byte[ inputLen];
for(int i=0; i <inputLen; ++i){
b[ i] = cipher(input[ inputOffset +i]);
}
return b;
}
@Override
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen
, byte[] output,int outputOffset)
throws ShortBufferException, IllegalBlockSizeException,
BadPaddingException {
String msg = "Not enough room in output buffer. ";
if((output.length - outputOffset) < inputLen){
throw new ShortBufferException(msg);
}
for(int i=0; i <inputLen; ++i){
output[ outputOffset + i] = cipher(input[inputOffset + i]);
}
return inputLen;
}
@Override
protected int engineGetBlockSize() {
return 1;
}
@Override
protected byte[] engineGetIV() {
return null;
}
@Override
protected int engineGetOutputSize(int inputLen) {
return inputLen;
}
@Override
protected AlgorithmParameters engineGetParameters() {
return null;
}
@Override
protected void engineInit(int opmode, Key key, SecureRandom random)
throws InvalidKeyException {
this.opmode = opmode;
}
@Override
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
SecureRandom random) throws InvalidKeyException,
InvalidAlgorithmParameterException {