using GenjiDotNet;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace NetFrameworkSample
{
    public partial class RSAForm : Form
    {
        GenjiDotNet.API api;

        public RSAForm(GenjiDotNet.API api_)
        {
            InitializeComponent();
            api = api_;
        }

        private void createkey_Click(object sender, EventArgs e)
        {
            try
            {
                api.Init();
                api.Count();

                if (MessageBox.Show("キーペアを生成しますか？", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    api.CreateRSAKeyPair(1234, 1, 1);
                    MessageBox.Show("生成しました");

                }
            }
            catch (APIException ex)
            {
                if( ex.Message=="Count" && ex.errorNo == 0)
                    MessageBox.Show("Genjiを接続してください");
                else
                    MessageBox.Show(ex.Message + ";" + ex.errorNo);
            }

            api.Release();
        }

        private void Encrypt_Click(object sender, EventArgs e)
        {
            try
            {
                if (PlainText.Text.Trim().Length == 0)
                {
                    MessageBox.Show("テキストが空です");
                    return;
                }

                byte[] bytePlainText = System.Text.Encoding.UTF8.GetBytes(PlainText.Text);
                if (bytePlainText.Length > 117)
                {
                    MessageBox.Show("データが長すぎます");
                    return;
                }

                api.Init();
                api.Count();

                byte[] cipher = new byte[256];
                short cipherLen = (short)cipher.Length;

                api.EncryptDataRSA(1234, 1, bytePlainText, (short)bytePlainText.Length, cipher, ref cipherLen, 1);

                byte[] cipher1 = new byte[cipherLen];
                System.Buffer.BlockCopy(cipher, 0, cipher1, 0, cipherLen);

                string hexString = BitConverter.ToString(cipher1).Replace("-", "");
                CipherText.Text = hexString;

                labelLength.Text = "長さ(バイナリ)=" + cipher1.Length;

                PlainText.Text = "";
            }
            catch (APIException ex)
            {
                if (ex.Message == "Count" && ex.errorNo == 0)
                    MessageBox.Show("Genjiを接続してください");
                else
                    MessageBox.Show(ex.Message + ";" + ex.errorNo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            api.Release();
        }

        private void Decrypt_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] bytePlainText2 = new byte[128];
                short plainTextLen2 = 128;

                if (CipherText.Text.Trim().Length == 0)
                {
                    MessageBox.Show("暗号化文字列が空です","復号化");
                    return;
                }

                api.Init();
                api.Count();

                byte[] cipher = Util.StringToByteArray(CipherText.Text.Trim());
                short cipherLen = (short)cipher.Length;

                api.DecryptDataRSA(1234, 1, cipher, cipherLen, bytePlainText2, ref plainTextLen2, 1);

                labelLength.Text = "";
                CipherText.Text = "";
                PlainText.Text = System.Text.Encoding.UTF8.GetString(bytePlainText2);
            }
            catch (APIException ex)
            {
                if (ex.Message == "Count" && ex.errorNo == 0)
                    MessageBox.Show("Genjiを接続してください");
                else
                    MessageBox.Show(ex.Message + ";" + ex.errorNo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            api.Release();
        }
    }
}
