I have here a test method for a cypher machine, but I'm not sure I've passed in the culture and type correctly. Below is my test class and further down you will find a copy of my original code.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WpfApplication3;
using System.Globalization;
using WpfApplication3;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
CultureInfo current = CultureInfo.CurrentCulture;
EncryptConverter logic = new EncryptConverter();
public Type T(string type)
{
CultureInfo english = CultureInfo.CreateSpecificCulture("English");
Type classtype = Type.GetType("English");
return classtype;
}
public object value()
{
string plainText = "abc";
char[] chars = new char[plainText.Length];
string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
string val = chars.ToString();
}
}
return new string(chars);
//string val = chars.ToString();
}
public static object parameter()
{
string plainText = "abc";
char[] chars = new char[plainText.Length];
string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
}
}
return new string(chars);
}
[TestMethod]
public void TestMethod1()
{
string plainText = "=2/";
char[] chars = new char[plainText.Length];
string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
}
string val = chars.ToString();
string par = chars.ToString();
Type classtype = Type.GetType("English");
object test = logic.Convert(val, classtype, par, current);
test.ToString();
if (test != "=2/")
{
throw new Exception("fail");
}
}
}
}
}
Below is my base code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
// View with no code-behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
// From plain to encripted Text
public class EncryptConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string plainText = (string)value;
char[] chars = new char[plainText.Length];
string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
string recrypt = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
}
}
return new string(chars);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
// From encripted to plain Text
public class DecryptConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string decodeText = (string)value;
char[] chars = new char[decodeText.Length];
string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
for (int i = 0; i < decodeText.Length; i++)
{
if (decodeText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = key.IndexOf(decodeText[i]) + 97;
chars[i] = (char)j;
}
}
return new string(chars);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Aucun commentaire:
Enregistrer un commentaire