C〓〓〓の3 DS暗号解読アルゴリズムの実例コード
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
/// <summary>
///
/// </summary>
public class EncryptHelper
{
//
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
#region
/// <summary>
///
/// </summary>
/// <param name="Value"> </param>
/// <param name="sKey"> , 32 </param>
/// <param name="sIV"> , 12 </param>
/// <returns> </returns>
public string EncryptString(string Value, string sKey,string sIV)
{
try
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
//
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
//
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);//
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ("Error in Encrypting " + ex.Message);
}
}
/// <summary>
///
/// </summary>
/// <param name="Value"> </param>
/// <param name="sKey"> , 32 </param>
/// <param name="sIV"> , 12 </param>
/// <returns> </returns>
public string DecryptString(string Value, string sKey, string sIV)
{
try
{
ICryptoTransform ct;//
MemoryStream ms;//
CryptoStream cs;//
byte[] byt;
// 3DES byte
mCSP.Key = Convert.FromBase64String(sKey);
// 3DES byte
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);//
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ("Error in Decrypting " + ex.Message);
}
}
#endregion
}
}
コール方法は以下の通りです。