ウェブサーバの 暗号アルゴリズムの選び方

有一份日语关于SSL加密算法的文档,后续有时间需对其进行研究。SSL加密算法在信息技术安全领域至关重要。
日语的一份关于ssl的加密算法的文档,有时间的话需要研究一下。
package jp.co.sej.ssc.cl.sclb1040; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KmsClient; import com.google.crypto.tink.aead.AeadConfig; import com.google.crypto.tink.aead.AeadParameters; import com.google.crypto.tink.aead.KmsEnvelopeAead; import com.google.crypto.tink.aead.PredefinedAeadParameters; import com.google.crypto.tink.integration.gcpkms.GcpKmsClient; import jakarta.annotation.PostConstruct; import lombok.Getter; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; /* * 暗号化部品用のTink設定クラス * * Bean登録(コンストラクタ)のタイミングで、時間のかかる初期化処理を行っておく。 */ @Log4j2 public final class Sclb1041 { //// キーテンプレート名 //private final String KEYTEMPLATE_NAME = "AES256_GCM"; //// キーテンプレート //@Getter //private KeyTemplate tinkKeyTemplate; private static final String ENVELOPE_ENCRYPTION_APP_YAML_SECTION_FIRST_KEK_URI = "envelope-encryption.keys.kek-uri.kek-uri-1"; @Getter //2023/12/05追加 定義済みAeadパラメータ private AeadParameters predefinedAeadParameters = PredefinedAeadParameters.AES256_GCM; /* * KMS未使用モード * true:KMS未使用モード / false:KMS使用モード(デフォルト)) */ @Value("${envelope_encryption_kms_use_disabled:false}") @Getter private boolean kmsUseDisabled; /* * サービスカウント(SA)キーファイルパス */ @Value("${envelope_encryption_gcp_credential_filepath:}") @Getter private String gcpCredentialFilePath; @Autowired private Environment environment; /* * コンストラクタ * * @throws RuntimeException */ public Sclb1041() throws RuntimeException { try { // Tink初期化(サポートするAEADキータイプを登録) AeadConfig.register(); //// キーテンプレート //tinkKeyTemplate = KeyTemplates.get(KEYTEMPLATE_NAME); } catch (Exception e) { throw new RuntimeException( Sclb1050.SCLM4001 + " " + Sclb1050.SCLM40011 + ":initialization failure in bean registration.", e); } } /* * KEK(kek-uri-1)のGCPリソースID取得 * * @param kekUriName application.ymlで指定するKEK(kek-uri-1)の名前 * @return application.ymlで指定するKEK(kek-uri-1)のGCPリソースID * @throws IllegalStateException */ private String getPrepareKekUriPath() { return environment.getProperty(ENVELOPE_ENCRYPTION_APP_YAML_SECTION_FIRST_KEK_URI); } /* * Spring Bootプリケーション起動後の処理 * Spring Bootプリケーション起動時にCloud KMSへのクセスを行うことで、業務処理による初回Cloud KMSクセスを高速化する。 * ・暗号化処理に使用するKEKは、"kek-uri-1"で固定。 * ・暗号化処理に使用するデータはダミーデータとして0x00のみのバイト配列を使用。 */ @PostConstruct protected void prepare() { if (!kmsUseDisabled) { // AEADプリミティブ Aead aead = null; Aead remoteAead = null; log.debug("preparing encryption started."); try { // キーセット //keysetHandle = KeysetHandle // .generateNew(KmsEnvelopeAeadKeyManager.createKeyTemplate(getPrepareKekUriPath(), // tinkKeyTemplate)); // KMSクライント KmsClient kmsClient = new GcpKmsClient(); if (gcpCredentialFilePath.isEmpty()) { // サービスカウントキー未使用(GCP環境で実行)(defaultCredentials()) //GcpKmsClient.register(Optional.of(getPrepareKekUriPath()), Optional.empty()); kmsClient.withDefaultCredentials(); } else { // サービスカウントキー使用(withCredentials(gcpCredentialFilename)) //GcpKmsClient.register(Optional.of(getPrepareKekUriPath()), Optional.of // (gcpCredentialFilePath)); kmsClient.withCredentials(gcpCredentialFilePath); } // AEADプリミティブ remoteAead = kmsClient.getAead(getPrepareKekUriPath()); //aead = keysetHandle.getPrimitive(Aead.class); aead = KmsEnvelopeAead.create(getPredefinedAeadParameters(), remoteAead); } catch (Exception e) { log.debug("Tink initialization failure in preparing encryption.KEK name:kek-uri-1." + e.getMessage()); } // 暗号化(暗号化データ後続利用無し。ダミーデータとして0x00のみのバイト配列を使用。) final byte[] preInitByte = {0}; try { if (aead != null) { aead.encrypt(preInitByte, null); } } catch (Exception e) { log.debug("preparing encryption failed.KEK name:kek-uri-1." + e.getMessage()); } log.debug("preparing encryption end."); } } } 42:package jp.co.sej.ssc.cl.sclb1040; import com.google.crypto.tink.Aead; import com.google.crypto.tink.KmsClient; import com.google.crypto.tink.aead.KmsEnvelopeAead; import com.google.crypto.tink.integration.gcpkms.GcpKmsClient; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.util.SerializationUtils; /** * 暗号化部品のロジッククラス */ @Configuration @Component @Scope("prototype") @Log4j2 public class Sclb1042 { // 定数 private static final String ENVELOPE_ENCRYPTION_APP_YAML_SECTION_KEK_URI = "envelope-encryption.keys.kek-uri."; // AEADプリミティブ //private KeysetHandle keysetHandle = null; private Aead aead = null; private Aead remoteAead = null; // Tink設定 @Autowired private Sclb1041 tinkConfig; @Autowired private Environment environment; /* * KEKのGCPリソースID取得 * * @param kekUriName application.ymlで指定するKEKの名前 * @return application.ymlで指定するKEKのGCPリソースID * @throws IllegalStateException */ private String getkekUriPath(String kekUriName) throws IllegalStateException { return environment.getRequiredProperty( ENVELOPE_ENCRYPTION_APP_YAML_SECTION_KEK_URI + kekUriName); } /* * Tink初期化 * * KEK未使用モードの場合も、application.yml上のKEKと暗号化対象項目定義のチェックは行う。 * * @param kekUriName application.ymlで指定するKEKの名前 * @throws RuntimeException */ private void init(String kekUriName) throws RuntimeException { String kekUriPath = ""; try { kekUriPath = getkekUriPath(kekUriName); } catch (Exception e) { throw new RuntimeException(Sclb1050.SCLM4001 + " " + Sclb1050.SCLM40012 + ":initialization failure. KEK URI does not defined on application.yml. KEK name:" + kekUriName + ".", e); } // GCP接続処理と暗号鍵関連の初期化 if (!tinkConfig.isKmsUseDisabled()) { try { // キーセット //keysetHandle = KeysetHandle.generateNew( // KmsEnvelopeAeadKeyManager.createKeyTemplate(kekUriPath, tinkConfig // .getTinkKeyTemplate())); // KMSクライント KmsClient kmsClient = new GcpKmsClient(); if (tinkConfig.getGcpCredentialFilePath().isEmpty()) { // サービスカウントキー未使用(GCP環境で実行)(defaultCredentials()) //GcpKmsClient.register(Optional.of(kekUriPath), Optional.empty()); kmsClient.withDefaultCredentials(); } else { // サービスカウントキー使用(withCredentials(gcpCredentialFilename)) //GcpKmsClient.register(Optional.of(kekUriPath), Optional.of(tinkConfig // .getGcpCredentialFilePath())); kmsClient.withCredentials(tinkConfig.getGcpCredentialFilePath()); } // AEADプリミティブ remoteAead = kmsClient.getAead(kekUriPath); //aead = keysetHandle.getPrimitive(Aead.class); aead = KmsEnvelopeAead.create(tinkConfig.getPredefinedAeadParameters(), remoteAead); } catch (Exception e) { throw new RuntimeException( Sclb1050.SCLM4001 + " " + Sclb1050.SCLM40013 + ":Tink initialization failure. KEK name:" + kekUriName, e); } } } /* * 暗号化(エンベロープ暗号化) * * 暗号化対象データをバイト配列にシリライズし、Tinkでエンベロープ暗号化する。 * * @param encryptTargetData 暗号化対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 暗号化済みデータ * @throws RuntimeException */ public byte[] envelopeEncrypt(String encryptTargetData, String kekUriName) throws RuntimeException { return envelopeEncrypt((Object) encryptTargetData, kekUriName); } /* * 暗号化(エンベロープ暗号化) * * 暗号化対象データをバイト配列にシリライズし、Tinkでエンベロープ暗号化する。 * * @param encryptTargetData 暗号化対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 暗号化済みデータ * @throws RuntimeException */ public byte[] envelopeEncrypt(Character encryptTargetData, String kekUriName) throws RuntimeException { return envelopeEncrypt((Object) encryptTargetData, kekUriName); } /* * 暗号化(エンベロープ暗号化) * * 暗号化対象データをバイト配列にシリライズし、Tinkでエンベロープ暗号化する。 * * @param encryptTargetData 暗号化対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 暗号化済みデータ * @throws RuntimeException */ public byte[] envelopeEncrypt(Integer encryptTargetData, String kekUriName) throws RuntimeException { return envelopeEncrypt((Object) encryptTargetData, kekUriName); } /* * 暗号化(エンベロープ暗号化) * * 暗号化対象データをバイト配列にシリライズし、Tinkでエンベロープ暗号化する。 * * @param encryptTargetData 暗号化対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 暗号化済みデータ * @throws RuntimeException */ public byte[] envelopeEncrypt(int encryptTargetData, String kekUriName) throws RuntimeException { return envelopeEncrypt((Object) encryptTargetData, kekUriName); } /* * 暗号化(エンベロープ暗号化) * * 暗号化対象データをバイト配列にシリライズし、Tinkでエンベロープ暗号化する。 * * @param encryptTargetData 暗号化対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 暗号化済みデータ * @throws RuntimeException */ public byte[] envelopeEncrypt(java.sql.Timestamp encryptTargetData, String kekUriName) throws RuntimeException { return envelopeEncrypt((Object) encryptTargetData, kekUriName); } /* * 暗号化(エンベロープ暗号化) * * 暗号化対象データをバイト配列にシリライズし、Tinkでエンベロープ暗号化する。 * * @param encryptTargetData 暗号化対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 暗号化済みデータ * @throws RuntimeException */ public byte[] envelopeEncrypt(java.sql.Date encryptTargetData, String kekUriName) throws RuntimeException { return envelopeEncrypt((Object) encryptTargetData, kekUriName); } /* * 暗号化(エンベロープ暗号化) * * 暗号化対象データをバイト配列にシリライズし、Tinkでエンベロープ暗号化する。 * * @param encryptTargetData 暗号化対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 暗号化済みデータ * @throws RuntimeException */ public byte[] envelopeEncrypt(Object encryptTargetData, String kekUriName) throws RuntimeException { try { // Tink初期化 init(kekUriName); if (!tinkConfig.isKmsUseDisabled()) { // Tinkによる暗号化 byte[] afterEncryptBytes = aead.encrypt(serializeObject(encryptTargetData), null); return afterEncryptBytes; } else { // バイト配列化 byte[] seraializedBytes = serializeObject(encryptTargetData); return seraializedBytes; } } catch (Exception e) { // 暗号化処理でエラー発生 throw new RuntimeException( Sclb1050.SCLM4002 + " " + Sclb1050.SCLM40021 + ":encryption failure.", e); } } /* * 復号(エンベロープ暗号化) * * Tinkでエンベロープ暗号化された復号対象データ(バイト配列)を復号し、Objectインスタンスにデシリライズする。 * * @param decryptTargetData 復号対象データ * @param clazz 復号済みデータの想定型 * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号済みデータ * @throws RuntimeException */ public <T> Object envelopeDecrypt(byte[] decryptTargetData, Class<T> clazz, String kekUriName) throws RuntimeException { // 復号 Object decTargetData = envelopeDecrypt(decryptTargetData, kekUriName); if (decTargetData.getClass() != clazz) { // 復号後オブジェクトの型が使用メソッドの想定型と不一致 throw new RuntimeException(Sclb1050.SCLM4003 + " " + Sclb1050.SCLM40034 + ":Object type does not match the expected object type of the using method.Expected: " + clazz.getName() + "/Decrypted: " + decTargetData.getClass().getName() + "."); } return decTargetData; } /* * 復号(エンベロープ暗号化) * * Tinkでエンベロープ暗号化された復号対象データ(バイト配列)を復号し、Objectインスタンスにデシリライズする。 * * @param decryptTargetData 復号対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号済みデータ * @throws RuntimeException */ public Object envelopeDecrypt(byte[] decryptTargetData, String kekUriName) throws RuntimeException { Object decTargetData = null; try { // Tink初期化 init(kekUriName); if (!tinkConfig.isKmsUseDisabled()) { // Tinkによる復号 decTargetData = deserializeObject(aead.decrypt(decryptTargetData, null)); } else { // バイト配列からのデシリライズ decTargetData = deserializeObject(decryptTargetData); } } catch (Exception e) { // 復号処理でエラー発生 throw new RuntimeException( Sclb1050.SCLM4003 + " " + Sclb1050.SCLM40031 + ":decryption failure.", e); } return decTargetData; } /* * Tinkにてエンベロープ暗号化されたデータを文字列(String)として復号する * * @param decryptTargetData 復号対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号データ */ public String envelopeDecryptString(byte[] decryptTargetData, String kekUriName) { Object obj = envelopeDecrypt(decryptTargetData, String.class, kekUriName); return (String) obj; } /* * Tinkにてエンベロープ暗号化されたデータを文字(char)として復号する * * @param decryptTargetData 復号対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号データ */ public char envelopeDecryptChar(byte[] decryptTargetData, String kekUriName) { Object obj = envelopeDecrypt(decryptTargetData, char.class, kekUriName); return (char) obj; } /* * Tinkにてエンベロープ暗号化されたデータを数値(Integer)として復号する * * @param decryptTargetData 復号対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号データ */ public Integer envelopeDecryptInteger(byte[] decryptTargetData, String kekUriName) { Object obj = envelopeDecrypt(decryptTargetData, Integer.class, kekUriName); return (Integer) obj; } /* * Tinkにてエンベロープ暗号化されたデータを数値(int)として復号する * * @param decryptTargetData 復号対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号データ */ public int envelopeDecryptInt(byte[] decryptTargetData, String kekUriName) { Object obj = envelopeDecrypt(decryptTargetData, int.class, kekUriName); return ((Integer) obj).intValue(); } /* * Tinkにてエンベロープ暗号化されたデータを日時(java.sql.Timestamp)として復号する * * @param decryptTargetData 復号対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号データ */ public java.sql.Timestamp envelopeDecryptTimestamp(byte[] decryptTargetData, String kekUriName) { Object obj = envelopeDecrypt(decryptTargetData, java.sql.Timestamp.class, kekUriName); return (java.sql.Timestamp) obj; } /* * Tinkにてエンベロープ暗号化されたデータを日付(java.sql.Date)として復号する * * @param decryptTargetData 復号対象データ * @param kekUriName application.ymlで指定するKEKの名前 * @return 復号データ */ public java.sql.Date envelopeDecryptDate(byte[] decryptTargetData, String kekUriName) { Object obj = envelopeDecrypt(decryptTargetData, java.sql.Date.class, kekUriName); return (java.sql.Date) obj; } /* * 暗号化対象データのシリライズ * * @param obj シリライズ対象データ(Serializable) * @return シリライズ済みデータ * @throws RuntimeException */ private byte[] serializeObject(Object obj) { if (obj instanceof Serializable) { return SerializationUtils.serialize(obj); } else { throw new RuntimeException( Sclb1050.SCLM4002 + " " + Sclb1050.SCLM40022 + ":object must Serializable."); } } /* * 復号済みデータのデシリライズ * * @param data デシリライズ対象データ * @throws IOException * @throws ClassNotFoundException */ private Object deserializeObject(byte[] data) throws IOException, ClassNotFoundException { try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) { return ois.readObject(); } } }
10-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值