1 31 32 package org.pdfbox.pdmodel.encryption; 33 34 import java.security.Key ; 35 import java.security.KeyStore ; 36 import java.security.KeyStoreException ; 37 import java.security.NoSuchAlgorithmException ; 38 import java.security.UnrecoverableKeyException ; 39 import java.security.cert.X509Certificate ; 40 import java.util.Enumeration ; 41 42 73 74 public class PublicKeyDecryptionMaterial extends DecryptionMaterial 75 { 76 private String password = null; 77 private KeyStore keyStore = null; 78 private String alias = null; 79 80 88 89 public PublicKeyDecryptionMaterial(KeyStore keystore, String a, String pwd) 90 { 91 keyStore = keystore; 92 alias = a; 93 password = pwd; 94 } 95 96 97 104 105 public X509Certificate getCertificate() throws KeyStoreException 106 { 107 if(keyStore.size() == 1) 108 { 109 Enumeration aliases = keyStore.aliases(); 110 String keyStoreAlias = (String )aliases.nextElement(); 111 return (X509Certificate )keyStore.getCertificate(keyStoreAlias); 112 } 113 else 114 { 115 if(keyStore.containsAlias(alias)) 116 { 117 return (X509Certificate )keyStore.getCertificate(alias); 118 } 119 throw new KeyStoreException ("the keystore does not contain the given alias"); 120 } 121 } 122 123 129 public String getPassword() 130 { 131 return password; 132 } 133 134 139 public Key getPrivateKey() throws KeyStoreException 140 { 141 try 142 { 143 if(keyStore.size() == 1) 144 { 145 Enumeration aliases = keyStore.aliases(); 146 String keyStoreAlias = (String )aliases.nextElement(); 147 return keyStore.getKey(keyStoreAlias, password.toCharArray()); 148 } 149 else 150 { 151 if(keyStore.containsAlias(alias)) 152 { 153 return keyStore.getKey(alias, password.toCharArray()); 154 } 155 throw new KeyStoreException ("the keystore does not contain the given alias"); 156 } 157 } 158 catch(UnrecoverableKeyException ex) 159 { 160 throw new KeyStoreException ("the private key is not recoverable"); 161 } 162 catch(NoSuchAlgorithmException ex) 163 { 164 throw new KeyStoreException ("the algorithm necessary to recover the key is not available"); 165 } 166 } 167 } | Popular Tags |