Implementasi Algoritma Kunci Publikntruencrypt Pada Add-In Microsoft Outlook

LAMPIRAN A : LISTING PROGRAM

File : EncryptionParameters.cs
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace NtruEncryptAddIn
{
public class EncryptionParameters
{
public static readonly EncryptionParameters
EncryptionParameters(401, 2048, 113);
public static readonly EncryptionParameters
EncryptionParameters(541, 2048, 49);

public static readonly EncryptionParameters
EncryptionParameters(659, 2048, 38);
public static readonly EncryptionParameters
EncryptionParameters(449, 2048, 134);
public static readonly EncryptionParameters
EncryptionParameters(613, 2048, 55);
public static readonly EncryptionParameters
EncryptionParameters(761, 2048, 42);
public static readonly EncryptionParameters
EncryptionParameters(677, 2048, 157);
public static readonly EncryptionParameters
EncryptionParameters(887, 2048, 81);
public static readonly EncryptionParameters
EncryptionParameters(1087, 2048, 63);
public static readonly EncryptionParameters
EncryptionParameters(1087, 2048, 120);
public static readonly EncryptionParameters
EncryptionParameters(1171, 2048, 106);
public static readonly EncryptionParameters
EncryptionParameters(1499, 2048, 79);


EES401EP1 = new
EES541EP1 = new
EES659EP1 = new
EES449EP1 = new
EES613EP1 = new
EES761EP1 = new
EES677EP1 = new
EES887EP1 = new
EES1087EP1 = new
EES1087EP2 = new
EES1171EP1 = new
EES1499EP1 = new

public int N, q, df, dr, dg, maxMsgLenBytes;
public EncryptionParameters(int N, int q, int df)
{
this.N = N;
this.q = q;
this.df = df;

this.maxMsgLenBytes = N * 3 / 2 / 8 - 1 - 32;
dr = df;
dg = N / 3;
}
public static EncryptionParameters findParams(string str,
EncryptionParameters param)
{
switch (str)
{
case "1":
param = EncryptionParameters.EES401EP1;

Universitas Sumatera Utara

A-2

break;
case "2":
param =
break;

case "3":
param =
break;
case "4":
param =
break;
case "5":
param =
break;
case "6":
param =
break;
case "7":
param =
break;
case "8":
param =
break;
case "9":
param =

break;
case "10":
param =
break;
case "11":
param =
break;
case "12":
param =
break;

EncryptionParameters.EES541EP1;
EncryptionParameters.EES659EP1;
EncryptionParameters.EES449EP1;
EncryptionParameters.EES613EP1;
EncryptionParameters.EES761EP1;
EncryptionParameters.EES677EP1;
EncryptionParameters.EES887EP1;
EncryptionParameters.EES1087EP1;
EncryptionParameters.EES1087EP2;

EncryptionParameters.EES1171EP1;
EncryptionParameters.EES1499EP1;

}
return param;
}
}
}

File : IntegerPolynomial.cs
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

System.Numerics;

namespace NtruEncryptAddIn
{
public class IntegerPolynomial
{
public int[] coeffs;
public IntegerPolynomial(int N)
{
coeffs = new int[N];
}

Universitas Sumatera Utara

A-3

public IntegerPolynomial(int[] coeffs)
{
this.coeffs = coeffs;
}

public static int[] encodePolynomial(byte[] b, int N)
{
byte[] c = new byte[b.Length];
int j = 0;
for (int i = b.Length - 1; i >= 0; i--)
{
c[j] = b[i];
j++;
}
BigInteger sum = new BigInteger(c);
int[] coeffs = new int[N];
for (int i = 0; i < N; i++)
{
coeffs[i] = (int)(sum % 3) - 1;
if (coeffs[i] > 1)
coeffs[i] -= 3;
sum = sum / 3;
}
return coeffs;
}

public static byte[] decodePolynomial(int[] intArray)
{
BigInteger sum = BigInteger.Zero;
for (int i = intArray.Length - 1; i >= 0; i--)
{
sum *= 3;
sum += (intArray[i] + 1);
}
int size = (bitLength(BigInteger.Pow(3, intArray.Length))
+ 7) / 8;
byte[] arr = sum.ToByteArray();
arr = balik(arr);
if (arr.Length < size)
{
byte[] arr2 = new byte[size];
Array.Copy(arr, 0, arr2, size - arr.Length,
arr.Length);
}
if (arr.Length > size)
{

byte[] temp = arr;
Array.Copy(temp, 1, arr, 0, temp.Length);
Array.Resize(ref arr, arr.Length - 1);
}
int xi2 = 0;
while (arr[xi2] == 0)
xi2++;
byte[] arr77 = new byte[arr.Length - xi2];
Array.Copy(arr, xi2, arr77, 0, arr.Length - xi2);
return arr77;
}

Universitas Sumatera Utara

A-4

public static byte[] balik(byte[] inpute)
{
byte[] input = inpute;
byte[] output = new byte[input.Length];

int j = input.Length;
for (int i = 0; i < output.Length; i++)
output[i] = input[--j];
return output;
}
public static IntegerPolynomial generateRandomSmall(int N,
int numOnes, int NumNegOnes)
{
List coeffs = new List();
for (int i = 0; i < numOnes; i++)
coeffs.Add(1);
for (int i = 0; i < NumNegOnes; i++)
coeffs.Add(-1);
IntegerPolynomial poly = new IntegerPolynomial(N);
poly.coeffs = coeffs.ToArray();
return poly;
}
public IntegerPolynomial invertFq(int q)
{
int N = coeffs.Length;
int k = 0;
IntegerPolynomial b = new IntegerPolynomial(N + 1);
b.coeffs[0] = 1;
IntegerPolynomial c = new IntegerPolynomial(N + 1);
IntegerPolynomial f = new IntegerPolynomial(N + 1);
Array.Copy(coeffs, f.coeffs, coeffs.Length);// coeffs;
Array.Resize(ref f.coeffs, N + 1);
// set g(x) = x^N − 1
IntegerPolynomial g = new IntegerPolynomial(N + 1);
g.coeffs[0] = -1;
g.coeffs[N] = 1;
while (true)
{
while (f.coeffs[0] == 0)
{
for (int i = 1; i = 0; i--)
{
j = i - k;
if (j < 0)
j += N;
Fq.coeffs[j] = b.coeffs[i];
}
// invers mod 2 ---> invers mod q
int v = 2;
while (v < q)
{
v *= 2;
int[] temparr = new int[Fq.coeffs.Length];
Fq.coeffs.CopyTo(temparr, 0);
IntegerPolynomial temp = new
IntegerPolynomial(temparr);
temp.mult2(v);
Fq = mult(Fq, v).mult(Fq, v);
temp.sub(Fq, v);
Fq = temp;
}
Fq.ensurePositive(q);
return Fq;
}
public IntegerPolynomial invertF3()
{
int N = coeffs.Length;
int k = 0;
IntegerPolynomial b = new IntegerPolynomial(N
b.coeffs[0] = 1;
IntegerPolynomial c = new IntegerPolynomial(N
IntegerPolynomial f = new IntegerPolynomial(N
Array.Copy(coeffs, f.coeffs, coeffs.Length);
Array.Resize(ref f.coeffs, N + 1);
// set g(x) = x^N − 1
IntegerPolynomial g = new IntegerPolynomial(N
g.coeffs[0] = -1;
g.coeffs[N] = 1;

+ 1);
+ 1);
+ 1);

+ 1);

while (true)
{

Universitas Sumatera Utara

A-6

while (f.coeffs[0] == 0)
{
for (int i = 1; i = 0; i--)
{
j = i - k;
if (j < 0)
j += N;
Fp.coeffs[j] = f.coeffs[0] * b.coeffs[i];
}
Fp.ensurePositive(3);
return Fp;
}
public void center0(int q)
{

Universitas Sumatera Utara

A-7

for (int i = 0; i < coeffs.Length; i++)
{
while (coeffs[i] < -q / 2)
coeffs[i] += q;
while (coeffs[i] > q / 2)
coeffs[i] -= q;
}
}
private Boolean equalsZero()
{
for (int i = 0; i < coeffs.Length; i++)
if (coeffs[i] != 0)
return false;
return true;
}
Boolean equalsOne()
{
for (int i = 1; i < coeffs.Length; i++)
if (coeffs[i] != 0)
return false;
return coeffs[0] == 1;
}
Boolean equalsAbsOne()
{
for (int i = 1; i < coeffs.Length; i++)
if (coeffs[i] != 0)
return false;
return Math.Abs(coeffs[0]) == 1;
}
public int degree()
{
int degree = coeffs.Length - 1;
while (degree > 0 && coeffs[degree] == 0)
degree--;
return degree;
}
public void add(IntegerPolynomial b, int modulus)

//

perfect
{
add(b);
mod(modulus);
}
public void add(IntegerPolynomial b)

//

perfect
{
if (b.coeffs.Length > coeffs.Length)
Array.Resize(ref coeffs, b.coeffs.Length);
for (int i = 0; i < b.coeffs.Length; i++)
coeffs[i] += b.coeffs[i];
}
public void sub(IntegerPolynomial b, int modulus)
// perfect
{

Universitas Sumatera Utara

A-8

sub(b);
mod(modulus);
}
public void sub(IntegerPolynomial b)
//perfect
{
if (b.coeffs.Length > coeffs.Length)
Array.Resize(ref coeffs, b.coeffs.Length);
for (int i = 0; i < b.coeffs.Length; i++)
coeffs[i] -= b.coeffs[i];
}
public IntegerPolynomial mult(IntegerPolynomial poly2, int
modulus)
{
int[] a = coeffs;
int[] b = poly2.coeffs;
if (b.Length != a.Length)
throw new System.ArgumentException("Number of
coefficients must be the same");
int N = a.Length;
int[] c = new int[N];
for (int k = N - 1; k >= 0; k--)
{
c[k] = 0;
int j = k + 1;
for (int i = N - 1; i >= 0; i--)
{
if (j == N)
j = 0;
if (a[i] != 0 && b[j] != 0)
{
c[k] += a[i] * b[j];
c[k] %= modulus;
}
j++;
}
}
return new IntegerPolynomial(c);
}
public void mult2(int modulus)
{
for (int i = 0; i < coeffs.Length; i++)
{
coeffs[i] *= 2;
coeffs[i] %= modulus;
}
}
public void mult3(int modulus)
{
for (int i = 0; i < coeffs.Length; i++)
{
coeffs[i] *= 3;
coeffs[i] %= modulus;
}
}

Universitas Sumatera Utara

A-9

public void mod(int modulus)
{
for (int i = 0; i < coeffs.Length; i++)
coeffs[i] %= modulus;
}
public void mod3()
{
for (int i = 0; i < coeffs.Length; i++)
{
coeffs[i] %= 3;
if (coeffs[i] > 1)
coeffs[i] -= 3;
if (coeffs[i] < -1)
coeffs[i] += 3;
}
}
public void ensurePositive(int modulus)
{
for (int i = 0; i < coeffs.Length; i++)
while (coeffs[i] < 0)
coeffs[i] += modulus;
}
public void clear()

//

perfect
{
for (int i = 0; i < coeffs.Length; i++)
coeffs[i] = 0;
}
static int bitLength(BigInteger n)
{
BigInteger a;
if (n < 0)
a = -n;
else
a = n;
a = n;
BigInteger sisa;
int[] lengthcount = new int[2];
int bitcount = 0;
int bitlength = 0;
while (a > 0)
{
sisa = a % 2;
a /= 2;
if (sisa == 1)
bitcount++;
bitlength++;
}
return bitlength;
}
}
}

File : Base64Reon.cs

Universitas Sumatera Utara

A-10

using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Collections;

namespace NtruEncryptAddIn
{
class Base64Reon
{
static private char s_CharPlusSign = '+';
static private char s_CharSlash = '/';
public int waktuEncode;
public int waktuDecode;
public char[] intToBase64(int[] input, int bitsLength)
{
DateTime start;
DateTime finish;
TimeSpan total;
int waktuEncode;
start = DateTime.Now;
List inputBool = int2BoolBits(input, bitsLength);
List inputBool2 = BoolBits2base64(inputBool);
inputBool2.Reverse();
BitArray BA1 = new BitArray(inputBool2.ToArray());
byte[] byte2 = new byte[BA1.Length / 8];
BA1.CopyTo(byte2, 0);
char[] akhir = SixBitToChar(byte2);
finish = DateTime.Now;
total = finish - start;
waktuEncode = Convert.ToInt32(total.TotalMilliseconds);
this.waktuEncode = waktuEncode;
return akhir;
}
public int[] Base64ToInt(char[] inputChar, int bitsLength)
{
DateTime start;
DateTime finish;
TimeSpan total;
int waktuDecode;
start = DateTime.Now;
byte[] byte1 = CharToSixBit(inputChar);
List listbalik = Byte2Bools(byte1);
int[] intAkhir = BoolBits2Int(listbalik, bitsLength);
finish = DateTime.Now;
total = finish - start;
waktuDecode = Convert.ToInt32(total.TotalMilliseconds);
this.waktuDecode = waktuDecode;
return intAkhir;
}
private static List int2BoolBits(int[] input, int
bitslength)
{
List bools = new List();
string s;
char[] bits;
bool[] bool1 = new bool[bitslength];

Universitas Sumatera Utara

A-11

for (int i = 0; i < input.Length; i++)
{
s = Convert.ToString(input[i], 2);
bits = s.PadLeft(bitslength, '0').ToCharArray();
for (int j = 0; j < bool1.Length; j++)
{
switch (bits[j])
{
case '0': bool1[j] = false;
break;
case '1': bool1[j] = true;
break;
}
}
bools.AddRange(bool1);
}
return bools;
}
private static List BoolBits2base64(List list)
{
List list1 = new List();
int length = list.Count - 1;
bool[] addbool = { false, false };
for (int i = 0; i < list.Count; )
{
list1.AddRange(addbool);
list1.AddRange(list.GetRange(i, 6));
i += 6;
}
return list1;
}
static private char[] SixBitToChar(byte[] b)
{
char[] c = new char[b.Length];
for (int i = 0; i < b.Length; i++)
{
if (b[i] < 26)
{
c[i] = (char)((int)b[i] + (int)'A');
}
else if (b[i] < 52)
{
c[i] = (char)((int)b[i] - 26 + (int)'a');
}
else if (b[i] < 62)
{
c[i] = (char)((int)b[i] - 52 + (int)'0');
}
else if (b[i] == 62)
{
c[i] = s_CharPlusSign;
}
else
{
c[i] = s_CharSlash;
}
}

Universitas Sumatera Utara

A-12

return c;
}
static private byte[] CharToSixBit(char[] c)
{
byte[] b = new byte[c.Length];
for (int i = 0; i < b.Length; i++)
{
if (c[i] >= 'A' && c[i] = 'a' && c[i] = '0' && c[i]
Convert.ToString(n)).ToArray();
if (upload2[1] == "1")
{
this.param =
EncryptionParameters.findParams(upload2[0], param);
int[] pubkey = upload2[2].Split(' ').Select(n =>
Convert.ToInt32(n)).ToArray();
this.pubKey = new IntegerPolynomial(pubkey);
}
else
{
MessageBox.Show("Sorry, it is not a public key.
Please upload another key.");
}
}
catch (System.IndexOutOfRangeException)

Universitas Sumatera Utara

A-17

{
MessageBox.Show("Sorry, it is not a public key.
Please upload another key.");
}
if (pubKey == null)
encryptBtn.Enabled = false;
else
encryptBtn.Enabled = true;
}
private void button4_Click(object sender,
RibbonControlEventArgs e)
{
openFileDialog2.ShowDialog();
}
private void openFileDialog2_FileOk(object sender,
System.ComponentModel.CancelEventArgs e)
{
string upload =
File.ReadAllText(openFileDialog2.FileName);
try
{
string[] upload2 = upload.Split('/').Select(n =>
Convert.ToString(n)).ToArray();
if (upload2[1] == "0")
{
this.param =
EncryptionParameters.findParams(upload2[0], param);
int[] privkey = upload2[2].Split(' ').Select(n =>
Convert.ToInt32(n)).ToArray();
this.privKey = new IntegerPolynomial(privkey);
}
else
{
MessageBox.Show("Sorry, it is not a private key.
Please upload another key.");
}
}
catch (System.IndexOutOfRangeException)
{
MessageBox.Show("Sorry, it is not a private key.
Please upload another key.");
}
if (privKey == null)
decryptBtn.Enabled = false;
else
decryptBtn.Enabled = true;
}
private void encryptBtn_Click(object sender,
RibbonControlEventArgs e)
{
Outlook.MailItem mailItem =
Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as
Outlook.MailItem;
pesan = mailItem.Body;
if (pesan == null || pesan == "")

Universitas Sumatera Utara

A-18

MessageBox.Show("Please write the message first.");
else if (pesan.Length > param.maxMsgLenBytes)
MessageBox.Show("Sorry, Your message is too
long.\nThe maximal length of message you can enrypt is " +
param.maxMsgLenBytes + " chars,\nyour message is " + pesan.Length + "
chars.");
else
{
byte[] pesanByte =
Encoding.GetEncoding(1252).GetBytes(pesan);
int[] pesanInt =
IntegerPolynomial.encodePolynomial(pesanByte, param.N);
IntegerPolynomial m = new
IntegerPolynomial(pesanInt);
IntegerPolynomial r =
IntegerPolynomial.generateRandomSmall(param.N, param.dr, param.dr);
NtruEncrypt newEncrypt = new NtruEncrypt();
IntegerPolynomial E = newEncrypt.encrypt(m, r,
this.pubKey, param);
Base64Reon newBase = new Base64Reon();
char[] ECoeffsChar = newBase.intToBase64(E.coeffs,
12);
mailItem.Body = new string(ECoeffsChar);
}
}
private void decryptBtn_Click(object sender,
RibbonControlEventArgs e)
{
Outlook.MailItem mailItem =
Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as
Outlook.MailItem;
pesan = mailItem.Body;
char[] arx2 = pesan.ToCharArray();
Array.Resize(ref arx2, arx2.Length - 2);
Base64Reon newBase = new Base64Reon();
int[] arr3 = newBase.Base64ToInt(arx2, 12);
IntegerPolynomial E = new IntegerPolynomial(arr3);
NtruEncrypt newEncrypt = new NtruEncrypt();
IntegerPolynomial C = newEncrypt.decrypt(E, this.privKey,
this.param);
byte[] decryptedByte =
IntegerPolynomial.decodePolynomial(C.coeffs);
mailItem.Body =
Encoding.GetEncoding(1252).GetString(decryptedByte);
}
private void button5_Click(object sender,
RibbonControlEventArgs e)
{
FileEncryption encryptFile = new FileEncryption();
encryptFile.Location = new Point(210, 50);
encryptFile.ShowDialog();
}
private void button6_Click(object sender,
RibbonControlEventArgs e)
{
FileDecryption decryptFile = new FileDecryption();
decryptFile.Location = new Point(210, 50);

Universitas Sumatera Utara

A-19

decryptFile.ShowDialog();
}
private void button8_Click(object sender,
RibbonControlEventArgs e)
{
AboutAddIn about = new AboutAddIn();
about.Location = new Point(0, 50);
about.ShowDialog();
}
private void button7_Click(object sender,
RibbonControlEventArgs e)
{
HelpAddIn help = new HelpAddIn();
help.Location = new Point(0, 50);
help.Show();
}
}
}

File : SavedKeysForm.cs
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.IO;

namespace NtruEncryptAddIn
{
public partial class SavedKeysForm : Form
{
private AdoNetEntities entiti1;
public SavedKeysForm()
{
InitializeComponent();
}
private void SavedKeysForm_Load(object sender, EventArgs e)
{
entiti1 = new AdoNetEntities();
Filter();
}
private void Filter()
{
var dataInput = (from b in entiti1.TabelKuncis select new
{b.id, b.Nama_Kunci, b.Parameters, b.Kunci_Private, b.Kunci_Publik});
dataGridView1.DataSource = dataInput;
dataGridView1.Columns[0].Visible = false;
dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;

Universitas Sumatera Utara

A-20

if (dataGridView1.RowCount == 0)
button2.Enabled = false;
else
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Anda yakin mau menghapus data?",
"Hapus Data?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Guid selectedID = new Guid
(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
var deleteData = (from b in entiti1.TabelKuncis where
b.id == selectedID select b).FirstOrDefault();
if (deleteData != null)
{
entiti1.DeleteObject(deleteData);
entiti1.SaveChanges();
Filter();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
CreateNewKeyForm CNKForm = new CreateNewKeyForm();
CNKForm.Location = new Point(217, 74);
CNKForm.ShowDialog();
Filter();
}
private void button3_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(0, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
contextMenuStrip1.Show(ptLowerLeft);
}
private void privateKeyToolStripMenuItem_Click(object sender,
EventArgs e)
{
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender,
CancelEventArgs e)
{
string paramUsed =
dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
string privKGV =
dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
if (paramUsed == "EES401EP1")
paramUsed = "1";
else if (paramUsed == "EES541EP1")
paramUsed = "2";
else if (paramUsed == "EES659EP1")
paramUsed = "3";
else if (paramUsed == "EES449EP1")

Universitas Sumatera Utara

A-21

paramUsed = "4";
else if (paramUsed ==
paramUsed = "5";
else if (paramUsed ==
paramUsed = "6";
else if (paramUsed ==
paramUsed = "7";
else if (paramUsed ==
paramUsed = "8";
else if (paramUsed ==
paramUsed = "9";
else if (paramUsed ==
paramUsed = "10";
else if (paramUsed ==
paramUsed = "11";
else if (paramUsed ==
paramUsed = "12";

"EES613EP1")
"EES761EP1")
"EES677EP1")
"EES887EP1")
"EES1087EP1")
"EES1087EP2")
"EES1171EP1")
"EES1499EP1")

string privK2File = paramUsed + "/0/" + privKGV;
string nama = saveFileDialog1.FileName;
File.WriteAllText(nama, privK2File);
}
private void publicKeyToolStripMenuItem_Click(object sender,
EventArgs e)
{
saveFileDialog2.ShowDialog();
}
private void saveFileDialog2_FileOk(object sender,
CancelEventArgs e)
{
string paramUsed =
dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
string pubKGV =
dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
if (paramUsed == "EES401EP1")
paramUsed = "1";
else if (paramUsed == "EES541EP1")
paramUsed = "2";
else if (paramUsed == "EES659EP1")
paramUsed = "3";
else if (paramUsed == "EES449EP1")
paramUsed = "4";
else if (paramUsed == "EES613EP1")
paramUsed = "5";
else if (paramUsed == "EES761EP1")
paramUsed = "6";
else if (paramUsed == "EES677EP1")
paramUsed = "7";
else if (paramUsed == "EES887EP1")
paramUsed = "8";
else if (paramUsed == "EES1087EP1")
paramUsed = "9";
else if (paramUsed == "EES1087EP2")
paramUsed = "10";
else if (paramUsed == "EES1171EP1")
paramUsed = "11";
else if (paramUsed == "EES1499EP1")

Universitas Sumatera Utara

A-22

paramUsed = "12";
string pubK2File = paramUsed + "/1/" + pubKGV;
string nama = saveFileDialog2.FileName;
File.WriteAllText(nama, pubK2File);
}
private void button3_Click_1(object sender, EventArgs e)
{
this.Close();
}
}
}

File : CreateNewKey.css
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Data.Common;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
Microsoft.VisualBasic;

namespace NtruEncryptAddIn
{
public partial class CreateNewKeyForm : Form
{
EncryptionParameters param = null;
IntegerPolynomial F = null;
IntegerPolynomial H = null;
private string paramString = null;
private AdoNetEntities entiti1;
public CreateNewKeyForm()
{
InitializeComponent();
}
private void CreateNewKeyForm_Load(object sender, EventArgs
e)
{
entiti1 = new AdoNetEntities();
}
private void comboBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
if (comboBox1.Text == "")
{
param = null;
button1.Enabled = false;
}
else if (comboBox1.Text == "EES401EP1")
{
param = EncryptionParameters.EES401EP1;

Universitas Sumatera Utara

A-23

paramString = "EES401EP1";
}
else if (comboBox1.Text == "EES541EP1")
{
param = EncryptionParameters.EES541EP1;
paramString = "EES541EP1";
}
else if (comboBox1.Text == "EES659EP1")
{
param = EncryptionParameters.EES659EP1;
paramString = "EES659EP1";
}
else if (comboBox1.Text == "EES449EP1")
{
param = EncryptionParameters.EES449EP1;
paramString = "EES449EP1";
}
else if (comboBox1.Text == "EES613EP1")
{
param = EncryptionParameters.EES613EP1;
paramString = "EES613EP1";
}
else if (comboBox1.Text == "EES761EP1")
{
param = EncryptionParameters.EES761EP1;
paramString = "EES761EP1";
}
else if (comboBox1.Text == "EES677EP1")
{
param = EncryptionParameters.EES677EP1;
paramString = "EES677EP1";
}
else if (comboBox1.Text == "EES887EP1")
{
param = EncryptionParameters.EES887EP1;
paramString = "EES887EP1";
}
else if (comboBox1.Text == "EES1087EP1")
{
param = EncryptionParameters.EES1087EP1;
paramString = "EES1087EP1";
}
else if (comboBox1.Text == "EES1087EP2")
{
param = EncryptionParameters.EES1087EP2;
paramString = "EES1087EP2";
}
else if (comboBox1.Text == "EES1171EP1")
{
param = EncryptionParameters.EES1171EP1;
paramString = "EES1171EP1";
}
else if (comboBox1.Text == "EES1499EP1")
{
param = EncryptionParameters.EES1499EP1;
paramString = "EES1499EP1";
}
textBox1.Text = param.N.ToString();
textBox3.Text = param.q.ToString();

Universitas Sumatera Utara

A-24

textBox2.Text = param.maxMsgLenBytes.ToString();
button1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
NtruEncrypt newEnrypt = new NtruEncrypt();
F = newEnrypt.generatePriv(param);
richTextBox1.Text = string.Join(" ", F.coeffs);
button2.Enabled = true;
MessageBox.Show("Waktu pembangkitan kunci private adalah:
" + newEnrypt.waktuGeneratePriv + " ms.");
}
private void button2_Click(object sender, EventArgs e)
{
NtruEncrypt newEnrypt = new NtruEncrypt();
H = newEnrypt.generatePub(F, param);
richTextBox2.Text = string.Join(" ", H.coeffs);
saveKeyBtn.Enabled = true;
MessageBox.Show("Waktu pembangkitan kunci public adalah:
" + newEnrypt.waktuGeneratePub + " ms.");
}
private void resetBtn_Click(object sender, EventArgs e)
{
F = null;
H = null;
param = null;
comboBox1.ResetText();
textBox1.ResetText();
textBox2.ResetText();
textBox3.ResetText();
richTextBox1.ResetText();
richTextBox2.ResetText();
button1.Enabled = false;
button2.Enabled = false;
saveKeyBtn.Enabled = false;
}
private void saveKeyBtn_Click(object sender, EventArgs e)
{
string keyName =
Microsoft.VisualBasic.Interaction.InputBox("Key name : ", "Save
Keys", "kunci . . .");
TabelKunci newtabel = new TabelKunci();
newtabel.id = Guid.NewGuid();
newtabel.Nama_Kunci = keyName;
newtabel.Parameters = paramString;
newtabel.Kunci_Private = richTextBox1.Text;
newtabel.Kunci_Publik = richTextBox2.Text;
DbTransaction dbTran;
entiti1.Connection.Open();
entiti1.AddToTabelKuncis(newtabel);
entiti1.SaveChanges();
dbTran = entiti1.Connection.BeginTransaction();
//The manipulation of DB

Universitas Sumatera Utara

A-25

dbTran.Commit();
this.Close();
}
private void closeBtn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

File : UseKey.cs
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace NtruEncryptAddIn
{
public partial class UseKey : Form
{
private AdoNetEntities entiti1;
public IntegerPolynomial pubKey = null;
public IntegerPolynomial privKey = null;
public EncryptionParameters param = null;
public enum formMode
{
getPriv, getPub
}
public formMode Mode
{
get;
set;
}
public UseKey()
{
InitializeComponent();
}
private void UseKey_Load(object sender, EventArgs e)
{
entiti1 = new AdoNetEntities();
Filter();
}
private void Filter()
{

Universitas Sumatera Utara

A-26

var dataInput = (from b in entiti1.TabelKuncis select new
{ b.id, b.Nama_Kunci, b.Parameters, b.Kunci_Private, b.Kunci_Publik
});
dataGridView1.DataSource = dataInput;
dataGridView1.Columns[0].Visible = false;
if(Mode == formMode.getPriv)
dataGridView1.Columns[4].Visible = false;
if(Mode == formMode.getPub)
dataGridView1.Columns[3].Visible = false;
dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;
if (dataGridView1.RowCount == 0)
button1.Enabled = false;
else
button1.Enabled = true;
}
private void getParam(string param)
{
if (param == "EES401EP1")
this.param = EncryptionParameters.EES401EP1;
if (param == "EES541EP1")
this.param = EncryptionParameters.EES541EP1;
if (param == "EES659EP1")
this.param = EncryptionParameters.EES659EP1;
if (param == "EES449EP1")
this.param = EncryptionParameters.EES449EP1;
if (param == "EES613EP1")
this.param = EncryptionParameters.EES613EP1;
if (param == "EES761EP1")
this.param = EncryptionParameters.EES761EP1;
if (param == "EES677EP1")
this.param = EncryptionParameters.EES677EP1;
if (param == "EES887EP1")
this.param = EncryptionParameters.EES887EP1;
if (param == "EES1087EP1")
this.param = EncryptionParameters.EES1087EP1;
if (param == "EES1087EP2")
this.param = EncryptionParameters.EES1087EP2;
if (param == "EES1171EP1")
this.param = EncryptionParameters.EES1171EP1;
if (param == "EES1499EP1")
this.param = EncryptionParameters.EES1499EP1;
}
private void button1_Click(object sender, EventArgs e)
{
string param =
dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
getParam(param); //this.param =
EncryptionParameters.findParams(param, this.param);
if (Mode == formMode.getPriv)
{
string privKey =
dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
int[] privKeyInt = privKey.Split(' ').Select(n =>
Convert.ToInt32(n)).ToArray();
this.privKey = new IntegerPolynomial(privKeyInt);
}

Universitas Sumatera Utara

A-27

if (Mode == formMode.getPub)
{
string pubKey =
dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
int[] pubKeyInt = pubKey.Split(' ').Select(n =>
Convert.ToInt32(n)).ToArray();
this.pubKey = new IntegerPolynomial(pubKeyInt);
}
this.Close();
}
}
}

File : FileEncryption.cs
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.IO;

namespace NtruEncryptAddIn
{
public partial class FileEncryption : Form
{
private IntegerPolynomial pubKey;
private EncryptionParameters param;
private IntegerPolynomial m;
private IntegerPolynomial E;
public FileEncryption()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(0, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
contextMenuStrip1.Show(ptLowerLeft);
}
private void fromDatabaseToolStripMenuItem_Click(object
sender, EventArgs e)
{
UseKey pubFromDB = new UseKey();
pubFromDB.Location = new Point(0, 50);
pubFromDB.Mode = UseKey.formMode.getPub;
pubFromDB.ShowDialog();
this.pubKey = pubFromDB.pubKey;
this.param = pubFromDB.param;
if(this.pubKey != null)
richTextBox1.Text = string.Join(" ", pubKey.coeffs);
}

Universitas Sumatera Utara

A-28

private void richTextBox1_TextChanged(object sender,
EventArgs e)
{
if (richTextBox1.Text == "")
button3.Enabled = false;
if (pubKey != null)
{
textBox3.Text = pubKey.coeffs.Length.ToString();
textBox4.Text = param.maxMsgLenBytes.ToString();
}
}
private void fromFileToolStripMenuItem_Click(object sender,
EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender,
CancelEventArgs e)
{
string upload =
File.ReadAllText(openFileDialog1.FileName);
try
{
string[] upload2 = upload.Split('/').Select(n =>
Convert.ToString(n)).ToArray();
if (upload2[1] == "1")
{
param =
EncryptionParameters.findParams(upload2[0], param);
int[] pubkey = upload2[2].Split(' ').Select(n =>
Convert.ToInt32(n)).ToArray();
this.pubKey = new IntegerPolynomial(pubkey);
richTextBox1.Text = upload2[2];
}
else
{
MessageBox.Show("Sorry, it is not a public key.
Please upload another key.");
}
}
catch (System.IndexOutOfRangeException)
{
MessageBox.Show("Sorry, it is not a public key.
Please upload another key.");
}
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog2.ShowDialog();
}
private void openFileDialog2_FileOk(object sender,
CancelEventArgs e)
{
string upload =
File.ReadAllText(openFileDialog2.FileName);

Universitas Sumatera Utara

A-29

richTextBox2.Text = upload;
}
private void richTextBox2_TextChanged(object sender,
EventArgs e)
{
if (richTextBox2.Text == "")
button3.Enabled = false;
else
button3.Enabled = true;
textBox1.Text = richTextBox2.Text.Length.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
if (richTextBox2.Text.Length > param.maxMsgLenBytes)
MessageBox.Show("Sorry, Your message is too
long.\nThe maximal length of message you can enrypt is " +
param.maxMsgLenBytes + " chars,\nyour message is " +
richTextBox2.Text.Length + " chars.");
else
{
byte[] pesanByte =
Encoding.GetEncoding(1252).GetBytes(richTextBox2.Text);
int[] pesanInt =
IntegerPolynomial.encodePolynomial(pesanByte, param.N);
m = new IntegerPolynomial(pesanInt);
IntegerPolynomial r =
IntegerPolynomial.generateRandomSmall(param.N, param.dr, param.dr);
NtruEncrypt newEncrypt = new NtruEncrypt();
E = newEncrypt.encrypt(m, r, pubKey, param);
Base64Reon newBase = new Base64Reon();
char[] char1 = newBase.intToBase64(E.coeffs, 12);
richTextBox3.Text = new string(char1);
MessageBox.Show("Proses enkripsi berhasil.\nWaktu
enkripsi : " + newEncrypt.waktuEnkripsi.ToString() + " ms.\nWaktu
encode base64 : " + newBase.waktuEncode.ToString() + " ms.");
}
}
private void richTextBox3_TextChanged(object sender,
EventArgs e)
{
if (richTextBox3.Text == "")
button4.Enabled = false;
else
button4.Enabled = true;
textBox2.Text = richTextBox3.Text.Length.ToString();
}
private void button5_Click(object sender, EventArgs e)
{
richTextBox1.ResetText();
richTextBox2.ResetText();
richTextBox3.ResetText();
}
private void button4_Click(object sender, EventArgs e)
{

Universitas Sumatera Utara

A-30

saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender,
CancelEventArgs e)
{
string nama = saveFileDialog1.FileName;
File.WriteAllText(nama, richTextBox3.Text);
}
}
}

File : FileDecryption.cs
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.IO;

namespace NtruEncryptAddIn
{
public partial class FileDecryption : Form
{
private IntegerPolynomial privKey;
private EncryptionParameters param;
private IntegerPolynomial E;
private IntegerPolynomial C;
private string cipher;
public FileDecryption()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(0, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
contextMenuStrip1.Show(ptLowerLeft);
}
private void fromDatabaseToolStripMenuItem_Click(object
sender, EventArgs e)
{
UseKey privFromDB = new UseKey();
privFromDB.Location = new Point(0, 50);
privFromDB.Mode = UseKey.formMode.getPriv;
privFromDB.ShowDialog();
this.privKey = privFromDB.privKey;
this.param = privFromDB.param;
if (this.privKey != null)

Universitas Sumatera Utara

A-31

richTextBox1.Text = string.Join(" ",
this.privKey.coeffs);
}
private void fromFileToolStripMenuItem_Click(object sender,
EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender,
CancelEventArgs e)
{
string upload =
File.ReadAllText(openFileDialog1.FileName);
try
{
string[] upload2 = upload.Split('/').Select(n =>
Convert.ToString(n)).ToArray();
if (upload2[1] == "0")
{
param =
EncryptionParameters.findParams(upload2[0], param);
int[] privkey = upload2[2].Split(' ').Select(n =>
Convert.ToInt32(n)).ToArray();
this.privKey = new IntegerPolynomial(privkey);
richTextBox1.Text = upload2[2];
}
else
{
MessageBox.Show("Sorry, it is not a private key.
Please upload another key.");
}
}
catch (System.IndexOutOfRangeException)
{
MessageBox.Show("Sorry, it is not a private key.
Please upload another key.");
}
}
private void richTextBox1_TextChanged(object sender,
EventArgs e)
{
if (richTextBox1.Text == "")
button3.Enabled = false;
if(privKey!=null)
textBox3.Text = privKey.coeffs.Length.ToString();
}
private void richTextBox3_TextChanged(object sender,
EventArgs e)
{
if (richTextBox3.Text == "")
button4.Enabled = false;
else
button4.Enabled = true;
textBox2.Text = richTextBox3.Text.Length.ToString();
}

Universitas Sumatera Utara

A-32

private void button3_Click(object sender, EventArgs e)
{
char[] char1 = cipher.ToCharArray();
Base64Reon newBase = new Base64Reon();
int[] encryptedInt = newBase.Base64ToInt(char1, 12);
E = new IntegerPolynomial(encryptedInt);
NtruEncrypt newEncrypt = new NtruEncrypt();
C = newEncrypt.decrypt(E, privKey, param);
byte[] decryptedByte =
IntegerPolynomial.decodePolynomial(C.coeffs);
richTextBox3.Text =
Encoding.GetEncoding(1252).GetString(decryptedByte);
MessageBox.Show("Proses dekripsi berhasil.\nWaktu
dekripsi : " + newEncrypt.waktuDekripsi.ToString() + " ms.\nWaktu
decode base64 : " + newBase.waktuDecode.ToString() + " ms.");
}
private void button5_Click(object sender, EventArgs e)
{
richTextBox1.ResetText();
richTextBox2.ResetText();
richTextBox3.ResetText();
}
private void richTextBox2_TextChanged(object sender,
EventArgs e)
{
if (richTextBox2.Text == "" || richTextBox1.Text == "")
button3.Enabled = false;
else
button3.Enabled = true;
textBox1.Text = richTextBox2.Text.Length.ToString();
cipher = richTextBox2.Text;
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog2.ShowDialog();
}
private void openFileDialog2_FileOk(object sender,
CancelEventArgs e)
{
string upload =
File.ReadAllText(openFileDialog2.FileName);
richTextBox2.Text = upload;
}
private void button4_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender,
CancelEventArgs e)
{
string nama = saveFileDialog1.FileName;
File.WriteAllText(nama, richTextBox3.Text);
}

Universitas Sumatera Utara

A-33

}
}

File : HelpAddIn.cs
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace NtruEncryptAddIn
{
public partial class HelpAddIn : Form
{
public HelpAddIn()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void checkBox1_CheckedChanged(object sender,
EventArgs e)
{
if (checkBox1.Checked == true)
this.TopMost = true;
else
this.TopMost = false;
}
}
}

File : AboutAddIn.cs
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Drawing;
System.Linq;
System.Reflection;
System.Windows.Forms;

namespace NtruEncryptAddIn
{
partial class AboutAddIn : Form
{
public AboutAddIn()
{
InitializeComponent();/*
this.Text = String.Format("About {0}", AssemblyTitle);
this.labelProductName.Text = AssemblyTitle;

Universitas Sumatera Utara

A-34

this.labelVersion.Text = String.Format("Version {0}",
AssemblyProduct);
this.labelCopyright.Text = AssemblyVersion;
this.labelCompanyName.Text = AssemblyCompany;
this.textBoxDescription.Text = AssemblyDescription;
* */
}
#region Assembly Attribute Accessors
public string AssemblyTitle
{
get
{
object[] attributes =
Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTi
tleAttribute), false);
if (attributes.Length > 0)
{
AssemblyTitleAttribute titleAttribute =
(AssemblyTitleAttribute)attributes[0];
if (titleAttribute.Title != "")
{
return titleAttribute.Title;
}
}
return
System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssem
bly().CodeBase);
}
}
public string AssemblyVersion
{
get
{
return
Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
public string AssemblyDescription
{
get
{
object[] attributes =
Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDe
scriptionAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return
((AssemblyDescriptionAttribute)attributes[0]).Description;
}
}
public string AssemblyProduct
{

Universitas Sumatera Utara

A-35

get
{
object[] attributes =
Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyPr
oductAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return
((AssemblyProductAttribute)attributes[0]).Product;
}
}
public string AssemblyCopyright
{
get
{
object[] attributes =
Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCo
pyrightAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return
((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
}
public string AssemblyCompany
{
get
{
object[] attributes =
Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCo
mpanyAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return
((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
#endregion
}
}

Universitas Sumatera Utara