本文整理汇总了Java中java.security.interfaces.DSAPrivateKey.getParams方法的典型用法代码示例。如果您正苦于以下问题:Java DSAPrivateKey.getParams方法的具体用法?Java DSAPrivateKey.getParams怎么用?Java DSAPrivateKey.getParams使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.interfaces.DSAPrivateKey
的用法示例。
在下文中一共展示了DSAPrivateKey.getParams方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: regenerateLocalPublicKey
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
public void regenerateLocalPublicKey(KeyFactory factory, String fullUserId, DSAPrivateKey privKey) {
String userId = Address.stripResource(fullUserId);
BigInteger x = privKey.getX();
DSAParams params = privKey.getParams();
BigInteger y = params.getG().modPow(x, params.getP());
DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
PublicKey pubKey;
try {
pubKey = factory.generatePublic(keySpec);
storeLocalPublicKey(userId, pubKey);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例2: getPublicKey
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
public PublicKey getPublicKey() throws GeneralSecurityException {
if (privateKey instanceof DSAPrivateKey) {
DSAPrivateKey dsa = (DSAPrivateKey) privateKey;
DSAParams params = dsa.getParams();
BigInteger g = params.getG();
BigInteger p = params.getP();
BigInteger q = params.getQ();
BigInteger x = dsa.getX();
BigInteger y = q.modPow( x, p );
DSAPublicKeySpec dsaKeySpec = new DSAPublicKeySpec(y, p, q, g);
return KeyFactory.getInstance("DSA").generatePublic(dsaKeySpec);
} else if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsa = (RSAPrivateCrtKey) privateKey;
RSAPublicKeySpec rsaKeySpec = new RSAPublicKeySpec(
rsa.getModulus(),
rsa.getPublicExponent()
);
return KeyFactory.getInstance("RSA").generatePublic(rsaKeySpec);
} else {
throw new GeneralSecurityException("Not an RSA or DSA key");
}
}
示例3: regenerateLocalPublicKey
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
public void regenerateLocalPublicKey(KeyFactory factory, String fullUserId, DSAPrivateKey privKey) {
String userId = Address.stripResource(fullUserId);
BigInteger x = privKey.getX();
DSAParams params = privKey.getParams();
BigInteger y = params.getG().modPow(x, params.getP());
DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
PublicKey pubKey;
try {
pubKey = factory.generatePublic(keySpec);
} catch (InvalidKeySpecException e) {
throw new RuntimeException(e);
}
storeLocalPublicKey(userId, pubKey);
}
示例4: testKeyGeneration
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
@SuppressWarnings("InsecureCryptoUsage")
public void testKeyGeneration(int keysize) throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("DSA");
generator.initialize(keysize);
KeyPair keyPair = generator.generateKeyPair();
DSAPrivateKey priv = (DSAPrivateKey) keyPair.getPrivate();
DSAParams params = priv.getParams();
assertEquals(keysize, params.getP().bitLength());
// The NIST standard does not fully specify the size of q that
// must be used for a given key size. Hence there are differences.
// For example if keysize = 2048, then OpenSSL uses 256 bit q's by default,
// but the SUN provider uses 224 bits. Both are acceptable sizes.
// The tests below simply asserts that the size of q does not decrease the
// overall security of the DSA.
int qsize = params.getQ().bitLength();
switch (keysize) {
case 1024:
assertTrue("Invalid qsize for 1024 bit key:" + qsize, qsize >= 160);
break;
case 2048:
assertTrue("Invalid qsize for 2048 bit key:" + qsize, qsize >= 224);
break;
case 3072:
assertTrue("Invalid qsize for 3072 bit key:" + qsize, qsize >= 256);
break;
default:
fail("Invalid key size:" + keysize);
}
// Check the length of the private key.
// For example GPG4Browsers or the KJUR library derived from it use
// q.bitCount() instead of q.bitLength() to determine the size of the private key
// and hence would generate keys that are much too small.
assertTrue(priv.getX().bitLength() >= qsize - 32);
}
示例5: dsaPrivateKeyToBlob
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
private static byte[] dsaPrivateKeyToBlob(DSAPrivateKey dsaPrivKey) throws CryptoException {
try {
DSAParams dsaParams = dsaPrivKey.getParams();
ByteBuffer bb = ByteBuffer.wrap(new byte[512]); // 328 sufficient for a 1024 bit DSA key
bb.order(ByteOrder.LITTLE_ENDIAN);
// Write out the blob fields
UnsignedUtil.putInt(bb, DSS_PRIV_MAGIC); // dsspubkey.magic
BigInteger prime = dsaParams.getP();
int bitLength = prime.toString(2).length();
UnsignedUtil.putInt(bb, bitLength); // dsspubkey.bitlen
/*
* Unlike RSA there are no bit length remainders (ie DSA bit length
* always divisible by 8 as they are multiples of 64)
*/
writeBigInteger(bb, dsaParams.getP(), (bitLength / 8)); // modulus
writeBigInteger(bb, dsaParams.getQ(), 20); // prime
writeBigInteger(bb, dsaParams.getG(), (bitLength / 8)); // generator
writeBigInteger(bb, dsaPrivKey.getX(), 20); // secret exponent
UnsignedUtil.putInt(bb, 0xffffffff); // dssseed.counter - none, fill 0xff
for (int i = 0; i < 20; i++) // dssseed.seed - none, fill 0xff
{
bb.put((byte) 0xff);
}
return getBufferBytes(bb);
} catch (IOException ex) {
throw new CryptoException(res.getString("NoConvertKeyToBlob.exception.message"), ex);
}
}
示例6: initSign
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
/** {@inheritDoc} */
public void initSign() {
if (signKey == null) {
throw new IllegalStateException(
"Sign key must be set prior to initialization.");
}
final DSAPrivateKey privKey = (DSAPrivateKey) signKey;
final DSAParams params = privKey.getParams();
final DSAPrivateKeyParameters bcParams = new DSAPrivateKeyParameters(
privKey.getX(), new DSAParameters(params.getP(), params.getQ(),
params.getG()));
init(true, bcParams);
}
示例7: checkPrivateIntegers
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
private void checkPrivateIntegers(DSAPrivateKey pk) {
DSAParams params = pk.getParams();
assertEquals("failure for 'pk.getX().compareTo(privateX)'", 0, pk
.getX().compareTo(privateX));
assertEquals("failure for 'params.getP().compareTo(privateP)'", 0,
params.getP().compareTo(privateP));
assertEquals("failure for 'params.getQ().compareTo(privateQ)'", 0,
params.getQ().compareTo(privateQ));
assertEquals("failure for 'params.getG().compareTo(privateG)'", 0,
params.getG().compareTo(privateG));
}
示例8: JDKDSAPrivateKey
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
JDKDSAPrivateKey(
DSAPrivateKey key)
{
this.x = key.getX();
this.dsaSpec = key.getParams();
}
示例9: BCDSAPrivateKey
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
BCDSAPrivateKey(
DSAPrivateKey key)
{
this.x = key.getX();
this.dsaSpec = key.getParams();
}
示例10: testBasic
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
/**
* This is just a test for basic functionality of DSA. The test generates a public and private
* key, generates a signature, verifies it and prints the whole thing out. This test is useful
* when an implementation is seriously broken.
*/
@SlowTest(providers = {ProviderType.BOUNCY_CASTLE, ProviderType.SPONGY_CASTLE})
@SuppressWarnings("InsecureCryptoUsage")
@Test
public void testBasic() throws Exception {
int keySize = 2048;
String algorithm = "SHA256WithDSA";
String hashAlgorithm = "SHA-256";
String message = "Hello";
byte[] messageBytes = message.getBytes("UTF-8");
KeyPairGenerator generator = java.security.KeyPairGenerator.getInstance("DSA");
generator.initialize(keySize);
KeyPair keyPair = generator.generateKeyPair();
DSAPublicKey pub = (DSAPublicKey) keyPair.getPublic();
DSAPrivateKey priv = (DSAPrivateKey) keyPair.getPrivate();
Signature signer = Signature.getInstance(algorithm);
Signature verifier = Signature.getInstance(algorithm);
signer.initSign(priv);
signer.update(messageBytes);
byte[] signature = signer.sign();
verifier.initVerify(pub);
verifier.update(messageBytes);
assertTrue(verifier.verify(signature));
// Extract some parameters.
byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
DSAParams params = priv.getParams();
// Print keys and signature, so that it can be used to generate new test vectors.
System.out.println("Message:" + message);
System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
System.out.println("Params:");
System.out.println("p:" + params.getP().toString());
System.out.println("q:" + params.getQ().toString());
System.out.println("g:" + params.getG().toString());
System.out.println("Private key:");
System.out.println("X:" + priv.getX().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
System.out.println("Public key:");
System.out.println("Y:" + pub.getY().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
System.out.println("Signature:" + TestUtil.bytesToHex(signature));
System.out.println("r:" + extractR(signature).toString());
System.out.println("s:" + extractS(signature).toString());
}
示例11: get
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
/**
* OpenSSL encode a private key.
*
* @return The encoding
* @param privateKey
* The private key
* @throws CryptoException
* Problem encountered while getting the encoded private key
*/
public static byte[] get(PrivateKey privateKey) throws CryptoException {
// DER encoding for each key type is a sequence
ASN1EncodableVector vec = new ASN1EncodableVector();
if (privateKey instanceof ECPrivateKey) {
try {
ECPrivateKey ecPrivKey = (ECPrivateKey) privateKey;
org.bouncycastle.asn1.sec.ECPrivateKey keyStructure = EccUtil.convertToECPrivateKeyStructure(ecPrivKey);
return keyStructure.toASN1Primitive().getEncoded();
} catch (IOException e) {
throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), e);
}
} else if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
vec.add(new ASN1Integer(VERSION));
vec.add(new ASN1Integer(rsaPrivateKey.getModulus()));
vec.add(new ASN1Integer(rsaPrivateKey.getPublicExponent()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrivateExponent()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeP()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeQ()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentP()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentQ()));
vec.add(new ASN1Integer(rsaPrivateKey.getCrtCoefficient()));
} else {
DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
DSAParams dsaParams = dsaPrivateKey.getParams();
BigInteger primeModulusP = dsaParams.getP();
BigInteger primeQ = dsaParams.getQ();
BigInteger generatorG = dsaParams.getG();
BigInteger secretExponentX = dsaPrivateKey.getX();
// Derive public key from private key parts, ie Y = G^X mod P
BigInteger publicExponentY = generatorG.modPow(secretExponentX, primeModulusP);
vec.add(new ASN1Integer(VERSION));
vec.add(new ASN1Integer(primeModulusP));
vec.add(new ASN1Integer(primeQ));
vec.add(new ASN1Integer(generatorG));
vec.add(new ASN1Integer(publicExponentY));
vec.add(new ASN1Integer(secretExponentX));
}
DERSequence derSequence = new DERSequence(vec);
try {
return derSequence.getEncoded();
} catch (IOException ex) {
throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), ex);
}
}
示例12: BaseDSAPrivateKey
import java.security.interfaces.DSAPrivateKey; //导入方法依赖的package包/类
public BaseDSAPrivateKey(DSAPrivateKey key){
X=key.getX();
DSAParams p=key.getParams();
params=new RawDSAParams(p.getP(),p.getQ(),p.getG());
}