「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。 -戻る --[[.NET開発]] --[[暗号化アルゴリズム]] * 目次 [#ba3e104c] #contents *概要 [#i831ac3c] [[暗号化アルゴリズム]]は、一通り揃っている。 -乱数生成、キー生成 -ハッシュ --暗号ハッシュ([[様々な用途>https://ja.wikipedia.org/wiki/%E3%83%8F%E3%83%83%E3%82%B7%E3%83%A5%E9%96%A2%E6%95%B0#.E7.94.A8.E9.80.94]]に利用可能) --キー付きハッシュ([[署名・検証>https://ja.wikipedia.org/wiki/%E6%9A%97%E5%8F%B7%E5%AD%A6%E7%9A%84%E3%83%8F%E3%83%83%E3%82%B7%E3%83%A5%E9%96%A2%E6%95%B0#.E7.94.A8.E9.80.94]]にも利用可能) -暗号(暗号化・復号化、署名・検証に利用可能) --秘密鍵暗号方式 --公開鍵暗号方式 -署名(署名・検証に利用可能) --公開鍵暗号方式 *.NET Frameworkの暗号化Providerの列挙 [#da2f4388] **乱数生成、キー生成 [#feec3024] ***乱数生成 [#z6a6e8e6] RNGCryptoServiceProvider クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rngcryptoserviceprovider.aspx RNGCryptoServiceProviderを使用して、暗号乱数を生成する。 Membership.GeneratePasswordも下位でRNGCryptoServiceProviderを使用している。 -GeneratePassword Method~ http://aspnet.4guysfromrolla.com/demos/GeneratePassword.aspx --This demo illustrates how to use the code for ASP.NET 2.0's Membership.GeneratePassword() method in ASP.NET 1.x. -- I 'borrowed' this code from the 2.0 .NET Framework using [[Reflector>http://www.aisto.com/roeder/dotnet/]]. ***キー生成 [#ta70b091] Rfc2898DeriveBytes クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rfc2898derivebytes.aspx Rfc2898DeriveBytesを使用して、パスワード ベースのキーを生成する。 主に、 -キー付きハッシュ(KeyedHashAlgorithm) -秘密鍵暗号方式(SymmetricAlgorithm) のキーを生成する(パスワードをそのまま使用しない)。 **ハッシュ [#de661712] ***暗号ハッシュ(HashAlgorithm) [#l1894cde] HashAlgorithm クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hashalgorithm.aspx -MD5 クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.md5.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.md5cng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.md5cryptoserviceprovider.aspx -RIPEMD160 クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.ripemd160.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.ripemd160managed.aspx -SHA1 クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha1.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha1cng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha1cryptoserviceprovider.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha1managed.aspx -SHA256 クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha256.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha256cng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha256cryptoserviceprovider.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha256managed.aspx -SHA384 クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha384.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha384cng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha384cryptoserviceprovider.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha384managed.aspx -SHA512 クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha512.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha512cng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha512cryptoserviceprovider.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.sha512managed.aspx ***キー付きハッシュ(KeyedHashAlgorithm) [#x4fe1902] suffixが無いが、調べると、 -基本、CSP (System.Security.Cryptography.Algorithms.dll, mscorlib.dll, netstandard.dll)実装、 -HMACRIPEMD160 と MACTripleDES は、mscorlib.dllとなっているためManaged実装と思われる。 KeyedHashAlgorithm クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.keyedhashalgorithm.aspx -HMAC クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hmac.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hmacmd5.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hmacripemd160.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hmacsha1.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hmacsha256.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hmacsha384.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.hmacsha512.aspx -MACTripleDES クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.mactripledes.aspx **秘密鍵暗号方式(SymmetricAlgorithm) [#r45cd534] ***SymmetricAlgorithm [#n8b6a137] SymmetricAlgorithm クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.symmetricalgorithm.aspx -Aes クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.aes.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.aescng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.aescryptoserviceprovider.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.aesmanaged.aspx -DES クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.des.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.descryptoserviceprovider.aspx -RC2 クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rc2.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rc2cryptoserviceprovider.aspx -Rijndael クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rijndael.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rijndaelmanaged.aspx -TripleDES クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.tripledes.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.tripledescng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.tripledescryptoserviceprovider.aspx **公開鍵暗号方式(AsymmetricAlgorithm) [#q04bc939] ***AsymmetricAlgorithm [#id81129e] AsymmetricAlgorithm クラス (System.Security.Cryptography)~ https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.asymmetricalgorithm -RSA クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rsa --https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacng --https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider --https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsaopenssl -ECDiffieHellman クラス (System.Security.Cryptography)~ https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.ecdiffiehellman --https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.ecdiffiehellmancng --https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.ecdiffiehellmanopenssl ※ AsymmetricAlgorithmの内、暗号化・復号化が可能な公開鍵暗号方式は、~ RSAとECDiffieHellmanのみ(他のアルゴリズムは、署名のみサポート)。 ***AsymmetricKeyExchangeFormatter, Deformatter [#u0fcff3a] 非対称キー交換フォーマッタ、デフォーマッタ -AsymmetricKeyExchangeFormatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.asymmetrickeyexchangeformatter --RSAOAEPKeyExchangeFormatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.rsaoaepkeyexchangeformatter --RSAPKCS1KeyExchangeFormatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.rsapkcs1keyexchangeformatter -AsymmetricKeyExchangeDeformatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.asymmetrickeyexchangedeformatter --RSAOAEPKeyExchangeDeformatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.rsaoaepkeyexchangedeformatter --RSAPKCS1KeyExchangeDeformatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.rsapkcs1keyexchangedeformatter **署名・検証(AsymmetricAlgorithm) [#lf5eb161] ***AsymmetricAlgorithm [#lbe3d6d2] AsymmetricAlgorithm クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.asymmetricalgorithm.aspx -RSA クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rsa.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rsacng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.rsacryptoserviceprovider.aspx -DSA クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.dsa.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.dsacng.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.dsacryptoserviceprovider.aspx -ECDsa クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.ecdsa.aspx --https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.ecdsacng.aspx ※ なお、RSAだけは、署名・検証だけでなく、暗号化・復号化にも利用可能。 ***AsymmetricSignatureFormatter, Deformatter [#ta0f34c9] 非対称署名フォーマッタ、デフォーマッタ -AsymmetricSignatureFormatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.asymmetricsignatureformatter --RSAPKCS1SignatureFormatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.rsapkcs1signatureformatter --DSASignatureFormatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.dsasignatureformatter -AsymmetricSignatureDeformatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.asymmetricsignaturedeformatter --RSAPKCS1SignatureDeformatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.rsapkcs1signaturedeformatter --DSASignatureDeformatter クラス (System.Security.Cryptography)~ https://docs.microsoft.com/ja-jp/dotnet/api/system.security.cryptography.dsasignaturedeformatter **追加のプロバイダ [#w62a67fc] ***Security.Cryptography [#dc18d317] -CLR Security - CodePlex Archive~ https://archive.codeplex.com/?p=clrsecurity -NuGet Gallery | Security.Cryptography~ https://www.nuget.org/packages/Security.Cryptography/ -clrsecurity/Security.Cryptography/src at master · MicrosoftArchive/clrsecurity~ https://github.com/MicrosoftArchive/clrsecurity/tree/master/Security.Cryptography/src --AES with CCM or GCM~ 以下のクラスは、コメントを読むとAES-CCM or AES-GCMを実装している。 ---AuthenticatedSymmetricAlgorithm >↑(継承) ---AuthenticatedAes, AuthenticatedAesCng *[[.NET Standard]]の暗号化Providerの列挙 [#db4c9ea1] **.NET Frameworkからの移植 [#i773c894] -予想外に、[[CSPやCNGのプロバイダ>#fde2f12c]]も移植されている -...が、一部含まれないものがある(特にCNG系実装に多い)。 -これら[[CNGのプロバイダの移植>#fde2f12c]]が、今後どう進むか?は不明。 **[[追加のプロバイダ>#u5d4c9e0]] [#b6180c03] *Managed、CAPI(CSP)、CNG実装とは? [#fde2f12c] -Managedで終わる名称のProviderは、.NET実装と思われる(mscorlib.dll)。 -CryptoServiceProvider (CSP) で終わる名称のProviderは、「CryptoAPI Cryptographic Service Providers (CSP ≒ CAPI) を使用する」とある(System.Core.dll)。 -Cryptography Next Generation (CNG) で終わる名称のProviderは、CNG Cryptographic Algorithm Providersの実装を使用している(System.Core.dll)。 **参考 [#le45730f] ***CAPI (CSP) [#n13961a2] -CryptoAPI Cryptographic Service Providers (Windows)~ https://msdn.microsoft.com/ja-jp/library/windows/desktop/bb931357.aspx -Cryptographic API - Wikipedia~ https://ja.wikipedia.org/wiki/Cryptographic_API ***CNG [#if785c4b] -CNGとは (Cryptography Next Generation) シーエヌジー: - IT用語辞典バイナリ~ http://www.sophia-it.com/content/Cryptography+Next+Generationax -Cryptography Next Generation~ https://technet.microsoft.com/en-us/library/532ac164-da33-4369-bef0-8f019d5a18b8 -CngAlgorithm クラス (System.Security.Cryptography)~ https://msdn.microsoft.com/ja-jp/library/system.security.cryptography.cngalgorithm.aspx --MD5 --Sha1 --Sha256 --Sha384 --Sha512 --Rsa --ECDiffieHellman ---ECDiffieHellmanP256 ---ECDiffieHellmanP384 ---ECDiffieHellmanP521 --ECDsa ---ECDsaP256 ---ECDsaP384 ---ECDsaP521 **どれを使用すべき? [#ub3ba427] -.NET Framework の暗号モデル~ https://msdn.microsoft.com/ja-jp/library/0ss79b2x.aspx には、以下のようにある。 ***最適な実装を選択可能 [#f3be9d35] -マネージ実装~ .NET Framework をサポートするすべてのプラットフォームで利用可能。~ ただし、マネージ実装はFIPSに認定されておらず、ラッパー クラスよりも低速である場合がある。 -CAPI 実装~ CSPラッパー。以前のOSで使用可能だが、開発中止となっている。 -CNG 実装~ CNGラッパー。まさに最新の実装であり、新しい開発が行われる。 ***アルゴリズムの選択 [#uf945e7e] -データのプライバシー~ Aes -データの整合性 --HMAC SHA-256 --HMAC SHA-512 -デジタル署名 --ECDsa --RSA(RS256: RSA SHA-256) -キー交換 --ECDiffieHellman --RSA(RS256: RSA SHA-256) -乱数生成~ RNGCryptoServiceProvider -パスワードからのキー生成~ Rfc2898DeriveBytes ***CngKey [#v4011842] -How to use RSA in .NET: RSACryptoServiceProvider vs. RSACng and good practise patterns - Dusted Codes~ https://dusted.codes/how-to-use-rsa-in-dotnet-rsacryptoserviceprovider-vs-rsacng-and-good-practise-patterns 以下の様な違いがあるもよう。 --RSACryptoServiceProviderは独自のキー操作に結びついている。 --RSACngのキーはCngKeyクラスによって管理され、コンストラクタに注入できる。 [[どちらでも良さそう。>#f3be9d35]] ***性能 [#h1bd7d6f] -Measuring the performance of AES implementations in .Net Framework (AesManaged vs AesCryptoServiceProvider) - CodeProject~ https://www.codeproject.com/Tips/844795/Measuring-the-performance-of-AES-implementations-i *参考 [#mf5f8a70] -Programming .NET Security - O'Reilly Media~ http://shop.oreilly.com/product/9780596004422.do -How to use RSA in .NET:~ RSACryptoServiceProvider vs. RSACng and good practise patterns - Dusted Codes~ https://dusted.codes/how-to-use-rsa-in-dotnet-rsacryptoserviceprovider-vs-rsacng-and-good-practise-patterns **DOBON.NET: VB.NET, C#, 無料ソフトウェア... [#j2316c2c] http://dobon.net/index.html -文字列、暗号化: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/index.html ***暗号ハッシュ [#bbfd012a] -MD5やSHA1などでハッシュ値を計算する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/md5.html -ファイルのMD5やSHA1などでハッシュ値を計算する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/filemd5.html ***秘密鍵暗号方式 [#geb60fa1] -パスワードで文字列を暗号化する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/encryptstring.html -パスワードでファイルを暗号化する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/encryptfile.html ***公開鍵暗号方式 [#icf617f4] -公開鍵暗号方法で暗号化する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/rsaencryption.html ***署名・検証 [#f3bcf75f] -デジタル署名を作成、検証する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/digitalsignature.html ***乱数生成、キー生成 [#gb1f54a6] -乱数を生成する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/programing/random.html -パスワードのようなランダムな文字列を生成する: .NET Tips: C#, VB.NET~ http://dobon.net/vb/dotnet/string/generatepassword.html **ライブラリ [#u5d4c9e0] ***[[OpenSSL>OpenSSLでの証明書の操作]] [#lddc9143] ***[[BouncyCastle]] [#tc1ee636] ***libsodium [#wc22e484] -C言語で開発された、クロスプラットフォームの暗号化ライブラリを含む。 -PHPでは、PECLで提供されていたがコアに標準的に組み込まれた等の経緯があるもよう。 ***[[Open 棟梁>https://opentouryo.osscons.jp/index.php?%E6%9A%97%E5%8F%B7%E3%83%BB%E5%BE%A9%E5%8F%B7%E5%8C%96%20%E9%83%A8%E5%93%81]] [#he1630c1] ***参考 [#j0ade641] -暗号ライブラリの比較 - Wikipedia~ https://ja.wikipedia.org/wiki/%E6%9A%97%E5%8F%B7%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E6%AF%94%E8%BC%83 ---- Tags: [[:.NET開発]], [[:セキュリティ]], [[:暗号化]], [[:証明書]]