LAMPIRAN A : LISTING PROGRAM
File : EncryptionParameters.cs
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace NtruEncryptAddIn {
public class EncryptionParameters {
public static readonly EncryptionParameters EES401EP1 = new EncryptionParameters(401, 2048, 113);
public static readonly EncryptionParameters EES541EP1 = new EncryptionParameters(541, 2048, 49);
public static readonly EncryptionParameters EES659EP1 = new EncryptionParameters(659, 2048, 38);
public static readonly EncryptionParameters EES449EP1 = new EncryptionParameters(449, 2048, 134);
public static readonly EncryptionParameters EES613EP1 = new EncryptionParameters(613, 2048, 55);
public static readonly EncryptionParameters EES761EP1 = new EncryptionParameters(761, 2048, 42);
public static readonly EncryptionParameters EES677EP1 = new EncryptionParameters(677, 2048, 157);
public static readonly EncryptionParameters EES887EP1 = new EncryptionParameters(887, 2048, 81);
public static readonly EncryptionParameters EES1087EP1 = new EncryptionParameters(1087, 2048, 63);
public static readonly EncryptionParameters EES1087EP2 = new EncryptionParameters(1087, 2048, 120);
public static readonly EncryptionParameters EES1171EP1 = new EncryptionParameters(1171, 2048, 106);
public static readonly EncryptionParameters EES1499EP1 = new EncryptionParameters(1499, 2048, 79);
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":
break; case "2":
param = EncryptionParameters.EES541EP1; break;
case "3":
param = EncryptionParameters.EES659EP1; break;
case "4":
param = EncryptionParameters.EES449EP1; break;
case "5":
param = EncryptionParameters.EES613EP1; break;
case "6":
param = EncryptionParameters.EES761EP1; break;
case "7":
param = EncryptionParameters.EES677EP1; break;
case "8":
param = EncryptionParameters.EES887EP1; break;
case "9":
param = EncryptionParameters.EES1087EP1; break;
case "10":
param = EncryptionParameters.EES1087EP2; break;
case "11":
param = EncryptionParameters.EES1171EP1; break;
case "12":
param = EncryptionParameters.EES1499EP1; break;
}
return param; }
} }
File : IntegerPolynomial.cs
using System;
using System.Collections.Generic; using System.Linq;
using System.Text; using System.Numerics;
namespace NtruEncryptAddIn {
public class IntegerPolynomial {
public int[] coeffs;
public IntegerPolynomial(int N) {
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;
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<int> coeffs = new List<int>(); 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 <= N; i++) {
f.coeffs[i - 1] = f.coeffs[i];
c.coeffs[N + 1 - i] = c.coeffs[N - i]; }
f.coeffs[N] = 0; c.coeffs[0] = 0; k++;
if (f.equalsZero()) return null; }
if (f.equalsOne()) break;
if (f.degree() < g.degree()) {
// exchange f and g
g = temp;
// exchange b and c temp = b;
b = c; c = temp; }
f.add(g, 2); b.add(c, 2); }
if (b.coeffs[N] != 0) return null;
// Fq(x) = x^(N-k) * b(x)
IntegerPolynomial Fq = new IntegerPolynomial(N); int j = 0;
k %= N;
for (int i = N - 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 + 1); b.coeffs[0] = 1;
IntegerPolynomial c = new IntegerPolynomial(N + 1); IntegerPolynomial f = new IntegerPolynomial(N + 1); 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 + 1); g.coeffs[0] = -1;
g.coeffs[N] = 1;
while (f.coeffs[0] == 0) {
for (int i = 1; i <= N; i++) {
f.coeffs[i - 1] = f.coeffs[i]; //f(x) = f(x)/x
c.coeffs[N + 1 - i] = c.coeffs[N - i]; //c(x) = c(x)*x
}
f.coeffs[N] = 0; c.coeffs[0] = 0; k++;
if (f.equalsZero())
return null; //not invertible }
if (f.equalsAbsOne()) break;
if (f.degree() < g.degree()) {
// exchange f and g
IntegerPolynomial temp = f; f = g;
g = temp;
// exchange b and c temp = b;
b = c; c = temp; }
if (f.coeffs[0] == g.coeffs[0]) {
f.sub(g, 3); b.sub(c, 3); }
else {
f.add(g, 3); b.add(c, 3); }
}
if (b.coeffs[N] != 0) return null;
// Fp(x) = [+-] x^(N-k) * b(x)
IntegerPolynomial Fp = new IntegerPolynomial(N); int j = 0;
k %= N;
for (int i = N - 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;
}
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
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; }
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; }
} }
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
using 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<bool> inputBool = int2BoolBits(input, bitsLength); List<bool> 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<bool> 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<bool> int2BoolBits(int[] input, int bitslength)
{
List<bool> bools = new List<bool>(); string s;
char[] bits;
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<bool> BoolBits2base64(List<bool> list) {
List<bool> list1 = new List<bool>(); 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; }
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] <= 'Z') {
b[i] = (byte)((int)c[i] - (int)'A'); }
else if (c[i] >= 'a' && c[i] <= 'z') {
b[i] = (byte)((int)c[i] - (int)'a' + 26); }
else if (c[i] >= '0' && c[i] <= '9') {
b[i] = (byte)((int)c[i] - (int)'0' + 52); }
else if (c[i] == s_CharPlusSign) {
b[i] = (byte)62; }
else {
b[i] = (byte)63; }
}
return b; }
private static List<bool> Byte2Bools(byte[] byte1) {
BitArray BA1 = new BitArray(byte1); bool[] boolArr = new bool[BA1.Length]; BA1.CopyTo(boolArr, 0);
List<bool> list1 = new List<bool>(boolArr); list1.Reverse();
List<bool> list2 = new List<bool>();
for (int i = 2; i < boolArr.Length; i += 8) {
list2.AddRange(list1.GetRange(i, 6)); }
return list2; }
private static int[] BoolBits2Int(List<bool> list1, int bitslength)
{
int stringLength = list1.Count/bitslength; string[] s = new string[stringLength]; int[] int1 = new int[s.Length];
int x = 0;
for (int i = 0; i < s.Length; i++) {
for (int j = x; j < (bitslength + x); j++) {
else if (list1[j] == true) s[i] += "1";
}
x += bitslength; }
for (int i = 0; i < int1.Length; i++) int1[i] = Convert.ToInt32(s[i], 2);
return int1; }
} }
File : NtruEncrypt.cs
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace NtruEncryptAddIn {
class NtruEncrypt {
public int waktuGeneratePriv; public int waktuGeneratePub; public int waktuEnkripsi; public int waktuDekripsi;
public IntegerPolynomial generatePriv(EncryptionParameters param)
{
DateTime start; DateTime finish; TimeSpan total; int waktuPriv;
start = DateTime.Now;
int N = param.N; int q = param.q; int df = param.df; int dg = param.dg;
IntegerPolynomial f = null; IntegerPolynomial fp = null; IntegerPolynomial fq = null; do
{
f = IntegerPolynomial.generateRandomSmall(N, df, df - 1);
fp = f.invertF3(); fq = f.invertFq(q);
} while (fp == null || fq == null); // repeat until f is invertible
finish = DateTime.Now; total = finish - start;
this.waktuGeneratePriv = waktuPriv;
return f; }
public IntegerPolynomial generatePub(IntegerPolynomial priv, EncryptionParameters param)
{
DateTime start; DateTime finish; TimeSpan total; int waktuPub;
start = DateTime.Now; int N = param.N; int q = param.q; int df = param.df; int dg = param.dg;
IntegerPolynomial fq = priv.invertFq(q); IntegerPolynomial g =
IntegerPolynomial.generateRandomSmall(N, dg, dg); IntegerPolynomial h = fq.mult(g, q); h.mult3(q);
h.ensurePositive(q); g.clear();
fq.clear();
finish = DateTime.Now; total = finish - start;
waktuPub = Convert.ToInt32(total.TotalMilliseconds); this.waktuGeneratePub = waktuPub;
return h; }
public IntegerPolynomial encrypt(IntegerPolynomial m,
IntegerPolynomial r, IntegerPolynomial pubKey, EncryptionParameters param)
{
DateTime start; DateTime finish; TimeSpan total; int waktuEnkripsi; start = DateTime.Now;
IntegerPolynomial e = r.mult(pubKey, param.q); e.add(m, param.q);
e.ensurePositive(param.q); finish = DateTime.Now; total = finish - start;
waktuEnkripsi = Convert.ToInt32(total.TotalMilliseconds); this.waktuEnkripsi = waktuEnkripsi;
return e; }
public IntegerPolynomial decrypt(IntegerPolynomial e, IntegerPolynomial privkey, EncryptionParameters param)
{
start = DateTime.Now;
IntegerPolynomial a = privkey.mult(e, param.q); a.center0(param.q);
a.mod3();
IntegerPolynomial fp = privkey.invertF3(); IntegerPolynomial c = fp.mult(a, 3);
c.center0(3);
finish = DateTime.Now; total = finish - start;
waktuDekripsi = Convert.ToInt32(total.TotalMilliseconds); this.waktuDekripsi = waktuDekripsi;
return c; }
} }
File : NtruRibbon.cs
using System;
using System.Collections.Generic; using System.Linq;
using Microsoft.Office.Tools.Ribbon; using System.Windows.Forms;
using System.Drawing;
using Outlook = Microsoft.Office.Interop.Outlook; using System.Text;
using System.IO;
namespace NtruEncryptAddIn {
public partial class NtruEncryptRibbon {
private string pesan = null;
private IntegerPolynomial pubKey = null; private IntegerPolynomial privKey = null; private EncryptionParameters param;
private void NtruRibbon_Load(object sender, RibbonUIEventArgs e)
{
if(RibbonId.Equals("Microsoft.Outlook.Mail.Compose") || RibbonId.Equals("Microsoft.Outlook.Mail.Read"))
group2.Visible = true; else
group2.Visible = false; }
private void KeyBtn_Click(object sender, RibbonControlEventArgs e)
{
SavedKeysForm savedKeys = new SavedKeysForm(); savedKeys.Location = new Point(0, 50);
private void button1_Click(object sender, RibbonControlEventArgs 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 (pubKey == null)
encryptBtn.Enabled = false; else
encryptBtn.Enabled = true; }
private void button3_Click(object sender, RibbonControlEventArgs 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 (privKey == null)
decryptBtn.Enabled = false; else
decryptBtn.Enabled = true; }
private void button2_Click(object sender, RibbonControlEventArgs e)
{
openFileDialog1.ShowDialog(); }
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string upload =
File.ReadAllText(openFileDialog1.FileName); try
{
string[] upload2 = upload.Split('/').Select(n => 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.");
} }
{
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;
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)
{
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 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.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 =
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";
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") paramUsed = "12";
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";
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 System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Data.Common; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using 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") {
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";
}
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.Commit();
this.Close(); }
private void closeBtn_Click(object sender, EventArgs e) {
this.Close(); }
} }
File : UseKey.cs
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;
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();
}
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();
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 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.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)
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 =
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(); }
saveFileDialog1.ShowDialog(); }
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
string nama = saveFileDialog1.FileName; File.WriteAllText(nama, richTextBox3.Text); }
} }
File : FileDecryption.cs
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.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();
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;
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)
{
} }
File : HelpAddIn.cs
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;
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 System;
using System.Collections.Generic; using System.ComponentModel; using System.Drawing;
using System.Linq;
using System.Reflection; using System.Windows.Forms;
namespace NtruEncryptAddIn {
partial class AboutAddIn : Form {
public AboutAddIn() {
InitializeComponent();/*
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; }
}
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 }