|
Hey guys,
got a friend who is still stuck on vb6(says he loves it ) anyhow he passed me some code that i need to convert, i got it to .net with the VS.net03 tool. But now i need it to C#, i'm not a vb programmer so i'm struggling to understand why it isn't working right. i've tried using a few different code converters but they all fail!
I've enclosed the code... any help is auesome.... oh, and anything quick is even better. My deadline got bumped from next week to tomorrow am. (which is why i had my friend "help" me.)
k,
one more thing.. it WILL run in vb.net once i upgraded it from vb6, but i can not get it to compile in C#!
'UPGRADE_NOTE: Text was upgraded to Text_Renamed. Click for more: 'ms-help:
Private Function Crypt(ByVal Text_Renamed As String, ByVal Key As String) As String
Dim KeyLen, i, KeyPtr As Integer
Dim KeyChr, TextChr As Short
KeyLen = Len(Key)
For i = 1 To Len(Text_Renamed)
TextChr = Asc(Mid(Text_Renamed, i, 1))
KeyChr = Asc(Mid(Key, KeyPtr + 1, 1))
Mid(Text_Renamed, i, 1) = Chr(TextChr Xor KeyChr)
KeyPtr = ((KeyPtr + 1) Mod KeyLen)
Next i
Crypt = Text_Renamed
End Function
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
txtText.Text = CStr(Now)
txtPath.Text = VB6.GetPath & "\DateTest.bin"
End Sub
just in case it helps here is the vb6 code...
Private Function Crypt(ByVal Text As String, ByVal Key As String) As String
Dim i As Long, KeyLen As Long, KeyPtr As Long, KeyChr As Integer, TextChr As Integer
KeyLen = Len(Key)
For i = 1 To Len(Text)
TextChr = Asc(Mid(Text, i, 1))
KeyChr = Asc(Mid(Key, KeyPtr + 1, 1))
Mid(Text, i, 1) = Chr(TextChr Xor KeyChr)
KeyPtr = ((KeyPtr + 1) Mod KeyLen)
Next i
Crypt = Text
End Function
Private Sub Form_Load()
txtText.Text = Now
txtPath.Text = App.Path & "\DateTest.bin"
End Sub
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
wetrivrrat wrote:
says he loves it
Kudos to you for sticking with your friend through this difficult time....
Your code converters suck, they are still calling old VB6 functions.
The core issue here is that you should not be writing your own cryptography functions, you should use the ones that .NET provides. However, what would help here is for you to show us the C# code you're ending up with and telling us what is going wrong.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I've never used the installed cyrpto....SO if you can help tha'd be auesome!!!
what i'm trying to do is:
Currently i'm passing a string value to a hash table then writing it to isolated storage. the problem is that it saves it in a .bat file which can be moded if opened in notepad THen i'm comparing that .bat file to a hardcoded value in the program (mini license attempt) but the problem is that if someone changes the value in the isolated storage the hash fails(only in debug mode) but since i wrote in string and i'm asking it to compare in string it will eventually pass the test because the string matched. even though the hash didn't. So I need to write to this isolated storage a hash / salt that will not show a string value. ie. i want to store the date of install and compare against the hard coded date in the program (i change that hard code every new install. yes i know thats not the best way, but for this installation it will) this way if someone tries to install it somewhere else it has to pass this and a list of other tests to pass for the install. and so they don't figure out the date is one of the keys i need it in a hash/salt once stored. All my friend was trying to do was give me a quick and dirty method that would do this. if you have an easier way please DO TELL!
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
You just want to create a hash and compare it to one you've stored ? How about MD5[^] ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
How about MD5[^] ?
Again, Never used the crypto, so all i need to do is this:
Hash hash = new Hash ( myAssembly );
Byte[] hashcode = hash.MD5;
myAssembly being the text that i'm trying to save? then would i just need to write hash.MD5 to my iso-storage? ie...
instead of this (Current)
storage["_"] = "Successful Install on " +dt.Date.ToShortDateString();
storage.Save();
this?
Hash hash = new Hash ("Successful Install on " +dt.Date.ToShortDateString())
Byte[] hashcode = hash.MD5
storage["_"] = hash.MD5;
storage.Save();
again, i'm only guessing here, so if not please help me out
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
Sorry, no. This[^] is the link I meant to give ( clicking on MD5 in the link I gave goes here ).
You need to convert your string to a byte[], then it's:
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
result is an array of bytes that represents the hash.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
You need to convert your string to a byte[], then it's:
Ok, so
byte[] data = new byte[DATA_SIZE];
do i need to set the DATA_SIZE ? or is that automatic?
and then how do i implement this?
it looks like i need to create a new class, then do i define the value of data in the class? or in my storage method?
ONce that is all done, how do i read this data.. aka, how do i reverse this process?
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
|
I tried that one, it does not work, plus it does not answer the question as it stands now.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
wetrivrrat wrote:
byte[] data = new byte[DATA_SIZE];
do i need to set the DATA_SIZE ? or is that automatic?
*sigh* Look, I have to ask you, when you read this stuff, do you consider what any of it means ? Your array will have a size, and the demo code is showing a variable to give the idea of the syntax. You need to create a byte array the size of the string. Here[^] is an article on converting a string to a byte[] and back.
wetrivrrat wrote:
it looks like i need to create a new class, then do i define the value of data in the class? or in my storage method?
What on earth for ? What makes you think you need to write a class ?
wetrivrrat wrote:
ONce that is all done, how do i read this data.. aka, how do i reverse this process?
Oh - I asked you before if you were comparing a stored hash to a new one. You cannot reverse a hash, that's one thing that makes it secure.
If you want something you can reverse, I'm not sure what algorithm to use. I would recommend retrieving a hash and comparing it to a hash of the value you expect, assuming that's what's going on here. If you're checking for a range, then you need to find another algorithm.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
*sigh*
You're not alone! I've been up since 6 somthing this morning going balls to the wall all day. I was sitting in a MS Word 1 intro class when I was writing to you about this(requirement for finacial aid for MCSE, don't ask) So between dealing with a teacher who didn't like me not paying attention, and having the fear of this deadline being bumped up a week I am not thinking too clearly.
that said i greatly appreciate your help thus far... and anything provided beyond this...
I have never had to implement any crypto in my apps, and this being the first i'm struggling to grasp the functions as i was dealing with the teacher...
What I need is the ablility to take today's date, hard code that to the app,
then install my app today, when it loads up the next time it checks to see if the date is the same or not, if it is it does set A of methods, if not it does set B of methods.
I am writing the date of install(installDate) to Isolated Storage thru a hashtable that
stores the info as a string(currently i'm too tired to remember why, and its too big a part of the app to disect it tonight(6hours to delivery) and reconstruct it.) then I compare that string to the date string hard coded in the app as well to today's date.
If a user found the iso-storage and changed the date to todays date then the program would re-install itself. I need to write the installDate to the iso-storage in a non-reconizable format that will then re-construct itself to the date of install for comparison.
I don't care if it is md5 or not, the reason i was looking for a way to convert the vb code was because it did what i needed it to, the problem is that I can't make it work in C#, and i'm not sure what needs to be done at this point to make it work. I tried the app from David Anton, but it has all kinds of errors that I haven't yet had the time/energy to debug, but i'll ask him about those.
If you know of a better way than this please enlighten me.... i'll post the code result from the converter David Anton suggested.
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
wetrivrrat wrote:
have never had to implement any crypto in my apps, and this being the first i'm struggling to grasp the functions as i was dealing with the teacher
OK, well, the first step is to remember that it's all still C#, the bits we're talking about now are pretty straightforward. You create an array of bytes and populate it. the example is showing you how to do that broadly, but copy and paste won't work, nor will you learn anything that way.
wetrivrrat wrote:
i'll post the code result from the converter David Anton suggested.
I already tried it, it's a disaster.
Do you really want to use code that you don't understand ?
Here it is, anyhow. This works, but I still think you're going about things the wrong way to be copying and pasting code you don't understand. I am just taking pity in this case because you're under the gun.
private string Crypt(string Text_Renamed, string Key)
{
int KeyLen = Key.Length;
string NewText = "";
int KeyPtr = 0;
for (int i = 0; (i < Text_Renamed.Length); i++)
{
short TextChr = Convert.ToInt16(Text_Renamed[i]);
short KeyChr = Convert.ToInt16(Key[KeyPtr]);
NewText += Convert.ToChar(TextChr ^ KeyChr);
KeyPtr = (KeyPtr+1) % KeyLen;
}
return NewText;
}
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
David, thanks for the direction to the app, here are the results for the project as a whole, enclosed are the results and the corresonding code.
I purposely did not omit anyting... sorry its so long...
I have higlighted the lines that are erroring.... I am currently working on debuggin this, but any assistance will be greatly apreciated!!!
remember this WAS working in vb after conversion from vb6 to vb.net.
1.\Form1.cs(269): No overload for method 'FileOpen' takes '3' arguments
2.\Form1.cs(270): The best overloaded method match for 'string.PadLeft(int)' has some invalid arguments
3.\Form1.cs(270): Argument '1': cannot convert from 'long' to 'int'
4.\Form1.cs(272): The best overloaded method match for 'Microsoft.VisualBasic.FileSystem.FileGet(int, ref System.ValueType, long)' has some invalid arguments
5.\Form1.cs(272): Argument '2': cannot convert from 'string' to 'ref System.ValueType'
6.\Form1.cs(293): No overload for method 'FileOpen' takes '3' arguments
7.\Form1.cs(331): Cannot implicitly convert type 'int' to 'short'
8.\Form1.cs(332): Cannot implicitly convert type 'int' to 'short'
using Microsoft.VisualBasic.Compatibility.VB6;
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
namespace Project1
{
internal class Form1 : System.Windows.Forms.Form
{
#region Windows Form Designer generated code
public Form1() : base()
{
if (m_vb6FormDefInstance == null)
{
if (m_InitializingDefInstance)
{
m_vb6FormDefInstance = this;
}
else
{
try
{
if (System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType == this.GetType())
{
m_vb6FormDefInstance = this;
}
}
catch
{
}
}
}
InitializeComponent();
}
protected override void Dispose(bool Disposing)
{
if (Disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(Disposing);
}
private System.ComponentModel.IContainer components;
public System.Windows.Forms.ToolTip ToolTip1;
public System.Windows.Forms.Button cmdDecrypt;
public System.Windows.Forms.TextBox txtPath;
public System.Windows.Forms.TextBox txtSalt;
public System.Windows.Forms.TextBox txtText;
public System.Windows.Forms.Button cmdEncrypt;
public System.Windows.Forms.Label Label2;
public System.Windows.Forms.Label Label1;
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.components = new System.ComponentModel.Container();
this.ToolTip1 = new System.Windows.Forms.ToolTip(components);
this.ToolTip1.Active = true;
this.cmdDecrypt = new System.Windows.Forms.Button();
this.txtPath = new System.Windows.Forms.TextBox();
this.txtSalt = new System.Windows.Forms.TextBox();
this.txtText = new System.Windows.Forms.TextBox();
this.cmdEncrypt = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.Text = "Form1";
this.ClientSize = new System.Drawing.Size(312, 213);
this.Location = new System.Drawing.Point(4, 23);
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation;
this.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
this.ControlBox = true;
this.Enabled = true;
this.KeyPreview = false;
this.MaximizeBox = true;
this.MinimizeBox = true;
this.Cursor = System.Windows.Forms.Cursors.Default;
this.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.ShowInTaskbar = true;
this.HelpButton = false;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.Name = "Form1";
this.cmdDecrypt.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.cmdDecrypt.Text = "Decrypt";
this.cmdDecrypt.Size = new System.Drawing.Size(97, 33);
this.cmdDecrypt.Location = new System.Drawing.Point(112, 128);
this.cmdDecrypt.TabIndex = 6;
this.cmdDecrypt.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.cmdDecrypt.BackColor = System.Drawing.SystemColors.Control;
this.cmdDecrypt.CausesValidation = true;
this.cmdDecrypt.Enabled = true;
this.cmdDecrypt.ForeColor = System.Drawing.SystemColors.ControlText;
this.cmdDecrypt.Cursor = System.Windows.Forms.Cursors.Default;
this.cmdDecrypt.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.cmdDecrypt.TabStop = true;
this.cmdDecrypt.Name = "cmdDecrypt";
this.txtPath.AutoSize = false;
this.txtPath.Size = new System.Drawing.Size(265, 25);
this.txtPath.Location = new System.Drawing.Point(8, 96);
this.txtPath.TabIndex = 5;
this.txtPath.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.txtPath.AcceptsReturn = true;
this.txtPath.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.txtPath.BackColor = System.Drawing.SystemColors.Window;
this.txtPath.CausesValidation = true;
this.txtPath.Enabled = true;
this.txtPath.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtPath.HideSelection = true;
this.txtPath.ReadOnly = false;
this.txtPath.MaxLength = 0;
this.txtPath.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtPath.Multiline = false;
this.txtPath.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtPath.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.txtPath.TabStop = true;
this.txtPath.Visible = true;
this.txtPath.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtPath.Name = "txtPath";
this.txtSalt.AutoSize = false;
this.txtSalt.Size = new System.Drawing.Size(137, 25);
this.txtSalt.Location = new System.Drawing.Point(8, 56);
this.txtSalt.TabIndex = 2;
this.txtSalt.Text = "WetRi";
this.txtSalt.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.txtSalt.AcceptsReturn = true;
this.txtSalt.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.txtSalt.BackColor = System.Drawing.SystemColors.Window;
this.txtSalt.CausesValidation = true;
this.txtSalt.Enabled = true;
this.txtSalt.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtSalt.HideSelection = true;
this.txtSalt.ReadOnly = false;
this.txtSalt.MaxLength = 0;
this.txtSalt.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtSalt.Multiline = false;
this.txtSalt.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtSalt.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.txtSalt.TabStop = true;
this.txtSalt.Visible = true;
this.txtSalt.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtSalt.Name = "txtSalt";
this.txtText.AutoSize = false;
this.txtText.Size = new System.Drawing.Size(137, 25);
this.txtText.Location = new System.Drawing.Point(8, 16);
this.txtText.TabIndex = 1;
this.txtText.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.txtText.AcceptsReturn = true;
this.txtText.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.txtText.BackColor = System.Drawing.SystemColors.Window;
this.txtText.CausesValidation = true;
this.txtText.Enabled = true;
this.txtText.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtText.HideSelection = true;
this.txtText.ReadOnly = false;
this.txtText.MaxLength = 0;
this.txtText.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtText.Multiline = false;
this.txtText.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtText.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.txtText.TabStop = true;
this.txtText.Visible = true;
this.txtText.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtText.Name = "txtText";
this.cmdEncrypt.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.cmdEncrypt.Text = "Encrypt and Save";
this.cmdEncrypt.Size = new System.Drawing.Size(97, 33);
this.cmdEncrypt.Location = new System.Drawing.Point(8, 128);
this.cmdEncrypt.TabIndex = 0;
this.cmdEncrypt.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.cmdEncrypt.BackColor = System.Drawing.SystemColors.Control;
this.cmdEncrypt.CausesValidation = true;
this.cmdEncrypt.Enabled = true;
this.cmdEncrypt.ForeColor = System.Drawing.SystemColors.ControlText;
this.cmdEncrypt.Cursor = System.Windows.Forms.Cursors.Default;
this.cmdEncrypt.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.cmdEncrypt.TabStop = true;
this.cmdEncrypt.Name = "cmdEncrypt";
this.Label2.Text = "Salt";
this.Label2.Size = new System.Drawing.Size(105, 25);
this.Label2.Location = new System.Drawing.Point(152, 56);
this.Label2.TabIndex = 4;
this.Label2.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.Label2.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.Label2.BackColor = System.Drawing.SystemColors.Control;
this.Label2.Enabled = true;
this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label2.UseMnemonic = true;
this.Label2.Visible = true;
this.Label2.AutoSize = false;
this.Label2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.Label2.Name = "Label2";
this.Label1.Text = "Date";
this.Label1.Size = new System.Drawing.Size(97, 25);
this.Label1.Location = new System.Drawing.Point(152, 16);
this.Label1.TabIndex = 3;
this.Label1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.Label1.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.Label1.BackColor = System.Drawing.SystemColors.Control;
this.Label1.Enabled = true;
this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label1.UseMnemonic = true;
this.Label1.Visible = true;
this.Label1.AutoSize = false;
this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.Label1.Name = "Label1";
this.Controls.Add(cmdDecrypt);
this.Controls.Add(txtPath);
this.Controls.Add(txtSalt);
this.Controls.Add(txtText);
this.Controls.Add(cmdEncrypt);
this.Controls.Add(Label2);
this.Controls.Add(Label1);
cmdDecrypt.Click += new System.EventHandler(cmdDecrypt_Click);
cmdEncrypt.Click += new System.EventHandler(cmdEncrypt_Click);
base.Load += new System.EventHandler(Form1_Load);
}
#endregion
#region Upgrade Support
private static Form1 m_vb6FormDefInstance;
private static bool m_InitializingDefInstance;
public static Form1 DefInstance
{
get
{
if (m_vb6FormDefInstance == null || m_vb6FormDefInstance.IsDisposed)
{
m_InitializingDefInstance = true;
m_vb6FormDefInstance = new Form1();
m_InitializingDefInstance = false;
}
return m_vb6FormDefInstance;
}
set
{
m_vb6FormDefInstance = value;
}
}
#endregion
private void cmdDecrypt_Click(object eventSender, System.EventArgs eventArgs)
{
string Saved = null;
if (Microsoft.VisualBasic.FileSystem.Dir(txtPath.Text, Microsoft.VisualBasic.FileAttribute.Normal).Length > 0)
{
Microsoft.VisualBasic.FileSystem.FileOpen(1, txtPath.Text, Microsoft.VisualBasic.OpenMode.Binary);
Saved = string.Empty.PadLeft(Microsoft.VisualBasic.FileSystem.LOF(1));
Microsoft.VisualBasic.FileSystem.FileGet(1, Saved, 1); Microsoft.VisualBasic.FileSystem.FileClose(1);
System.Diagnostics.Debug.WriteLine(Crypt(Saved, txtSalt.Text));
MessageBox.Show(Crypt(Saved, txtSalt.Text));
}
else
{
MessageBox.Show(txtPath.Text + " not found!");
return;
}
}
private void cmdEncrypt_Click(object eventSender, System.EventArgs eventArgs)
{
if (Microsoft.VisualBasic.FileSystem.Dir(txtPath.Text, Microsoft.VisualBasic.FileAttribute.Normal).Length > 0)
{
Microsoft.VisualBasic.FileSystem.Kill(txtPath.Text);
}
Microsoft.VisualBasic.FileSystem.FileOpen(1, txtPath.Text, Microsoft.VisualBasic.OpenMode.Binary);
Microsoft.VisualBasic.FileSystem.FilePut(1, Crypt(txtText.Text, txtSalt.Text), -1);
Microsoft.VisualBasic.FileSystem.FileClose(1);
MessageBox.Show("Saved!");
}
private void Command1_Click()
{
string TT = null;
string Salt = null;
TT = "this is the one I love and hate";
Salt = "WetRivrRat";
string Encrypted = null;
Encrypted = Crypt(TT, Salt);
System.Diagnostics.Debug.WriteLine(Encrypted);
System.Diagnostics.Debug.WriteLine(Crypt(Encrypted, Salt));
}
private string Crypt(string Text_Renamed, string Key)
{
int KeyLen = 0;
int i = 0;
int KeyPtr = 0;
short KeyChr = 0;
short TextChr = 0;
KeyLen = Key.Length;
int tempFor1 = Text_Renamed.Length;
for (i = 1; i <= tempFor1; i++)
{
TextChr = System.Convert.ToInt32(Text_Renamed[0]);
KeyChr = System.Convert.ToInt32(Key[0]);
Text_Renamed = Text_Renamed.Remove(i - 1, 1).Insert(i - 1, ((char)(TextChr ^ KeyChr)).ToString());
KeyPtr = ((KeyPtr + 1) % KeyLen);
}
return Text_Renamed;
}
private void Form1_Load(object eventSender, System.EventArgs eventArgs)
{
txtText.Text = System.Convert.ToString(System.DateTime.Now);
txtPath.Text = Support.GetPath() + "\\DateTest.bin";
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
Here's a hint. The include of the VB6 dll will show anyone who looks at your code that you've spent ages importing VB6 code instead of writing C# code. I would throw ALL of this away.
wetrivrrat wrote:
using Microsoft.VisualBasic.Compatibility.VB6;
Delete this for starters. Even if you use a VB->C# converter, delete this and fix the lines that break. Otherwise, it's not C# at all.
wetrivrrat wrote:
Microsoft.VisualBasic.FileSystem.Dir(txtPath.Text, Microsoft.VisualBasic.FileAttribute.Normal).Length > 0) { Microsoft.VisualBasic.FileSystem.FileOpen(1, txtPath.Text, Microsoft.VisualBasic.OpenMode.Binary); Saved = string.Empty.PadLeft(Microsoft.VisualBasic.FileSystem.LOF(1));
C# provides much nicer ways to do all of these things. This code is a disaster.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
yes, i know,
but
his idea was the best i had, and at this point i still don't compute how to make anything else better.....
i've got 6 books sitting here on the floor all dealing with hashing, salting or MD5, and i'm not sure where to start, my brain is fried, and all i want is to NOT see a string value of the date in my isoStore, his code made that happen even if it was vb, ....................................
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
Then take the function I gave you above, it does in fact convert the string and convert it back the next time through.
FYI, it's incredibly simple. It takes the string and a key, and it walks the key over and over, so if the string is 'this is it' and the key is 'fish', it's worked out like this:
this is it
saltsaltsa
The two values are XORed, which means that all bits are reversed. 10010011 becomes 01100011. Because all bits are reversed, doing it again restores the original value.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
You might find the following, produced with our Instant C# VB.NET to C# converter, more useful (you can download a free limited version at www.instantvb.com):
//TODO: INSTANT C# TODO TASK: Insert the following converted event handlers at the end of the 'InitializeComponent' method for forms or into a constructor for other classes:
base.Load += new System.EventHandler(Form1_Load);
private string Crypt(string Text_Renamed, string Key)
{
int KeyLen = 0;
int i = 0;
int KeyPtr = 0;
short KeyChr = 0;
short TextChr = 0;
KeyLen = Key.Length;
//INSTANT C# NOTE: The ending condition of VB 'For' loops is tested only on entry to the loop. Instant C# has created a temporary variable in order to use the initial value of Len(Text_Renamed) for every iteration:
int tempFor1 = Text_Renamed.Length;
for (i = 1; i <= tempFor1; i++)
{
TextChr = System.Convert.ToInt32(Text_Renamed[0]);
KeyChr = System.Convert.ToInt32(Key[0]);
Text_Renamed = Text_Renamed.Remove(i - 1, 1).Insert(i - 1, ((char)(TextChr ^ KeyChr)).ToString());
KeyPtr = ((KeyPtr + 1) % KeyLen);
}
return Text_Renamed;
}
private void Form1_Load(object eventSender, System.EventArgs eventArgs)
{
txtText.Text = System.Convert.ToString(System.DateTime.Now);
txtPath.Text = Support.GetPath + "\\DateTest.bin";
}
David Anton
Tangible Software Solutions
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
|
|
|
|
|
David Anton wrote:
for (i = 1; i <= tempFor1; i++)
This doesn't work. It looks like VB is 1 indexed, changing it to 0 and < tempFor1 fixes this.
David Anton wrote:
TextChr = System.Convert.ToInt32(Text_Renamed[0]);
KeyChr = System.Convert.ToInt32(Key[0]);
Gosh - this is a mess. Perhaps the source code being converted is the problem ? I've just done it by hand, and this needs to be i-1 with the indexes your using.
David Anton wrote:
Text_Renamed = Text_Renamed.Remove(i - 1, 1).Insert(i - 1, ((char)(TextChr ^ KeyChr)).ToString());
Yeah, this will work, it's correctly using i-1. I guess that's an example of how a conversion can work but not produce the worlds prettiest code.
I just did this by hand, and knowing how excellent these conversion tools are, I'd say at a guess his original source is a disaster, I would be surprised if your tool introduced these problems.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thanks for noticing that - there was a bug in the conversion of the combination of Asc and Mid. It's fixed now in the latest build of Instant C#.
The for loop conversion is correct though - I'm not saying that the original code worked or not - I'm just speaking from a C# equivalency viewpoint. You are probably right that the loop itself needs to be different for the code to work (in VB or C#).
David Anton
Tangible Software Solutions
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
|
|
|
|
|
David Anton wrote:
You are probably right that the loop itself needs to be different for the code to work (in VB or C#).
Yeah - I thought that would be the case, I didn't think your product would have a bug that changed array indices
I'm glad I helped you find a bug though. I can only imagine how complex a product like yours would be to get absolutely right.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
hello everyone.
I have a form that contain a panel i had design. and i have a button bellow this panel . Can i print this panel only ?
quangmogulasiaUNS
|
|
|
|
|
Hello.. i have a problem.. i have an application, and i want it to do smth when a key-combination is pressed... the problem is that i want it to do that even when it doesn't have focus.. i need smth like a "global hotkey", or i need to set up an event listener, that can listen for events even when my application doesn't have focus.. can anybody help? pls...
|
|
|
|
|
Ok in your app import the User32.dll function RegisterHotKey
[DllImport("user32.dll", SetLastError=true)]<br />
public static extern bool RegisterHotKey( IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
Keys vk
);
Whilst Your at it - may as well import the Unregister one too
[DllImport("user32.dll", SetLastError=true)]<br />
public static extern bool UnregisterHotKey( IntPtr hWnd,
int id
);
In your MainForm constructor - register the hot key
RegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L);
In this case it's ctrl, shift and L - anything you want really.
Then override the WndProc for the main form and handle the event
protected override void WndProc(ref Message m)<br />
{<br />
const int WM_HOTKEY = 0x0312;<br />
<br />
switch(m.Msg)<br />
{<br />
case WM_HOTKEY:<br />
{<br />
break;<br />
}<br />
}<br />
<br />
base.WndProc (ref m);<br />
}
Remember to also unregister the hot key in the Form.OnClosed method
private void Form1_Closed(object sender, System.EventArgs e)<br />
{ <br />
UnregisterHotKey(Handle, 100);<br />
}
That should do what you want.
Mark.
|
|
|
|
|
Hey . Thanks a lot
|
|
|
|
|
still, i have a problem.... "KeyModifiers fsModifiers" the program throws an error when i attempt to build it..
" The type or namespace name 'KeyModifiers' could not be found (are you missing a using directive or an assembly reference?)" it's a common error.. pls help, the rest of the code is perfect
|
|
|
|
|