Last active
July 4, 2018 07:19
-
-
Save ishisaka/c6c89eda5245f061372602120d0640bb to your computer and use it in GitHub Desktop.
RFC 2998に基づいたパスワードによる暗号化と複合化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* RFC 2998に基づいたパスワードによる暗号化と複合化 | |
* Tadahiro Ishisaka 2018 | |
*/ | |
using System; | |
using System.Text; | |
using System.IO; | |
using System.Security.Cryptography; | |
namespace rfc2998test | |
{ | |
class rfc2998test | |
{ | |
private const string usageText = "Usage: RFC2998 <password> <salt>\nYou must specify the password of encryption. \n"; | |
static void Main(string[] passwordargs) | |
{ | |
if (passwordargs.Length != 2) | |
{ | |
Console.WriteLine(usageText); | |
} | |
else | |
{ | |
string pwd1 = passwordargs[0]; | |
// 2番目の引数をバイト配列にしてsaltの値にする。 | |
var salt = new UTF8Encoding(false).GetBytes(passwordargs[1]); | |
// 暗号化対象データ | |
string data1 = "Some Test data"; | |
// RFC 2998に適合した方法で暗号化キーを作成する | |
// 再計算のカウント(デフォルトは1000) | |
int myIterations = 1000; | |
// k1 暗号化キー | |
var k1 = new Rfc2898DeriveBytes(pwd1, salt, myIterations); // myIterationsの値を変えてみて復号時にエラーが起きることを確認する | |
// k2 複合化キー | |
var k2 = new Rfc2898DeriveBytes(pwd1, salt); | |
// データの暗号化 暗号化にはTriple DESを使う | |
var encAlg = TripleDES.Create(); | |
encAlg.Key = k1.GetBytes(encAlg.KeySize / 8); // Key Suzeはビット | |
encAlg.IV = k1.GetBytes(encAlg.BlockSize / 8); //Block Sizeはビット | |
var encryptionStream = new MemoryStream(); | |
var encrypt = new CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write); | |
byte[] ufd1 = new UTF8Encoding(false).GetBytes(data1); | |
encrypt.Write(ufd1, 0, ufd1.Length); | |
encrypt.FlushFinalBlock(); | |
encrypt.Close(); | |
byte[] edata1 = encryptionStream.ToArray(); // edata1 暗号化されたデータ | |
k1.Reset(); | |
// データの複合化. | |
var decAlg = TripleDES.Create(); | |
decAlg.Key = k2.GetBytes(decAlg.KeySize / 8); | |
decAlg.IV = k2.GetBytes(decAlg.BlockSize / 8); | |
var decryptionStreamBacking = new MemoryStream(); | |
var decrypt = new CryptoStream(decryptionStreamBacking, decAlg.CreateDecryptor(), CryptoStreamMode.Write); | |
decrypt.Write(edata1, 0, edata1.Length); | |
decrypt.Flush(); | |
decrypt.Close(); | |
k2.Reset(); | |
var data2 = new UTF8Encoding(false).GetString(decryptionStreamBacking.ToArray()); | |
if (!data1.Equals(data2)) | |
{ | |
Console.WriteLine("Error: The two values are not equal."); | |
} | |
else | |
{ | |
Console.WriteLine("The two values are equal."); | |
Console.WriteLine("Data1: " + data1); | |
Console.WriteLine("Data2: " + data2); | |
Console.WriteLine("k1 iterations: {0}", k1.IterationCount); | |
Console.WriteLine("k2 iterations: {0}", k2.IterationCount); | |
} | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment