ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

C#コミュの暗号化(GUIにしてもうたスマソ。。) 無駄な箇所を教えていただけると嬉しい

  • mixiチェック
  • このエントリーをはてなブックマークに追加
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private byte[] DesKey;
private byte[] DesIV;


private void button1_Click(object sender, EventArgs e)
{
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
DesKey = TDES.Key;
DesIV = TDES.IV;

// string to byte
byte[] source = Encoding.Unicode.GetBytes(textBox1.Text);   

// select Triple DES service provider
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();  

// make to write stream
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(DesKey, DesIV),
CryptoStreamMode.Write);

//write data Encrypte
cs.Write(source, 0, source.Length);
cs.Close();

//get to byte from string
byte[] destination = ms.ToArray();
ms.Close();

//show this
textBox2.Text = Encoding.Unicode.GetString(destination);

textBox4.Text = Encoding.Unicode.GetString(DesKey);

textBox5.Text = Encoding.Unicode.GetString(DesIV);

}

private void button2_Click(object sender, EventArgs e)
{

byte[] source = Encoding.Unicode.GetBytes(textBox2.Text);


TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

DesKey = Encoding.Unicode.GetBytes(textBox6.Text);
DesIV = Encoding.Unicode.GetBytes(textBox7.Text);


MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(DesKey, DesIV),
CryptoStreamMode.Write);


cs.Write(source, 0, source.Length);
cs.Close();


byte[] destination = ms.ToArray();
ms.Close();


textBox3.Text = Encoding.Unicode.GetString(destination);
}
}
}

コメント(16)

メッセありがとうです。 まだC#初めて半年ほどで
デコードの部分でエラーを回避するためにkeyを挿入するboxを別に作ったんですが

もう少し別の方法があったのではと思いまして^^
>>3 ありがとうございます。

複合keyの出力先と読み取り先を一緒にすると再暗号化時にエラーが起こってしまうのですが
これはメモリor配列の関係でのエラーなのでしょうか?

それともただたんに出力先と読み取り先を一緒にすることによって起こるエラーなのですか?
説明不足でした。
あらかた参考書などで解決したのですが

cs.close();の処理がまだ理解できていないのですが
.close()ってのは具体的にどういう処理なんですか?
てことはcs.close();で起こるエラーはファイルを閉じれないもしくはアクセスが継続され続けてるって
理解でいいですか?
以下で十分。
public partial class Form1 : Form
{
private TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

// string to byte
byte[] source = Encoding.Unicode.GetBytes(textBox1.Text);

// make to write stream
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, TDES.CreateEncryptor(), CryptoStreamMode.Write);

//write data Encrypte
cs.Write(source, 0, source.Length);
cs.Close();

//get to byte from string
byte[] destination = ms.ToArray();
ms.Close();

//show this
textBox2.Text = Encoding.Unicode.GetString(destination);
}

private void button2_Click(object sender, EventArgs e)
{

byte[] source = Encoding.Unicode.GetBytes(textBox2.Text);

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, TDES.CreateDecryptor(), CryptoStreamMode.Write);

cs.Write(source, 0, source.Length);
cs.Close();

byte[] destination = ms.ToArray();
ms.Close();

textBox3.Text = Encoding.Unicode.GetString(destination);
}
}
↑ありがとうございます。
参考にして作り直したいと思います。

ログインすると、残り10件のコメントが見れるよ

mixiユーザー
ログインしてコメントしよう!

C# 更新情報

C#のメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング