我们可以使用keytool和java.security API(如KeyPairGenerator等)做任何事情吗
我们可以使用keytool和java.security API(如KeyPairGenerator等)做任何事情吗 我对延长证书的有效期感兴趣 例如,可以使用Java安全API运行以下命令吗 keytool-genkeypair{-alias alias}{-keyalg keyalg}{-keysize keysize}{-sigalg sigalg}[-dname dname][-keypass keypass]{-validity valDays}{-storetype storetype}我们可以使用keytool和java.security API(如KeyPairGenerator等)做任何事情吗,java,security,api,keytool,Java,Security,Api,Keytool,我们可以使用keytool和java.security API(如KeyPairGenerator等)做任何事情吗 我对延长证书的有效期感兴趣 例如,可以使用Java安全API运行以下命令吗 keytool-genkeypair{-alias alias}{-keyalg keyalg}{-keysize keysize}{-sigalg sigalg}[-dname dname][-keypass keypass]{-validity valDays}{-storetype storetype
我只想使用java核心安全API,对第三方API不感兴趣大多数
keytool
(至少我知道的那些)的操作都可以使用java.security.*
类和一些传统的实用程序类重新创建,例如,要创建一对新的密钥,您可以使用:
private static final String ALGORITHM = "RSA";
private static final String PROVIDER = "BC";
private PrivateKey privateKey;
private PublicKey publicKey;
...
public void generateNewKeyPair() {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM, PROVIDER);
keyGen.initialize(2048, new SecureRandom());
KeyPair keypair = keyGen.genKeyPair();
privateKey = keypair.getPrivate();
publicKey = keypair.getPublic();
} catch (Exception e) {
LOG.error("Error creating keyPair", e);
}
}
这是来自密钥库的密钥对
下面是一个(更详细的)示例,它不仅创建密钥对
,而且还将其存储在文件中
您还可以将密钥对
与到期时间戳一起序列化,以模拟有效性
参数和密钥工具
编辑:单独不会给您有效性
参数模拟,是与密钥对(在密封对象
中)一起存储的时间戳,它将“模拟”过期日期(可以视为密钥的有效性)。例如:
class KeyWithExpiration {
private PublicKey publicKey;
private Date expirationDate;
}
public static void serializeEncrypted(File file, Serializable instance) {
// With these lines, I hope to expose some of the craft that is needed to work with the API
PBEKeySpec keySpecObj = new PBEKeySpec(PASSWORD, SALT, ITERATIONS);
Cipher ecipherObj = Cipher.getInstance(keyObj.getAlgorithm());
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey keyObj = secretKeyFactory.generateSecret(keySpecObj);
SealedObject sealedObject = new SealedObject(instance, ecipherObj);
ObjectOutputStream objOutputStream = new ObjectOutputStream(new FileOutputStream(file));
objOutputStream.writeObject(sealedObject);
objOutputStream.close();
}
// Generate a new KeyWithExpiration
KeyWithExpiration key = new KeyWithExpiration(keyPair, DateUtil.future().days(365));
serializeEncrypted(new File(".key"), key);
这就是为什么需要API加上一些实用程序类来实现keytool
提供的一些功能。请澄清使用SealeObject的有效性。我在SealeObject中没有看到任何用于设置有效期/到期的API。谢谢