use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.
the class PKCS1Encoding method init.
public void init(boolean forEncryption, CipherParameters param) {
AsymmetricKeyParameter kParam;
if (param instanceof ParametersWithRandom) {
ParametersWithRandom rParam = (ParametersWithRandom) param;
this.random = rParam.getRandom();
kParam = (AsymmetricKeyParameter) rParam.getParameters();
} else {
kParam = (AsymmetricKeyParameter) param;
if (!kParam.isPrivate() && forEncryption) {
this.random = CryptoServicesRegistrar.getSecureRandom();
}
}
engine.init(forEncryption, param);
this.forPrivateKey = kParam.isPrivate();
this.forEncryption = forEncryption;
this.blockBuffer = new byte[engine.getOutputBlockSize()];
if (pLen > 0 && fallback == null && random == null) {
throw new IllegalArgumentException("encoder requires random");
}
}
use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.
the class OpenSSHPublicKeyUtil method parsePublicKey.
/**
* Parse a public key from an SSHBuffer instance.
*
* @param buffer containing the SSH public key.
* @return A CipherParameters instance.
*/
public static AsymmetricKeyParameter parsePublicKey(SSHBuffer buffer) {
AsymmetricKeyParameter result = null;
String magic = buffer.readString();
if (RSA.equals(magic)) {
BigInteger e = buffer.readBigNumPositive();
BigInteger n = buffer.readBigNumPositive();
result = new RSAKeyParameters(false, n, e);
} else if (DSS.equals(magic)) {
BigInteger p = buffer.readBigNumPositive();
BigInteger q = buffer.readBigNumPositive();
BigInteger g = buffer.readBigNumPositive();
BigInteger pubKey = buffer.readBigNumPositive();
result = new DSAPublicKeyParameters(pubKey, new DSAParameters(p, q, g));
} else if (magic.startsWith(ECDSA)) {
String curveName = buffer.readString();
ASN1ObjectIdentifier oid = SSHNamedCurves.getByName(curveName);
X9ECParameters x9ECParameters = SSHNamedCurves.getParameters(oid);
if (x9ECParameters == null) {
throw new IllegalStateException("unable to find curve for " + magic + " using curve name " + curveName);
}
ECCurve curve = x9ECParameters.getCurve();
byte[] pointRaw = buffer.readBlock();
result = new ECPublicKeyParameters(curve.decodePoint(pointRaw), new ECNamedDomainParameters(oid, x9ECParameters));
} else if (ED_25519.equals(magic)) {
byte[] pubKeyBytes = buffer.readBlock();
if (pubKeyBytes.length != Ed25519PublicKeyParameters.KEY_SIZE) {
throw new IllegalStateException("public key value of wrong length");
}
result = new Ed25519PublicKeyParameters(pubKeyBytes, 0);
}
if (result == null) {
throw new IllegalArgumentException("unable to parse key");
}
if (buffer.hasRemaining()) {
throw new IllegalArgumentException("decoded key has trailing data");
}
return result;
}
use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.
the class RSADigestSigner method init.
/**
* Initialize the signer for signing or verification.
*
* @param forSigning true if for signing, false otherwise
* @param parameters necessary parameters.
*/
public void init(boolean forSigning, CipherParameters parameters) {
this.forSigning = forSigning;
AsymmetricKeyParameter k;
if (parameters instanceof ParametersWithRandom) {
k = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters();
} else {
k = (AsymmetricKeyParameter) parameters;
}
if (forSigning && !k.isPrivate()) {
throw new IllegalArgumentException("signing requires private key");
}
if (!forSigning && k.isPrivate()) {
throw new IllegalArgumentException("verification requires public key");
}
reset();
rsaEngine.init(forSigning, parameters);
}
use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.
the class DigestingMessageSigner method init.
public void init(boolean forSigning, CipherParameters param) {
this.forSigning = forSigning;
AsymmetricKeyParameter k;
if (param instanceof ParametersWithRandom) {
k = (AsymmetricKeyParameter) ((ParametersWithRandom) param).getParameters();
} else {
k = (AsymmetricKeyParameter) param;
}
if (forSigning && !k.isPrivate()) {
throw new IllegalArgumentException("Signing Requires Private Key.");
}
if (!forSigning && k.isPrivate()) {
throw new IllegalArgumentException("Verification Requires Public Key.");
}
reset();
messSigner.init(forSigning, param);
}
use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.
the class IESCipher method engineDoFinal.
// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
if (inputLen != 0) {
buffer.write(input, inputOffset, inputLen);
}
byte[] in = buffer.toByteArray();
buffer.reset();
// Convert parameters for use in IESEngine
CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
if (engineSpec.getNonce() != null) {
params = new ParametersWithIV(params, engineSpec.getNonce());
}
DHParameters dhParams = ((DHKeyParameters) key).getParameters();
byte[] V;
if (otherKeyParameter != null) {
try {
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
engine.init(true, otherKeyParameter, key, params);
} else {
engine.init(false, key, otherKeyParameter, params);
}
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
}
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
// Generate the ephemeral key pair
DHKeyPairGenerator gen = new DHKeyPairGenerator();
gen.init(new DHKeyGenerationParameters(random, dhParams));
EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {
public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
byte[] Vloc = new byte[(((DHKeyParameters) keyParameter).getParameters().getP().bitLength() + 7) / 8];
byte[] Vtmp = BigIntegers.asUnsignedByteArray(((DHPublicKeyParameters) keyParameter).getY());
if (Vtmp.length > Vloc.length) {
throw new IllegalArgumentException("Senders's public key longer than expected.");
} else {
System.arraycopy(Vtmp, 0, Vloc, Vloc.length - Vtmp.length, Vtmp.length);
}
return Vloc;
}
});
// Encrypt the buffer
try {
engine.init(key, params, kGen);
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
} else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
// Decrypt the buffer
try {
engine.init(key, params, new DHIESPublicKeyParser(((DHKeyParameters) key).getParameters()));
return engine.processBlock(in, 0, in.length);
} catch (InvalidCipherTextException e) {
throw new BadBlockException("unable to process block", e);
}
} else {
throw new IllegalStateException("IESCipher not initialised");
}
}
Aggregations