当前位置: 首页>>代码示例>>Java>>正文


Java AlgorithmParameters.init方法代码示例

本文整理汇总了Java中java.security.AlgorithmParameters.init方法的典型用法代码示例。如果您正苦于以下问题:Java AlgorithmParameters.init方法的具体用法?Java AlgorithmParameters.init怎么用?Java AlgorithmParameters.init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.security.AlgorithmParameters的用法示例。


在下文中一共展示了AlgorithmParameters.init方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getAlgorithmParameters

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
private AlgorithmParameters getAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(), iterationCount);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getAlgorithmParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:18,代码来源:PKCS12KeyStore.java

示例2: createRawSignature

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
public Signature createRawSignature(AlgorithmIdentifier algorithm)
{
    Signature   sig;

    try
    {
        String algName = getSignatureName(algorithm);

        algName = "NONE" + algName.substring(algName.indexOf("WITH"));

        sig = helper.createSignature(algName);

        // RFC 4056
        // When the id-RSASSA-PSS algorithm identifier is used for a signature,
        // the AlgorithmIdentifier parameters field MUST contain RSASSA-PSS-params.
        if (algorithm.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS))
        {
            AlgorithmParameters params = helper.createAlgorithmParameters(algName);

            params.init(algorithm.getParameters().toASN1Primitive().getEncoded(), "ASN.1");

            PSSParameterSpec spec = (PSSParameterSpec)params.getParameterSpec(PSSParameterSpec.class);
            sig.setParameter(spec);
        }
    }
    catch (Exception e)
    {
        return null;
    }

    return sig;
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:33,代码来源:OperatorHelper.java

示例3: decodeAlgorithmParameters

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public AlgorithmParameters decodeAlgorithmParameters(byte[] encoded)
{
    try
    {
        AlgorithmParameters p = null;
        String algorithm = "DESede";
        if(getCipherProvider() != null)
        {
            p = AlgorithmParameters.getInstance(algorithm, getCipherProvider());
        }
        else
        {
            p = AlgorithmParameters.getInstance(algorithm);
        }
        p.init(encoded);
        return p;
    }
    catch(Exception e)
    {
        throw new AlfrescoRuntimeException("", e);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-core,代码行数:27,代码来源:AbstractEncryptor.java

示例4: getPublicKey

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
/**
 * Returns the ECPublicKey instance from its encoded raw bytes. 
 * The first byte has the fixed value 0x04 indicating the uncompressed form.
 * Therefore, the byte array must be of form: [0x04, x coord of point (32 bytes), y coord of point (32 bytes)]
 * 
 * @param publicKeyBytes The byte array representing the encoded raw bytes of the public key
 * @return The ECPublicKey instance
 */
public static ECPublicKey getPublicKey(byte[] publicKeyBytes) {
	// First we separate x and y of coordinates into separate variables
    byte[] x = new byte[32];
    byte[] y = new byte[32];
    System.arraycopy(publicKeyBytes, 1, x, 0, 32);
    System.arraycopy(publicKeyBytes, 33, y, 0, 32);
    
    try {
		KeyFactory kf = KeyFactory.getInstance("EC");
		
		AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
		parameters.init(new ECGenParameterSpec("secp256r1"));
		ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
		
		ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), ecParameterSpec);
		ECPublicKey ecPublicKey = (ECPublicKey) kf.generatePublic(ecPublicKeySpec);
		return ecPublicKey;
    } catch (NoSuchAlgorithmException | InvalidParameterSpecException | InvalidKeySpecException e) {
		getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get public key from raw bytes", e);
        return null;
	}
}
 
开发者ID:V2GClarity,项目名称:RISE-V2G,代码行数:31,代码来源:SecurityUtils.java

示例5: isAvailableCurve

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
private static boolean isAvailableCurve(int curveId) {
    String oid = idToOidMap.get(curveId);
    if (oid != null) {
        AlgorithmParameters params = null;
        try {
            params = JsseJce.getAlgorithmParameters("EC");
            params.init(new ECGenParameterSpec(oid));
        } catch (Exception e) {
            return false;
        }

        // cache the parameters
        idToParams.put(curveId, params);

        return true;
    }

    return false;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:20,代码来源:SupportedEllipticCurvesExtension.java

示例6: parseAlgParameters

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
private AlgorithmParameters parseAlgParameters(ObjectIdentifier algorithm,
    DerInputStream in) throws IOException
{
    AlgorithmParameters algParams = null;
    try {
        DerValue params;
        if (in.available() == 0) {
            params = null;
        } else {
            params = in.getDerValue();
            if (params.tag == DerValue.tag_Null) {
               params = null;
            }
        }
        if (params != null) {
            if (algorithm.equals(pbes2_OID)) {
                algParams = AlgorithmParameters.getInstance("PBES2");
            } else {
                algParams = AlgorithmParameters.getInstance("PBE");
            }
            algParams.init(params.toByteArray());
        }
    } catch (Exception e) {
       throw new IOException("parseAlgParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:PKCS12KeyStore.java

示例7: engineGenerateParameters

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
protected AlgorithmParameters engineGenerateParameters()
{
    GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();

    if (random != null)
    {
        pGen.init(strength, 2, random);
    }
    else
    {
        pGen.init(strength, 2, new SecureRandom());
    }

    GOST3410Parameters p = pGen.generateParameters();

    AlgorithmParameters params;

    try
    {
        params = AlgorithmParameters.getInstance("GOST3410", BouncyCastleProvider.PROVIDER_NAME);
        params.init(new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec(p.getP(), p.getQ(), p.getA())));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e.getMessage());
    }

    return params;
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:30,代码来源:AlgorithmParameterGeneratorSpi.java

示例8: engineGenerateParameters

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
@Override
     protected AlgorithmParameters engineGenerateParameters()
     {
         DSAParametersGenerator pGen = new DSAParametersGenerator();

if ( random != null )
{
	pGen.init(strength, 20, random);
}
else
{
	pGen.init(strength, 20, new SecureRandom());
}

         DSAParameters p = pGen.generateParameters();

         AlgorithmParameters params;

         try
         {
             params = AlgorithmParameters.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
             params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
         }
         catch (Exception e)
         {
             throw new RuntimeException(e.getMessage());
         }

         return params;
     }
 
开发者ID:BiglySoftware,项目名称:BiglyBT,代码行数:31,代码来源:JDKAlgorithmParameterGenerator.java

示例9: testParams

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
private static byte[] testParams(AlgorithmParameters rc2Params,
    RC2ParameterSpec rc2Spec) throws Exception {

    // test getParameterSpec returns object equal to input
    rc2Params.init(rc2Spec);
    RC2ParameterSpec rc2OtherSpec = (RC2ParameterSpec)
        rc2Params.getParameterSpec(RC2ParameterSpec.class);
    if (!rc2Spec.equals(rc2OtherSpec)) {
        throw new Exception("AlgorithmParameterSpecs should be equal");
    }

    // test RC2ParameterSpec with RC2 Cipher
    Cipher rc2Cipher = Cipher.getInstance("RC2/CBC/PKCS5PADDING", "SunJCE");
    rc2Cipher.init(Cipher.ENCRYPT_MODE,
        new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), rc2Spec);

    // get IV
    byte[] iv = rc2Cipher.getIV();
    if (!Arrays.equals(iv, rc2Spec.getIV())) {
        throw new Exception("ivs should be equal");
    }

    // test encoding and decoding
    byte[] encoded = rc2Params.getEncoded();
    AlgorithmParameters params = AlgorithmParameters.getInstance("RC2");
    params.init(encoded);

    // test RC2 AlgorithmParameters with RC2 Cipher
    rc2Cipher.init(Cipher.ENCRYPT_MODE,
        new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), params);

    // get IV
    iv = rc2Cipher.getIV();
    if (!Arrays.equals(iv, rc2Spec.getIV())) {
        throw new Exception("ivs should be equal");
    }
    return encoded;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:RC2AlgorithmParameters.java

示例10: decrypt

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
public static byte[] decrypt(byte[] bArr, String str, byte[] bArr2) throws Exception {
    Cipher instance = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters instance2 = AlgorithmParameters.getInstance("AES");
    instance2.init(new IvParameterSpec(bArr2));
    Key bй0439йй0439й = bй0439йй0439й(str);
    if (((b0429Щ0429Щ0429Щ + b04290429ЩЩ0429Щ) * b0429Щ0429Щ0429Щ) % bЩЩ0429Щ0429Щ != bЩ0429ЩЩ0429Щ) {
        b0429Щ0429Щ0429Щ = 87;
        bЩ0429ЩЩ0429Щ = 42;
    }
    instance.init(2, bй0439йй0439й, instance2);
    return instance.doFinal(bArr);
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:13,代码来源:rrrccc.java

示例11: protect

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
/**
 * Protects the given cleartext private key, using the password provided at
 * construction time.
 */
byte[] protect(PrivateKey key)
    throws Exception
{
    // create a random salt (8 bytes)
    byte[] salt = new byte[8];
    SunJCE.getRandom().nextBytes(salt);

    // create PBE parameters from salt and iteration count
    PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);

    // create PBE key from password
    PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
    SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
    pbeKeySpec.clearPassword();

    // encrypt private key
    PBEWithMD5AndTripleDESCipher cipher;
    cipher = new PBEWithMD5AndTripleDESCipher();
    cipher.engineInit(Cipher.ENCRYPT_MODE, sKey, pbeSpec, null);
    byte[] plain = key.getEncoded();
    byte[] encrKey = cipher.engineDoFinal(plain, 0, plain.length);

    // wrap encrypted private key in EncryptedPrivateKeyInfo
    // (as defined in PKCS#8)
    AlgorithmParameters pbeParams =
        AlgorithmParameters.getInstance("PBE", SunJCE.getInstance());
    pbeParams.init(pbeSpec);

    AlgorithmId encrAlg = new AlgorithmId
        (new ObjectIdentifier(PBE_WITH_MD5_AND_DES3_CBC_OID), pbeParams);
    return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded();
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:37,代码来源:KeyProtector.java

示例12: main

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        // Prepare a JKS keystore with many entries
        new File(JKSFILE).delete();
        for (int i=0; i<SIZE; i++) {
            System.err.print(".");
            String cmd = "-keystore " + JKSFILE
                    + " -storepass changeit -keypass changeit -keyalg rsa "
                    + "-genkeypair -alias p" + i + " -dname CN=" + i;
            sun.security.tools.keytool.Main.main(cmd.split(" "));
        }

        // Prepare EncryptedPrivateKeyInfo parameters, copied from various
        // places in PKCS12KeyStore.java
        AlgorithmParameters algParams =
                AlgorithmParameters.getInstance("PBEWithSHA1AndDESede");
        algParams.init(new PBEParameterSpec("12345678".getBytes(), 1024));
        AlgorithmId algid = new AlgorithmId(
                new ObjectIdentifier("1.2.840.113549.1.12.1.3"), algParams);

        PBEKeySpec keySpec = new PBEKeySpec(PASSWORD);
        SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
        SecretKey skey = skFac.generateSecret(keySpec);

        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);

        // Pre-calculated keys and certs and aliases
        byte[][] keys = new byte[SIZE][];
        Certificate[][] certChains = new Certificate[SIZE][];
        String[] aliases = new String[SIZE];

        // Reads from JKS keystore and pre-calculate
        KeyStore ks = KeyStore.getInstance("jks");
        try (FileInputStream fis = new FileInputStream(JKSFILE)) {
            ks.load(fis, PASSWORD);
        }
        for (int i=0; i<SIZE; i++) {
            aliases[i] = "p" + i;
            byte[] enckey = cipher.doFinal(
                    ks.getKey(aliases[i], PASSWORD).getEncoded());
            keys[i] = new EncryptedPrivateKeyInfo(algid, enckey).getEncoded();
            certChains[i] = ks.getCertificateChain(aliases[i]);
        }

        // Write into PKCS12 keystore. Use this overloaded version of
        // setKeyEntry() to be as fast as possible, so that they would
        // have same localKeyId.
        KeyStore p12 = KeyStore.getInstance("pkcs12");
        p12.load(null, PASSWORD);
        for (int i=0; i<SIZE; i++) {
            p12.setKeyEntry(aliases[i], keys[i], certChains[i]);
        }
        try (FileOutputStream fos = new FileOutputStream(P12FILE)) {
            p12.store(fos, PASSWORD);
        }

        // Check private keys still match certs
        p12 = KeyStore.getInstance("pkcs12");
        try (FileInputStream fis = new FileInputStream(P12FILE)) {
            p12.load(fis, PASSWORD);
        }
        for (int i=0; i<SIZE; i++) {
            String a = "p" + i;
            X509Certificate x = (X509Certificate)p12.getCertificate(a);
            X500Name name = (X500Name)x.getSubjectDN();
            if (!name.getCommonName().equals(""+i)) {
                throw new Exception(a + "'s cert is " + name);
            }
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:72,代码来源:PKCS12SameKeyId.java

示例13: isPrivateKeyValid

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
/**
 * Checks if the private key is a valid key (according to requirement [V2G2-823]) for the received contract 
 * certificate before saving it to the keystore.
 * @param privateKey The private key corresponding to the contract certificate
 * @param contractCertChain The received contract certificate chain 
 * @return True, if the private key is a valid key, false otherwise.
 */
private static boolean isPrivateKeyValid(ECPrivateKey privateKey, CertificateChainType contractCertChain) {
	AlgorithmParameters parameters;
	
	try {
		parameters = AlgorithmParameters.getInstance("EC");
		parameters.init(new ECGenParameterSpec("secp256r1"));
		
		ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
		
		// Now we need to check if the private key is correct (see requirement [V2G2-823]) 
		BigInteger order = ecParameterSpec.getOrder();
		ECPoint basePoint = ecParameterSpec.getGenerator();
		BigInteger privateKeyValue = privateKey.getS();
		X509Certificate contractCert = getCertificate(contractCertChain.getCertificate());
		ECPublicKey publicKey = (ECPublicKey) contractCert.getPublicKey();
		
		// 1. check
		if (privateKeyValue.compareTo(order) != -1) {
			getLogger().error("Validation of private key failed: its value is not strictly smaller than the "
							+ "order of the base point");
			return false;
		}
		
		// 2. check
		/*
		 * TODO: 
		 * No idea how to check for 
		 * "multiplication of the base point with this value must generate a key matching the public key of 
		 * the contract certificate"
		 * "this value" = value of private key
		 * -> some more expert knowledge on the arithmetic of elliptic curves is needed to tackle this!
		 */
		
	} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) {
		getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get private key from raw bytes", e);
		return false;
	}
	
	return true;
}
 
开发者ID:V2GClarity,项目名称:RISE-V2G,代码行数:48,代码来源:SecurityUtils.java

示例14: recover

import java.security.AlgorithmParameters; //导入方法依赖的package包/类
Key recover(EncryptedPrivateKeyInfo encrInfo)
    throws UnrecoverableKeyException, NoSuchAlgorithmException
{
    byte[] plain;

    try {
        String encrAlg = encrInfo.getAlgorithm().getOID().toString();
        if (!encrAlg.equals(PBE_WITH_MD5_AND_DES3_CBC_OID)
            && !encrAlg.equals(KEY_PROTECTOR_OID)) {
            throw new UnrecoverableKeyException("Unsupported encryption "
                                                + "algorithm");
        }

        if (encrAlg.equals(KEY_PROTECTOR_OID)) {
            // JDK 1.2 style recovery
            plain = recover(encrInfo.getEncryptedData());
        } else {
            byte[] encodedParams =
                encrInfo.getAlgorithm().getEncodedParams();

            // parse the PBE parameters into the corresponding spec
            AlgorithmParameters pbeParams =
                AlgorithmParameters.getInstance("PBE");
            pbeParams.init(encodedParams);
            PBEParameterSpec pbeSpec =
                    pbeParams.getParameterSpec(PBEParameterSpec.class);

            // create PBE key from password
            PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
            SecretKey sKey =
                new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
            pbeKeySpec.clearPassword();

            // decrypt private key
            PBEWithMD5AndTripleDESCipher cipher;
            cipher = new PBEWithMD5AndTripleDESCipher();
            cipher.engineInit(Cipher.DECRYPT_MODE, sKey, pbeSpec, null);
            plain=cipher.engineDoFinal(encrInfo.getEncryptedData(), 0,
                                       encrInfo.getEncryptedData().length);
        }

        // determine the private-key algorithm, and parse private key
        // using the appropriate key factory
        String oidName = new AlgorithmId
            (new PrivateKeyInfo(plain).getAlgorithm().getOID()).getName();
        KeyFactory kFac = KeyFactory.getInstance(oidName);
        return kFac.generatePrivate(new PKCS8EncodedKeySpec(plain));

    } catch (NoSuchAlgorithmException ex) {
        // Note: this catch needed to be here because of the
        // later catch of GeneralSecurityException
        throw ex;
    } catch (IOException ioe) {
        throw new UnrecoverableKeyException(ioe.getMessage());
    } catch (GeneralSecurityException gse) {
        throw new UnrecoverableKeyException(gse.getMessage());
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:59,代码来源:KeyProtector.java


注:本文中的java.security.AlgorithmParameters.init方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。