Package java.security.spec

Examples of java.security.spec.X509EncodedKeySpec


    harness.checkPoint("testSymmetry()");

    try
      {
        encoded = publicK.getEncoded();
        X509EncodedKeySpec spec1 = new X509EncodedKeySpec(encoded);
        PublicKey k1 = keyFactory.generatePublic(spec1);
        harness.check(k1.equals(publicK), "Public DSS keys MUST be equal");

        encoded = privateK.getEncoded();
        PKCS8EncodedKeySpec spec2 = new PKCS8EncodedKeySpec(encoded);
View Full Code Here


        aliceKA.init(alicePrivateK);
        byte[] aliceEncodedK = alicePublicK.getEncoded();

        harness.verbose("*** Alice sends Bob her encoded key...");

        X509EncodedKeySpec aliceKAtBob = new X509EncodedKeySpec(aliceEncodedK);
        PublicKey aliceK = bobKF.generatePublic(aliceKAtBob);
        bobKA.init(bobPrivateK);
        bobKA.doPhase(aliceK, true);
        byte[] bobSharedSecret = bobKA.generateSecret(); // bob ready
        byte[] bobEncodedK = bobPublicK.getEncoded();

        harness.verbose("*** Bob sends Alice his encoded key...");

        X509EncodedKeySpec bobKAtAlice = new X509EncodedKeySpec(bobEncodedK);
        PublicKey bobK = aliceKF.generatePublic(bobKAtAlice);
        aliceKA.doPhase(bobK, true);
        byte[] aliceSharedSecret = aliceKA.generateSecret(); // alice ready

        harness.check(Arrays.equals(aliceSharedSecret, bobSharedSecret),
View Full Code Here

        harness.verbose("*** Alice sends Bob her encoded key...");
        byte[] alicePubKEnc = aliceKP.getPublic().getEncoded();

        // Bob side
        KeyFactory bobKF = KeyFactory.getInstance("DH");
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(alicePubKEnc);
        PublicKey alicePubK = bobKF.generatePublic(x509KeySpec);

        // Bob gets the DH parameters associated with Alice's public key.
        // He MUST use the same parameters when generating his own key-pair
        DHParameterSpec spec2 = ((DHPublicKey) alicePubK).getParams();
        harness.verbose("*** Generating Bob's Diffie-Hellman key-pair");
        KeyPairGenerator bobKPG = KeyPairGenerator.getInstance("DH");
        bobKPG.initialize(spec2);
        KeyPair bobKP = bobKPG.generateKeyPair();

        harness.verbose("*** Initializing Bob's Diffie-Hellman key-agreement");
        KeyAgreement bobKA = KeyAgreement.getInstance("DH");
        bobKA.init(bobKP.getPrivate());

        harness.verbose("*** Bob sends Alice his encoded key...");
        byte[] bobPubKEnc = bobKP.getPublic().getEncoded();

        // Alice uses Bob's public key for the one and only phase of her
        // version of the DH protocol.  Before she can do that, she has to
        // re-generate Bob's DH public key from his encoded key material
        KeyFactory aliceKF = KeyFactory.getInstance("DH");
        x509KeySpec = new X509EncodedKeySpec(bobPubKEnc);
        PublicKey bobPubK = aliceKF.generatePublic(x509KeySpec);
        harness.verbose("*** Alice does phase #1");
        aliceKA.doPhase(bobPubK, true);

        // Bob uses Alice's public key for the first and only phase of his
View Full Code Here

    harness.checkPoint("testSymmetry()");

    try
      {
        encoded = publicK.getEncoded();
        X509EncodedKeySpec spec1 = new X509EncodedKeySpec(encoded);
        PublicKey k1 = keyFactory.generatePublic(spec1);
        harness.check(k1.equals(publicK), "Public DH keys MUST be equal");

        encoded = privateK.getEncoded();
        PKCS8EncodedKeySpec spec2 = new PKCS8EncodedKeySpec(encoded);
View Full Code Here

    private PublicKey readPublicKeyFromFile(String fileName) throws IOException {
        InputStream base64InputStream = new Base64.InputStream(getClass().getResourceAsStream(fileName));
        byte[] data = new byte[base64InputStream.available()];
        base64InputStream.read(data);
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(data);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return keyFactory.generatePublic(publicKeySpec);
        } catch (NoSuchAlgorithmException e) {
            throw new EveManageSecurityException(e);
View Full Code Here

        ASN1Sequence    crmfSeq = (ASN1Sequence) in.readObject();
        ASN1Sequence reqSeq =  (ASN1Sequence) ((ASN1Sequence) crmfSeq.getObjectAt(0)).getObjectAt(0);
        CertRequest certReq = new CertRequest( reqSeq );
        SubjectPublicKeyInfo pKeyInfo = certReq.getCertTemplate().getPublicKey();
        KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
        KeySpec keySpec = new X509EncodedKeySpec( pKeyInfo.getEncoded() );
        PublicKey pubKey = keyFact.generatePublic(keySpec); // just check it's ok
        imsg = new SimpleRequestMessage(pubKey, username, password);
        // a simple crmf is not a complete PKI message, as desired by the CrmfRequestMessage class
        //PKIMessage msg = PKIMessage.getInstance(new ASN1InputStream(new ByteArrayInputStream(request)).readObject());
        //CrmfRequestMessage reqmsg = new CrmfRequestMessage(msg, null, true, null);
View Full Code Here

        priKey = keyFac.generatePrivate(encodedKeySpec);
    }

    protected void retrievePubKey(File keyFile) throws Exception {
        byte[] pubKeyByte = readByte(keyFile);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKeyByte);
        KeyFactory keyFact = KeyFactory.getInstance(this.algorithm);
        pubKey = keyFact.generatePublic(x509KeySpec);
    }
View Full Code Here

    @Override
    public void decrypt(String inputFile, String outputSignature, File keyFile) {
        try {
            byte[] encKey = readByte(keyFile);

            X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);

            KeyFactory keyFactory = KeyFactory.getInstance(this.algorithm,"SUN");
            PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

            /* input the signature bytes */
 
View Full Code Here

        ASN1Sequence    crmfSeq = (ASN1Sequence) in.readObject();
        ASN1Sequence reqSeq =  (ASN1Sequence) ((ASN1Sequence) crmfSeq.getObjectAt(0)).getObjectAt(0);
        CertRequest certReq = new CertRequest( reqSeq );
        SubjectPublicKeyInfo pKeyInfo = certReq.getCertTemplate().getPublicKey();
        KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
        KeySpec keySpec = new X509EncodedKeySpec( pKeyInfo.getEncoded() );
        PublicKey pubKey = keyFact.generatePublic(keySpec); // just check it's ok
        imsg = new SimpleRequestMessage(pubKey, username, password);
        // a simple crmf is not a complete PKI message, as desired by the CrmfRequestMessage class
        //PKIMessage msg = PKIMessage.getInstance(new ASN1InputStream(new ByteArrayInputStream(request)).readObject());
        //CrmfRequestMessage reqmsg = new CrmfRequestMessage(msg, null, true, null);
        //imsg = reqmsg;
      } else if (reqType == SecConst.CERT_REQ_TYPE_PUBLICKEY) {
        byte[] request;
        // Request can be Base64 encoded or in PEM format
        try {
          request = FileTools.getBytesFromPEM(req.getBytes(), CertTools.BEGIN_PUBLIC_KEY, CertTools.END_PUBLIC_KEY);
        } catch (IOException ex) {
          try {
            request = Base64.decode(req.getBytes());
            if (request == null) {
              throw new IOException("Base64 decode of buffer returns null");
            }         
          } catch (ArrayIndexOutOfBoundsException ae) {
            throw new IOException("Base64 decode fails, message not base64 encoded: " + ae.getMessage());
          }
        }
        final ASN1InputStream in = new ASN1InputStream(request);
        final SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(in.readObject());
        final AlgorithmIdentifier keyAlg = keyInfo.getAlgorithmId();
        final X509EncodedKeySpec xKeySpec = new X509EncodedKeySpec(new DERBitString(keyInfo).getBytes());
        final KeyFactory keyFact = KeyFactory.getInstance(keyAlg.getObjectId().getId(), "BC");
        final PublicKey pubKey = keyFact.generatePublic(xKeySpec);
        imsg = new SimpleRequestMessage(pubKey, username, password);
      }
      if (imsg != null) {
View Full Code Here

    final PublicKey pk = getPublicKey(keyInfo, "BC");
    return pk;
  }
  private PublicKey getPublicKey(final SubjectPublicKeyInfo subjectPKInfo, final String  provider) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException {   
    try {
      final X509EncodedKeySpec xspec = new X509EncodedKeySpec(new DERBitString(subjectPKInfo).getBytes());
      final AlgorithmIdentifier keyAlg = subjectPKInfo.getAlgorithmId ();
      return KeyFactory.getInstance(keyAlg.getObjectId().getId (), provider).generatePublic(xspec);
    } catch (java.security.spec.InvalidKeySpecException e) {
      final InvalidKeyException newe = new InvalidKeyException("Error decoding public key.");
      newe.initCause(e);
View Full Code Here

TOP

Related Classes of java.security.spec.X509EncodedKeySpec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.