use of com.github.zhenwei.core.crypto.modes.CBCBlockCipher in project LinLong-Java by zhenwei1108.
the class DESedeWrapEngine method init.
/**
* Method init
*
* @param forWrapping true if for wrapping, false otherwise.
* @param param necessary parameters, may include KeyParameter, ParametersWithRandom, and
* ParametersWithIV
*/
public void init(boolean forWrapping, CipherParameters param) {
this.forWrapping = forWrapping;
this.engine = new CBCBlockCipher(new DESedeEngine());
SecureRandom sr;
if (param instanceof ParametersWithRandom) {
ParametersWithRandom pr = (ParametersWithRandom) param;
param = pr.getParameters();
sr = pr.getRandom();
} else {
sr = CryptoServicesRegistrar.getSecureRandom();
}
if (param instanceof KeyParameter) {
this.param = (KeyParameter) param;
if (this.forWrapping) {
// Hm, we have no IV but we want to wrap ?!?
// well, then we have to create our own IV.
this.iv = new byte[8];
sr.nextBytes(iv);
this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
}
} else if (param instanceof ParametersWithIV) {
this.paramPlusIV = (ParametersWithIV) param;
this.iv = this.paramPlusIV.getIV();
this.param = (KeyParameter) this.paramPlusIV.getParameters();
if (this.forWrapping) {
if ((this.iv == null) || (this.iv.length != 8)) {
throw new IllegalArgumentException("IV is not 8 octets");
}
} else {
throw new IllegalArgumentException("You should not supply an IV for unwrapping");
}
}
}
Aggregations