LISTING PROGRAM
Form Utama :
using System;
using System.Collections.Generic; using System.Linq;
using System.Net; using System.Windows;
using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using vigenere_des.Resources;
namespace vigenere_des {
public partial class MainPage : PhoneApplicationPage {
public MainPage() {
InitializeComponent(); }
private void Button_Click(object sender, RoutedEventArgs e) {
Application.Current.Terminate(); }
private void Button_Click_1(object sender, RoutedEventArgs e) {
NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute));
}
private void Button_Click_2(object sender, RoutedEventArgs e) {
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.RelativeOrAbsolute));
}
private void Button_Click_3(object sender, RoutedEventArgs e) {
NavigationService.Navigate(new Uri("/Page3.xaml", UriKind.RelativeOrAbsolute));
}
private void Button_Click_4(object sender, RoutedEventArgs e) {
NavigationService.Navigate(new Uri("/Page4.xaml", UriKind.RelativeOrAbsolute));
Form Buat Pesan :
using System; using System.IO;
using System.Collections.Generic; using System.Linq;
using System.Net; using System.Windows;
using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using Microsoft.Phone.UserData; using System.Security.Cryptography; using System.Text;
using Microsoft.Phone.Tasks;
namespace vigenere_des {
public partial class Page3 : PhoneApplicationPage {
public Page3() {
InitializeComponent(); }
string enkripsiv(string pesan, string kunci) {
int pjgkunci = kunci.Length; int pjgpesan = pesan.Length; string hasil = "";
for (int i = 0; i < pjgpesan; i++) {
hasil = hasil + Convert.ToChar((((int)pesan[i] + (int)kunci[i % (pjgkunci)]) % 95) + 32);
}
return hasil; }
private void Button2_Click(object sender, RoutedEventArgs e) {
string message = plaint.Text; string key = plaink.Text;
ciphert.Text = enkripsiv(message, key); }
private void Button3_Click(object sender, RoutedEventArgs e) {
Cryptographer cryptographer = new
Cryptographer(Algorithms.DES);
cipherk.Text = cryptographer.EncryptionStart(plaink.Text, desk.Text.ToUpper(), false);
}
private void Button4_Click(object sender, RoutedEventArgs e) {
SmsComposeTask smsComposeTask = new SmsComposeTask(); smsComposeTask.To = buku_telepon.Text;
smsComposeTask.Body = ciphert.Text; smsComposeTask.Show();
}
string data;
private void txtName_GotFocus(object sender, RoutedEventArgs e)
{
TextBox t = sender as TextBox; data = t.Text;
t.Text = string.Empty;
}
private void txtName_LostFocus(object sender, RoutedEventArgs e)
{
TextBox t = sender as TextBox; if (t.Text.Equals(string.Empty)) {
t.Text = data; }
}
private void Button1_Click(object sender, RoutedEventArgs e) {
Contacts cons = new Contacts();
cons.SearchCompleted += new
EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
System.Text.StringBuilder sb = new
System.Text.StringBuilder();
foreach (Contact con in e.Results) {
sb.AppendLine(con.DisplayName); }
MessageBox.Show(sb.ToString()); }
Form Dekrip Pesan :
using System;
using System.Collections.Generic; using System.Linq;
using System.Net; using System.Windows;
using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell;
namespace vigenere_des {
public partial class Page4 : PhoneApplicationPage {
public Page4() {
InitializeComponent(); }
string dekripsiv(string pesan, string kunci) {
int pjgkunci = kunci.Length; int pjgpesan = pesan.Length; string hasil = "";
for (int i = 0; i < pjgpesan; i++) {
if ((((int)pesan[i] - 32) - (int)kunci[i % (pjgkunci - 1)]) < -63)
{
hasil = hasil + Convert.ToChar((((int)pesan[i] -32) - (int)kunci[i % (pjgkunci)]) + 190);
}
else if ((((int)pesan[i] - 32) - (int)kunci[i % (pjgkunci - 1)]) < 32)
{
hasil = hasil + Convert.ToChar((((int)pesan[i] -32) - (int)kunci[i % (pjgkunci)]) + 95);
} else {
hasil = hasil + Convert.ToChar((((int)pesan[i] -32) - (int)kunci[i % (pjgkunci)]) % 95);
} }
return hasil; }
private void Button3_Click(object sender, RoutedEventArgs e) {
string message = ciphert1.Text; string key = plaink1.Text;
plaint1.Text = dekripsiv(message, key); }
{
Cryptographer cryptographer = new
Cryptographer(Algorithms.DES);
plaink1.Text =
cryptographer.DecryptionStart(cipherk1.Text, desk.Text.ToUpper(), false);
}
private void plaint_TextChanged(object sender, TextChangedEventArgs e)
{
}
string data;
private void txtName_GotFocus(object sender, RoutedEventArgs e)
{
TextBox t = sender as TextBox; data = t.Text;
t.Text = string.Empty;
}
private void txtName_LostFocus(object sender, RoutedEventArgs e)
{
TextBox t = sender as TextBox; if (t.Text.Equals(string.Empty)) {
t.Text = data; }
} }
}
Class Algorithms.cs :
using System;
using System.Collections.Generic; using System.Text;
namespace vigenere_des {
class Algorithms {
public static readonly int DES = 0; }
Class Cryptographer.cs :
using System;
using System.Collections.Generic; using System.Text;
using System.Threading;
namespace vigenere_des
{
class Cryptographer {
CommonProcess cProcess = null;
public Cryptographer(int algorithm_number) {
if (Algorithms.DES == algorithm_number) {
cProcess = new DES.ProcessDES(); }
}
public string EncryptionStart(string text, string key, bool IsBinary)
{
return cProcess.EncryptionStart(text, key, IsBinary); }
public string DecryptionStart(string text, string key, bool IsBinary)
{
return cProcess.DecryptionStart(text, key, IsBinary); }
} }
Class Commonprocess.cs :
using System;
using System.Collections.Generic; using System.Text;
namespace vigenere_des {
abstract class CommonProcess {
public abstract string EncryptionStart(string text, string key, bool IsTextBinary);
public abstract string DecryptionStart(string text, string key, bool IsTextBinary);
Class DESData.cs :
using System;
using System.Collections.Generic; using System.Text;
namespace vigenere_des.DES {
#region tabel permutasi class DESData
{
public static readonly int[] pc_1 ={ 57, 49, 41, 33, 25, 17, 9, 1, 58, 50,
42, 34, 26, 18, 10, 2, 59, 51, 43, 35,
27, 19, 11, 3, 60, 52, 44, 36, 63, 55,
47, 39, 31, 23, 15, 7, 62, 54, 46, 38,
30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
13, 5, 28, 20, 12, 4 };
public static readonly int[] pc_2 ={ 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21,
10, 23, 19, 12, 4, 26, 8, 16, 7, 27,
20, 13, 2, 41, 52, 31, 37, 47, 55, 30,
40, 51, 45, 33, 48, 44, 49, 39, 56, 34,
53, 46, 42, 50, 36, 29, 32 };
public static readonly int[] ip ={ 58, 50, 42, 34, 26, 18, 10, 2, 60, 52,
44, 36, 28, 20, 12, 4, 62, 54, 46, 38,
30, 22, 14, 6, 64, 56, 48, 40, 32, 24,
16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59,
51, 43, 35, 27, 19, 11, 3, 61, 53, 45,
37, 29, 21, 13, 5, 63, 55, 47, 39, 31,
23, 15, 7 };
public static readonly int[] ip_1 ={ 40, 8, 48, 16, 56, 24, 64, 32, 39, 7,
47, 15, 55, 23, 63, 31, 38, 6, 46, 14,
54, 22, 62, 30, 37, 5, 45, 13, 53, 21,
61, 29, 36, 4, 44, 12, 52, 20, 60, 28,
42, 10, 50, 18, 58, 26, 33, 1, 41, 9,
49, 17, 57, 25 };
public static readonly int[] pc_e ={ 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8,
9, 10, 11, 12, 13, 12, 13, 14, 15, 16,
17, 16, 17, 18, 19, 20, 21, 20, 21, 22,
23, 24, 25, 24, 25, 26, 27, 28, 29, 28,
29, 30, 31, 32, 1 };
public static readonly int[] pc_p ={ 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23,
26, 5, 18, 31, 10, 2, 8, 24, 14, 32,
27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 };
public static readonly int[] nrOfShifts ={ 0, 1, 1, 2, 2, 2, 2, 2, 2, 1,
2, 2, 2, 2, 2, 2, 1 };
public static List<int[,]> sBoxes = new List<int[,]>();
static DESData() {
sBoxes.Add(s1); sBoxes.Add(s2); sBoxes.Add(s3); sBoxes.Add(s4); sBoxes.Add(s5); sBoxes.Add(s6); sBoxes.Add(s7); sBoxes.Add(s8); }
public static readonly int[,] s1 ={ { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
{ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
{ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } };
public static readonly int[,] s2 ={ { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
{ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
{ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
public static readonly int[,] s3 ={ { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
{ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
{ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
{ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } };
public static readonly int[,] s4 ={ { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
{ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
{ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } };
public static readonly int[,] s5 ={ { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
{ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
{ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
{ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } };
public static readonly int[,] s6 ={ { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
{ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
{ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
{ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } };
public static readonly int[,] s7 ={ { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
{ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
{ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
{ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } };
public static readonly int[,] s8 ={ { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
{ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
{ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };
}
#region pembuatan kelas kunci class Keys
{
public string[] Cn = new string[17]; public string[] Dn = new string[17];
public string[] Kn = new string[16]; }
#endregion }
Class ProcessDES.cs :
using System;
using System.Collections.Generic; using System.Text;
namespace vigenere_des.DES {
class ProcessDES : CommonProcess {
public ProcessDES() {
}
#region proses enkripsi
public override string EncryptionStart(string text, string key, bool IsTextBinary)
{
#region pengambilan 16 kunci
string binary_key = this.FromTextToBinary(key);
string key_plus = this.DoPermutation(binary_key, DESData.pc_1);
string C0 = "", D0 = "";
C0 = this.SetLeftHalvesKey(key_plus); D0 = this.SetRightHalvesKey(key_plus);
Keys keys = this.SetAllKeys(C0, D0);
#endregion
#region proses enkripsi
string binaryText = "";
if (IsTextBinary == false) {
binaryText = this.FromTextToBinary(text); }
else {
binaryText = this.setTextMutipleOf64Bits(binaryText);
StringBuilder EncryptedTextBuilder = new StringBuilder(binaryText.Length);
for (int i = 0; i < (binaryText.Length / 64); i++) {
string PermutatedText =
this.DoPermutation(binaryText.Substring(i * 64, 64), DESData.ip);
string L0 = "", R0 = "";
L0 = this.SetLeftHalvesKey(PermutatedText); R0 = this.SetRightHalvesKey(PermutatedText);
string FinalText = this.FinalEncription(L0, R0, keys, false);
string selesai = BinaryStringToHexString(FinalText); EncryptedTextBuilder.Append(selesai);
}
return EncryptedTextBuilder.ToString();
#endregion }
#endregion
#region proses dekripsi
public override string DecryptionStart(string text, string key, bool IsTextBinary)
{
#region pengambilan 16 kunci
string binary_key = this.FromTextToBinary(key);
string key_plus = this.DoPermutation(binary_key, DESData.pc_1);
string C0 = "", D0 = "";
C0 = this.SetLeftHalvesKey(key_plus); D0 = this.SetRightHalvesKey(key_plus);
Keys keys = this.SetAllKeys(C0, D0);
#endregion
#region proses dekripsi
string binaryText = "";
if (IsTextBinary == false) {
binaryText = this.FromHexToBinary(text); }
binaryText = text; }
binaryText = this.setTextMutipleOf64Bits(binaryText);
StringBuilder DecryptedTextBuilder = new StringBuilder(binaryText.Length);
for (int i = 0; i < (binaryText.Length / 64); i++) {
string PermutatedText =
this.DoPermutation(binaryText.Substring(i * 64, 64), DESData.ip);
string L0 = "", R0 = "";
L0 = this.SetLeftHalvesKey(PermutatedText); R0 = this.SetRightHalvesKey(PermutatedText);
string FinalText = this.FinalEncription(L0, R0, keys, true);
string selesai = FromBinaryToText(FinalText);
DecryptedTextBuilder.Append(selesai); }
return DecryptedTextBuilder.ToString();
#endregion }
#endregion
public static string BinaryStringToHexString(string binary) {
StringBuilder result = new StringBuilder(binary.Length / 8 + 1);
int mod4Len = binary.Length % 8; if (mod4Len != 0)
{
binary = binary.PadLeft(((binary.Length / 8) + 1) * 8, '0');
}
for (int i = 0; i < binary.Length; i += 8) {
string eightBits = binary.Substring(i, 8); result.AppendFormat("{0:X2}",
Convert.ToByte(eightBits, 2)); }
return result.ToString(); }
#region mengubah biner menjadi ascii
public string FromBinaryToText(string binarystring) {
StringBuilder text = new
for (int i = 0; i < (binarystring.Length / 8); i++) {
string word = binarystring.Substring(i * 8, 8); text.Append((char)Convert.ToInt32(word, 2)); }
return text.ToString(); }
#endregion
#region pembagian teks menjadi 64 bit
public string setTextMutipleOf64Bits(string text) {
if ((text.Length % 64) != 0) {
int maxLength = 0;
maxLength = ((text.Length / 64) + 1) * 64; text = text.PadRight(maxLength, '0'); }
return text; }
#endregion
#region mengubah ASCII menjadi biner
public string FromTextToBinary(string text) {
StringBuilder binarystring = new
StringBuilder(text.Length * 8);
foreach (char word in text) {
int binary = (int)word; int factor = 128;
for (int i = 0; i < 8; i++) {
if (binary >= factor) {
binary -= factor;
binarystring.Append("1"); }
else {
binarystring.Append("0"); }
factor /= 2; }
}
return binarystring.ToString(); }
public static string FromDeciamlToBinary(int binary) {
if (binary < 0) {
Console.WriteLine("It requires a integer greater than 0.");
}
string binarystring = ""; int factor = 128;
for (int i = 0; i < 8; i++) {
if (binary >= factor) {
binary -= factor; binarystring += "1"; }
else {
binarystring += "0"; }
factor /= 2; }
return binarystring; }
public static byte FromBinaryToByte(string binary) {
byte value = 0; int factor = 128;
for (int i = 0; i < 8; i++) {
if (binary[i] == '1') {
value += (byte)factor; }
factor /= 2; }
return value; }
#endregion
#region mengubah heksa ke biner
public string FromHexToBinary(string hexstring) {
string binarystring = "";
try {
for (int i = 0; i < hexstring.Length; i++) {
int hex =
Convert.ToInt32(hexstring[i].ToString(), 16);
int factor = 8;
for(int j=0; j<4; j++) {
if (hex >= factor) {
binarystring += "1"; }
else {
binarystring += "0"; }
factor /= 2; }
} }
catch (Exception e) {
Console.WriteLine(e.Message + " - wrong hexa integer format.");
}
return binarystring; }
#endregion
#region perulangan
public string DoPermutation(string text, int[] order) {
StringBuilder PermutatedText = new
StringBuilder(order.Length);
for (int i = 0; i < order.Length; i++) {
PermutatedText.Append(text[order[i] - 1]); }
return PermutatedText.ToString(); }
//untuk S box
public string DoPermutation(string text, int[,] order) {
string PermutatedText = "";
int rowIndex = Convert.ToInt32(text[0].ToString() + text[text.Length - 1].ToString(), 2);
int colIndex = Convert.ToInt32(text.Substring(1, 4), 2);
PermutatedText =
ProcessDES.FromDeciamlToBinary(order[rowIndex, colIndex]);
return PermutatedText; }
#endregion
#region memecah blok menjadi kiri dan kanan public string SetLeftHalvesKey(string text) {
return this.SetHalvesKey(true, text); }
public string SetRightHalvesKey(string text) {
public string SetHalvesKey(bool IsLeft, string text) {
if ((text.Length % 8) != 0) {
Console.WriteLine("The key is not multiple of 8bit.");
return null; }
int midindex = (text.Length / 2) - 1;
string result = "";
if (IsLeft) {
result = text.Substring(0, midindex + 1); }
else {
result = text.Substring(midindex + 1); }
return result; }
#endregion
#region pergeseran ke kiri
public string LeftShift(string text) {
return this.LeftShift(text, 1); }
public string LeftShift(string text, int count) {
if (count < 1) {
Console.WriteLine("The count of leftshift is must more than 1 time.");
return null; }
string temp = text.Substring(0, count);
StringBuilder shifted = new StringBuilder(text.Length); shifted.Append(text.Substring(count) + temp);
return shifted.ToString(); }
#endregion
#region pengmabilan semua kunci
public Keys SetAllKeys(string C0, string D0) {
Keys keys = new Keys(); keys.Cn[0] = C0;
keys.Dn[0] = D0;
keys.Cn[i] = this.LeftShift(keys.Cn[i - 1], DESData.nrOfShifts[i]);
keys.Dn[i] = this.LeftShift(keys.Dn[i - 1], DESData.nrOfShifts[i]);
keys.Kn[i - 1] = this.DoPermutation(keys.Cn[i] + keys.Dn[i], DESData.pc_2);
}
return keys; }
#endregion
#region enkripsi
public string FinalEncription(string L0, string R0, Keys keys, bool IsReverse)
{
string Ln = "", Rn = "", Ln_1 = L0, Rn_1 = R0;
int i = 0;
if (IsReverse == true) {
i = 15; }
while (this.IsEnough(i, IsReverse)) {
Ln = Rn_1;
Rn = this.XOR(Ln_1, this.f(Rn_1, keys.Kn[i]));
Ln_1 = Ln; Rn_1 = Rn;
if (IsReverse == false) {
i += 1; }
else {
i -= 1; }
}
string R16L16 = Rn + Ln;
string Encripted_Text = this.DoPermutation(R16L16, DESData.ip_1);
return Encripted_Text; }
public bool IsEnough(int i, bool IsReverse) {
return (IsReverse == false) ? i < 16 : i >= 0; }
#endregion
#region fungsi f
string E_Rn_1 = this.E_Selection(Rn_1);
string XOR_Rn_1_Kn = this.XOR(E_Rn_1, Kn);
string sBoxedText = this.sBox_Transform(XOR_Rn_1_Kn);
string P_sBoxedText = this.P(sBoxedText);
return P_sBoxedText; }
#endregion
#region fungsi P
public string P(string text) {
string PermutatedText = "";
PermutatedText = this.DoPermutation(text, DESData.pc_p);
return PermutatedText; }
#endregion
#region transformasi s box
public string sBox_Transform(string text) {
StringBuilder TransformedText = new StringBuilder(32);
for (int i = 0; i < 8; i++) {
string temp = text.Substring(i * 6, 6);
TransformedText.Append(this.DoPermutation(temp, DESData.sBoxes[i]));
}
return TransformedText.ToString(); }
#endregion
#region pemilihan E
public string E_Selection(string Rn_1) {
string ExpandedText = this.DoPermutation(Rn_1, DESData.pc_e);
return ExpandedText; }
#endregion
#region XOR
public string XOR(string text1, string text2) {
if (text1.Length != text2.Length) {
Console.WriteLine("Two data blocks for XOR are must get same size.");
StringBuilder XORed_Text = new StringBuilder(text1.Length);
for (int i = 0; i < text1.Length; i++) {
if (text1[i] != text2[i]) {
XORed_Text.Append("1"); }
else {
XORed_Text.Append("0"); }
}
return XORed_Text.ToString(); }
#endregion }