Search in sources :

Example 1 with CramerShoupPrivateKeyParameters

use of com.github.zhenwei.core.crypto.params.CramerShoupPrivateKeyParameters in project LinLong-Java by zhenwei1108.

the class CramerShoupKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    CramerShoupParameters csParams = param.getParameters();
    CramerShoupPrivateKeyParameters sk = generatePrivateKey(param.getRandom(), csParams);
    CramerShoupPublicKeyParameters pk = calculatePublicKey(csParams, sk);
    sk.setPk(pk);
    return new AsymmetricCipherKeyPair(pk, sk);
}
Also used : CramerShoupParameters(com.github.zhenwei.core.crypto.params.CramerShoupParameters) CramerShoupPrivateKeyParameters(com.github.zhenwei.core.crypto.params.CramerShoupPrivateKeyParameters) CramerShoupPublicKeyParameters(com.github.zhenwei.core.crypto.params.CramerShoupPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 2 with CramerShoupPrivateKeyParameters

use of com.github.zhenwei.core.crypto.params.CramerShoupPrivateKeyParameters in project LinLong-Java by zhenwei1108.

the class CramerShoupCoreEngine method decryptBlock.

public BigInteger decryptBlock(CramerShoupCiphertext input) throws CramerShoupCiphertextException {
    BigInteger result = null;
    if (key.isPrivate() && !this.forEncryption && key instanceof CramerShoupPrivateKeyParameters) {
        CramerShoupPrivateKeyParameters sk = (CramerShoupPrivateKeyParameters) key;
        BigInteger p = sk.getParameters().getP();
        Digest digest = sk.getParameters().getH();
        byte[] u1Bytes = input.getU1().toByteArray();
        digest.update(u1Bytes, 0, u1Bytes.length);
        byte[] u2Bytes = input.getU2().toByteArray();
        digest.update(u2Bytes, 0, u2Bytes.length);
        byte[] eBytes = input.getE().toByteArray();
        digest.update(eBytes, 0, eBytes.length);
        if (this.label != null) {
            byte[] lBytes = this.label;
            digest.update(lBytes, 0, lBytes.length);
        }
        byte[] out = new byte[digest.getDigestSize()];
        digest.doFinal(out, 0);
        BigInteger a = new BigInteger(1, out);
        BigInteger v = input.u1.modPow(sk.getX1().add(sk.getY1().multiply(a)), p).multiply(input.u2.modPow(sk.getX2().add(sk.getY2().multiply(a)), p)).mod(p);
        // check correctness of ciphertext
        if (input.v.equals(v)) {
            result = input.e.multiply(input.u1.modPow(sk.getZ(), p).modInverse(p)).mod(p);
        } else {
            throw new CramerShoupCiphertextException("Sorry, that ciphertext is not correct");
        }
    }
    return result;
}
Also used : CramerShoupPrivateKeyParameters(com.github.zhenwei.core.crypto.params.CramerShoupPrivateKeyParameters) Digest(com.github.zhenwei.core.crypto.Digest) BigInteger(java.math.BigInteger)

Example 3 with CramerShoupPrivateKeyParameters

use of com.github.zhenwei.core.crypto.params.CramerShoupPrivateKeyParameters in project LinLong-Java by zhenwei1108.

the class CramerShoupKeyPairGenerator method generatePrivateKey.

private CramerShoupPrivateKeyParameters generatePrivateKey(SecureRandom random, CramerShoupParameters csParams) {
    BigInteger p = csParams.getP();
    CramerShoupPrivateKeyParameters key = new CramerShoupPrivateKeyParameters(csParams, generateRandomElement(p, random), generateRandomElement(p, random), generateRandomElement(p, random), generateRandomElement(p, random), generateRandomElement(p, random));
    return key;
}
Also used : CramerShoupPrivateKeyParameters(com.github.zhenwei.core.crypto.params.CramerShoupPrivateKeyParameters) BigInteger(java.math.BigInteger)

Aggregations

CramerShoupPrivateKeyParameters (com.github.zhenwei.core.crypto.params.CramerShoupPrivateKeyParameters)3 BigInteger (java.math.BigInteger)2 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1 Digest (com.github.zhenwei.core.crypto.Digest)1 CramerShoupParameters (com.github.zhenwei.core.crypto.params.CramerShoupParameters)1 CramerShoupPublicKeyParameters (com.github.zhenwei.core.crypto.params.CramerShoupPublicKeyParameters)1