예제: 자동화된 키 생성을 위한 키 또는 키 쌍 개발
암호화 조작을 위해 키를 생성하는 클래스는 자동으로 작성할 수 있습니다. 이 기능을 사용하여 키 관리 인프라는 사전정의된 키 세트에 대해 키 목록을 유지보수하고 애플리케이션은 이러한 키에 액세스할 수 있습니다.
사전정의된 빈도로 새 키 생성을 스케줄할 수 있습니다. 키 생성 빈도가 데이터의 보안에 영향을 주는 것에 유의하십시오. 예를 들어, 지속적 데이터에 대해 이전 키가 만료되는 빈도보다 높은 빈도로 키가 생성되어야 하는 실시간 통신보다 낮은 빈도로 키 생성을 스케줄할 수 있습니다.
키 생성 클래스를 개발하는 경우 공유 키를 작성할지 또는 키 쌍을 작성할지 결정하십시오. 이 결정에 따라 사용해야 하는 인터페이스가 판별되기 때문입니다.
공유 키를 개발하는 경우 com.ibm.websphere.crypto.KeyGenerator 인터페이스를 구현하기 위해 KeyGenerator 클래스를 사용하는 다음 예제를 참조하십시오. 이 인터페이스는 JCEKS 키 저장소 유형의 SecretKey로 저장되는 java.security.Key key를 리턴합니다. 비밀 키 저장을 지원하는 다른 모든 키 저장소 유형을 사용할 수 있습니다.
package com.ibm.test;
import java.util.*;
import com.ibm.ws.ssl.core.*;
import com.ibm.ws.ssl.config.*;
import com.ibm.websphere.crypto.KeyException;
public class KeyGenerator implements com.ibm.websphere.crypto.KeyGenerator
{
private java.util.Properties customProperties = null;
private java.security.Key secretKey = null;
public KeyGenerator()
{
}
/**
* This method is called to pass any custom properties configured with
* the KeySet to the implementation of this interface.
*
* @param java.util.Properties
**/
public void init (java.util.Properties customProps)
{
customProperties = customProps;
}
/**
* This method is called whenever a key needs to be generated either
* from the schedule or manually requested. The key is stored in the
* KeyStore referenced by the configured KeySet that contains the
* keyGenerationClass implementing this interface. The implementation of
* this interface manages the key type. The user of the KeySet
* must know the type that is returned by this keyGenerationClass.
*
* @return java.security.Key
* @throws com.ibm.websphere.crypto.KeyException
**/
public java.security.Key generateKey () throws KeyException
{
try
{
// Assume generate3DESKey is there to create the key.
byte[] tripleDESKey = generate3DESKey();
secretKey = new javax.crypto.spec.SecretKeySpec(tripleDESKey, 0, 24, "3DES");
if (secretKey != null)
{
return secretKey;
}
else
{
throw new com.ibm.websphere.crypto.KeyException ("Key could not be generated.");
}
}
catch (Exception e)
{
e.printStackTrace(); // handle exception
}
}
}
키 쌍을 개발하는 경우 com.ibm.websphere.crypto.KeyPairGenerator 인터페이스를 구현하기 위해 KeyPairGenerator 클래스를 사용하는 다음 예제를 참조하십시오.
package com.ibm.test;
import java.util.*;
import javax.crypto.spec.SecretKeySpec;
import com.ibm.websphere.crypto.KeyException;
/**
* This implementation defines the method to generate a java.security.KeyPair.
* When a keyGeneration class implements this method, the generateKeyPair method
* is called and a KeyPair is stored in the keystore. The isKeyPair
* attribute is ignored since the KeyGenerationClass is an
* implementation of KeyPairGenerator. The isKeyPair attributes is for when
* the keys already exist in a KeyStore, and are just read (not
* generating them).
*
* @author IBM Corporation
* @version WebSphere Application Server 6.1
* @since WebSphere Application Server 6.1
**/
public class KeyPairGenerator implements com.ibm.websphere.crypto.KeyPairGenerator
{
private java.util.Properties customProperties = null;
public KeyPairGenerator()
{
}
/**
* This method is called to pass any custom properties configured with
* the KeySet to the implementation of this interface.
*
* @param java.util.Properties
**/
public void init (java.util.Properties customProps)
{
customProperties = customProps;
}
/**
* This method is called whenever a key needs to be generated either
* from the schedule or manually requested and isKeyPair=true in the KeySet
* configuration. The key is stored in the KeyStore referenced by
* the configured KeySet which contains the keyGenerationClass implementing
* this interface. The implementation of this interface manages the
* type of the key. The user of the KeySet must know the type that
* is returned by this keyGenerationClass.
*
* @return com.ibm.websphere.crypto.KeyPair
* @throws com.ibm.websphere.crypto.KeyException
**/
public com.ibm.websphere.crypto.KeyPair generateKeyPair () throws KeyException
{
try
{
java.security.KeyPair keyPair = generateKeyPair();
// Store as SecretKeySpec
if (keyPair != null)
{
java.security.PrivateKey privKey = keyPair.getPrivate();
java.security.PublicKey pubKey = keyPair.getPublic();
SecretKeySpec publicKeyAsSecretKey = new SecretKeySpec
(pubKey.getEncoded(), "RSA_PUBLIC");
SecretKeySpec privateKeyAsSecretKey = new SecretKeySpec
(privKey.getEncoded(), "RSA_PRIVATE");
com.ibm.websphere.crypto.KeyPair pair = new
com.ibm.websphere.crypto.KeyPair(publicKeyAsSecretKey, privateKeyAsSecretKey);
return pair;
}
else
{
throw new com.ibm.websphere.crypto.KeyException ("Key pair could
not be generated.");
}
}
catch (Exception e)
{
e.printStackTrace(); // handle exception
}
}
}
이 인터페이스는 X509Certificate 및 PrivateKey 오브젝트 또는 PublicKey 및 PrivateKey 오브젝트를 포함할 수 있는 com.ibm.websphere.crypto.KeyPair 키 쌍을 리턴합니다. com.ibm.websphere.crypto.KeyPair 인터페이스가 aX509Certificate 및 PrivateKey 오브젝트를 포함하는 경우 이 인증서 및 개인 키는 키 저장소에 저장됩니다. 따라서 모든 키 저장소 유형을 사용할 수 있습니다.
com.ibm.websphere.crypto.KeyPair 인터페이스가 PublicKey 및 PrivateKey 오브젝트를 포함하는 경우 인코딩된 값을 저장하기 위해 SecretKeySpec 오브젝트로 변환해야 합니다. WebSphere® Application Server 런타임은 키 쌍을 저장하고 비밀 키로 검색합니다. 런타임은 서버가 핸드쉐이크 중에 쌍을 검색하면 키 쌍을 다시 PublicKey 및 PrivateKey 오브젝트로 변환합니다.
- 공용 및 개인 생성자
public KeyPair(java.security.Key publicKey, java.security.Key privateKey)
- 인증서 및 개인 생성자.
public KeyPair(java.security.cert.Certificate[] certChain, java.security.Key privateKey)
이전 예제 코드는 공용 및 개인 생성자를 사용하는 KeyPairGenerator 클래스를 표시합니다. 이 클래스에 대한 각 호출에서는 새롭고 고유한 키 쌍이 생성되고 isKeyPair=true인 경우 이 클래스는 KeySet에 의해 호출되어 새 키 쌍을 작성합니다. 키 세트에서 버전 번호는 호출될 때마다 증분됩니다.