本文整理汇总了Java中org.bouncycastle.crypto.params.ElGamalParameters类的典型用法代码示例。如果您正苦于以下问题:Java ElGamalParameters类的具体用法?Java ElGamalParameters怎么用?Java ElGamalParameters使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ElGamalParameters类属于org.bouncycastle.crypto.params包,在下文中一共展示了ElGamalParameters类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateKeyPair
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
public AsymmetricCipherKeyPair generateKeyPair()
{
BigInteger p, g, x, y;
int qLength = param.getStrength() - 1;
ElGamalParameters elParams = param.getParameters();
p = elParams.getP();
g = elParams.getG();
//
// calculate the private key
//
x = new BigInteger(qLength, param.getRandom());
//
// calculate the public key.
//
y = g.modPow(x, p);
return new AsymmetricCipherKeyPair(
new ElGamalPublicKeyParameters(y, elParams),
new ElGamalPrivateKeyParameters(x, elParams));
}
示例2: testInvalidP
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
private void testInvalidP()
{
ElGamalParameters dhParams = new ElGamalParameters(pPgpBogusPSamp, gPgpBogusPSamp, lPgpBogusPSamp);
ElGamalPublicKeyParameters pu = new ElGamalPublicKeyParameters(yPgpBogusPSamp, dhParams);
ElGamalPrivateKeyParameters pv = new ElGamalPrivateKeyParameters(xPgpBogusPSamp, dhParams);
ElGamalEngine e = new ElGamalEngine();
e.init(true, pu);
byte[] message = Hex.decode("5468697320697320612074657374");
byte[] pText = message;
byte[] cText = e.processBlock(pText, 0, pText.length);
e.init(false, pv);
pText = e.processBlock(cText, 0, cText.length);
if (Arrays.areEqual(message, pText))
{
fail("invalid test failed");
}
}
示例3: createAsymmetricCipherKeyPairGenerator
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
@Override
public AsymmetricCipherKeyPairGenerator createAsymmetricCipherKeyPairGenerator(final boolean initWithDefaults) {
final ElGamalKeyPairGenerator generator = new ElGamalKeyPairGenerator();
if (initWithDefaults) {
/*
* How certain do we want to be that the chosen primes are really primes.
* <p>
* The higher this number, the more tests are done to make sure they are primes (and not composites).
* <p>
* See: <a href="http://crypto.stackexchange.com/questions/3114/what-is-the-correct-value-for-certainty-in-rsa-key-pair-generation">What is the correct value for “certainty” in RSA key pair generation?</a>
* and
* <a href="http://crypto.stackexchange.com/questions/3126/does-a-high-exponent-compensate-for-a-low-degree-of-certainty?lq=1">Does a high exponent compensate for a low degree of certainty?</a>
*/
final int certainty = 12;
final SecureRandom random = new SecureRandom();
ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
pGen.init(4096, certainty, random);
ElGamalParameters elGamalParameters = pGen.generateParameters();
generator.init(new ElGamalKeyGenerationParameters(random, elGamalParameters));
}
return generator;
}
示例4: createElGamalKeyGenerationParameters
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
private ElGamalKeyGenerationParameters createElGamalKeyGenerationParameters(final CreatePgpKeyParam createPgpKeyParam) {
/*
* How certain do we want to be that the chosen primes are really primes.
* <p>
* The higher this number, the more tests are done to make sure they are primes (and not composites).
* <p>
* See: <a href="http://crypto.stackexchange.com/questions/3114/what-is-the-correct-value-for-certainty-in-rsa-key-pair-generation">What is the correct value for “certainty” in RSA key pair generation?</a>
* and
* <a href="http://crypto.stackexchange.com/questions/3126/does-a-high-exponent-compensate-for-a-low-degree-of-certainty?lq=1">Does a high exponent compensate for a low degree of certainty?</a>
*/
final int certainty = 8; // 12 takes ages - and DSA+El-Gamal is anyway a bad idea and discouraged. Reducing this to make it bearable.
final SecureRandom random = getSecureRandom();
ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
pGen.init(createPgpKeyParam.getStrength(), certainty, random);
ElGamalParameters elGamalParameters = pGen.generateParameters();
// Maybe we should generate our "DH safe primes" only once and store them somewhere? Or maybe we should provide a long list
// of them in the resources? DHParametersHelper.generateSafePrimes(size, certainty, random); takes really really very long.
// BUT BEWARE: Attacks on El Gamal can re-use expensively calculated stuff, if p (one of the "safe primes) is the same.
// However, it is still not *so* easy. Hmmm... don't know. Security is really important here.
return new ElGamalKeyGenerationParameters(random, elGamalParameters);
}
示例5: generateParameters
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
/**
* which generates the p and g values from the given parameters,
* returning the ElGamalParameters object.
* <p>
* Note: can take a while...
*/
public ElGamalParameters generateParameters()
{
//
// find a safe prime p where p = 2*q + 1, where p and q are prime.
//
BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random);
BigInteger p = safePrimes[0];
BigInteger q = safePrimes[1];
BigInteger g = DHParametersHelper.selectGenerator(p, q, random);
return new ElGamalParameters(p, g);
}
示例6: generateKeyPair
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
public AsymmetricCipherKeyPair generateKeyPair()
{
DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
ElGamalParameters egp = param.getParameters();
DHParameters dhp = new DHParameters(egp.getP(), egp.getG(), null, egp.getL());
BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
BigInteger y = helper.calculatePublic(dhp, x);
return new AsymmetricCipherKeyPair(
new ElGamalPublicKeyParameters(y, egp),
new ElGamalPrivateKeyParameters(x, egp));
}
示例7: engineGenerateParameters
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
protected AlgorithmParameters engineGenerateParameters()
{
ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
if (random != null)
{
pGen.init(strength, 20, random);
}
else
{
pGen.init(strength, 20, new SecureRandom());
}
ElGamalParameters p = pGen.generateParameters();
AlgorithmParameters params;
try
{
params = AlgorithmParameters.getInstance("ElGamal", BouncyCastleProvider.PROVIDER_NAME);
params.init(new DHParameterSpec(p.getP(), p.getG(), l));
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage());
}
return params;
}
示例8: generatePublicKeyParameter
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
static public AsymmetricKeyParameter generatePublicKeyParameter(
PublicKey key)
throws InvalidKeyException
{
if (key instanceof ElGamalPublicKey)
{
ElGamalPublicKey k = (ElGamalPublicKey)key;
return new ElGamalPublicKeyParameters(k.getY(),
new ElGamalParameters(k.getParams().getP(), k.getParams().getG()));
}
throw new InvalidKeyException("can't identify ElGamal public key.");
}
示例9: generatePrivateKeyParameter
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
static public AsymmetricKeyParameter generatePrivateKeyParameter(
PrivateKey key)
throws InvalidKeyException
{
if (key instanceof ElGamalPrivateKey)
{
ElGamalPrivateKey k = (ElGamalPrivateKey)key;
return new ElGamalPrivateKeyParameters(k.getX(),
new ElGamalParameters(k.getParams().getP(), k.getParams().getG()));
}
throw new InvalidKeyException("can't identify ElGamal private key.");
}
示例10: engineGenerateParameters
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
protected AlgorithmParameters engineGenerateParameters()
{
ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
if ( random != null )
{
pGen.init(strength, 20, random);
}
else
{
pGen.init(strength, 20, new SecureRandom());
}
ElGamalParameters p = pGen.generateParameters();
AlgorithmParameters params;
try
{
params = AlgorithmParameters.getInstance("ElGamal", BouncyCastleProvider.PROVIDER_NAME);
params.init(new ElGamalParameterSpec(p.getP(), p.getG()));
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage());
}
return params;
}
示例11: engineGenerateParameters
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
protected AlgorithmParameters engineGenerateParameters()
{
ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
if (random != null)
{
pGen.init(strength, 20, random);
}
else
{
pGen.init(strength, 20, new SecureRandom());
}
ElGamalParameters p = pGen.generateParameters();
AlgorithmParameters params;
try
{
params = createParametersInstance("ElGamal");
params.init(new DHParameterSpec(p.getP(), p.getG(), l));
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage());
}
return params;
}
示例12: encryptWithPublicKey
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
@Override
public byte[] encryptWithPublicKey(byte[] data) {
ElGamalParameters params = new ElGamalParameters(keys.getP(), keys.getG());
ElGamalPublicKeyParameters pubKey = new ElGamalPublicKeyParameters(keys.getPublicKey(), params);
ElGamalEngine e = new ElGamalEngine();
e.init(true, pubKey);
return e.processBlock(data, 0, data.length) ;
}
示例13: encryptForContract
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
public ElGamalEncrypt encryptForContract(byte[] data) {
ElGamalParameters params = new ElGamalParameters(keys.getP(), keys.getG());
ElGamalPublicKeyParameters pubKey = new ElGamalPublicKeyParameters(keys.getPublicKey(), params);
ElGamalEngine e = new ElGamalEngine();
e.init(true, pubKey);
byte[] m = e.processBlock(data, 0, data.length);
BigInteger k = e.getK();
BigInteger u = keys.getG().modPow(k,keys.getP());
BigInteger v = (keys.getPublicKey().modPow(e.getK(), keys.getP()).multiply(new BigInteger(data)));
return new ElGamalEncrypt (u,v,k, m);
}
示例14: decryptWithPrivateKey
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
@Override
public byte[] decryptWithPrivateKey(byte[] data) {
ElGamalParameters params = new ElGamalParameters(keys.getP(), keys.getG());
ElGamalPrivateKeyParameters privKey = new ElGamalPrivateKeyParameters(keys.getPrivateKey(), params);
ElGamalEngine e = new ElGamalEngine();
e.init(false, privKey);
return e.processBlock(data, 0, data.length) ;
}
示例15: GenerateKeys
import org.bouncycastle.crypto.params.ElGamalParameters; //导入依赖的package包/类
/**
* This method is used to generate Public Key and Private Key
* @param params
*/
private void GenerateKeys(ElGamalParameters params, String password){
ElGamalKeyGenerationParameters elGP = new ElGamalKeyGenerationParameters(random,params);
ElGamalKeyPairGenerator KeyPair = new ElGamalKeyPairGenerator();
KeyPair.init(elGP);
AsymmetricCipherKeyPair cipher1 = KeyPair.generateKeyPair();
publicKey = ((ElGamalPublicKeyParameters) cipher1.getPublic()).getY();
privateKey = ((ElGamalPrivateKeyParameters)cipher1.getPrivate()).getX();
wellGenerated = true;
encryptPrivateKey(password);
}