Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB
Raw RSA Encryption with VB Raw RSA Encryption with VB
Raw RSA Encryption with VB
Go Back  Xtreme Visual Basic Talk > > > Raw RSA Encryption with VB


Reply
 
Thread Tools Display Modes
  #1  
Old 11-15-2011, 03:18 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default Raw RSA Encryption with VB


I am attempting to create a program that can do raw RSA encryption (performing the encryption without using 'Encrypt' and 'Decrypt') of a user's message that can contain a-z, A-Z, 0-9, and [,.;:'"()]. I've attached a screen shot of the application form.

Basically, the user will pick from a list of prime numbers from the first box and input them into p and q. They will type their message in the designated textbox and then hit <encrypt> which will encrypt using p and q and then show the encrypted message below. I'll leave out the Key box since that deals with the math behind the encryption.

The part I am stuck on right now is assigning a number to each one of the characters so that it can be encrypted. These numbers need to remain the same so that the message can be decrypted as well.

Any help would be very much appreciated.
Attached Images
File Type: png Capture.PNG (11.7 KB, 6 views)
Reply With Quote
  #2  
Old 11-15-2011, 03:55 PM
jwshepherd jwshepherd is offline
Freshman
 
Join Date: May 2009
Location: West Texas
Posts: 45
Default

a little code that you are having trouble with would be helpful.
Reply With Quote
  #3  
Old 11-15-2011, 04:00 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

I am not even sure how to start. I need each character to equal a unique number that I can use in the equation.
Reply With Quote
  #4  
Old 11-15-2011, 04:36 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

Dim ArrayLo("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z") As Array
Dim ArrayUp("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z") As Array
Dim ArrayCha(",", ".", ";", ":", "!", "@", "#", "$", "%", "^", "&", "*", "-", "+", "=", "(", ")") As Array
Dim ArrayNum("1", "2", "3", "4", "5", "6", "7", "8", "9", "0") As Array

This is as far as I've gotten, and I don't know if there is anyway to put this all in one array and if there is a way to assign each character in the array a number that is unique and permanent.
Reply With Quote
  #5  
Old 11-15-2011, 04:39 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

I'm not entirely sure what you are after here are you trying to use an RSA implementation to encrypt the data or are you trying to implement the RSA algorithm yourself?

Could you explain what you mean by
Code:
I need each character to equal a unique number that I can use in the equation.
as it isn't particularly clear.

The code
Code:
Dim ArrayLo("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z") As Array
Dim ArrayUp("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z") As Array
Dim ArrayCha(",", ".", ";", ":", "!", "@", "#", "$", "%", "^", "&", "*", "-", "+", "=", "(", ")") As Array
Dim ArrayNum("1", "2", "3", "4", "5", "6", "7", "8", "9", "0") As Array
is just declaring 4 arrays (in a fairly unusual way admittedly) if you wanted it to be one array you could just declare one array and list all the characters.

Not sure what you mean by
Quote:
if there is a way to assign each character in the array a number that is unique and permanent
the index of the character in the array would be unique for the array, also every one of those characters has an associated ASCII code (or Unicode codepoint if we are using unicode strings) or are you after some other form of unique identifier?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags

Last edited by PlausiblyDamp; 11-15-2011 at 04:44 PM. Reason: Updated to include more info
Reply With Quote
  #6  
Old 11-16-2011, 10:03 AM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

I get a sort of character limit error if I try to place them in a single array. And it has been a while since I have used VB if the array is awkward, this also means the part about ASCII etc is over my head as well. I am trying to implement the algorithm myself. So for example, if the algorithm to encrypt the message was simply (q*p) * t = m where:

p and q = the prime numbers the user selects from the list in the program
t = the number that is assigned to the character in the message they input to encrypt
m = the encrypted character

The encryption of the message "A" with p = 4 and q = 3 would go as follows:
1. The character (capitalized letter A) is matched with the number 5 in the program (unique and permanent number assigned to that character)
2. The program performs the math: (4*3) * 5 = 60
3. The encrypted message is displayed as "60"

The assignment of a number to the character needs to be 'permanent' because I don't want the program to take the message and THEN assign numbers to the characters (EX hey = 123 but cat = 123 too). I want the numbers to already be assigned so h is always 1 and c is always 5 for example. This way it will be possible to decrypt the message using another program.

I hope this helps. Thank you for the help thus far.
Reply With Quote
  #7  
Old 11-16-2011, 03:32 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

ASCII is a very common for of character encoding (http://en.wikipedia.org/wiki/ASCII) which maps characters to a byte (http://www.asciitable.com/) - this means that if you are using an ascii character set then the letter to number mapping is already taken care of.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #8  
Old 11-16-2011, 04:04 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

Thank you, so how would I have VB convert each character into the ASCII number?

In this code, m is the message, which in this example is simply the number 3, so it is easy to compute. For my program, m will equal 'word' (or anything else the user inputs) or 119 111 114 100 in ASCII. So I need each of the ASCII numbers to be computed in c = (m ^ e) Mod n and separated with a space. So 'word' encrypted would be 4521 3521 4231 5601 for example.

Private Sub Command1_Click()
Dim p, q, n, e, PHI, d, m, c As Long

p = Text1.Text
q = Text2.Text
If (check_prime(p) = False) Then
status.Text = "p is not a prime or is too large, please re-enter"
ElseIf (check_prime(q) = False) Then
status.Text = "q is not a prime or is too large, please re-enter"
Else
n = p * q
Text3.Text = n

PHI = (p - 1) * (q - 1)
e = getE((PHI))
d = getD((e), (PHI))
Text4.Text = PHI
Text5.Text = d
Text6.Text = e
m = Text7.Text

c = (m ^ e) Mod n
Text8.Text = c
m = decrypt(c, n, d)
Text9.Text = m
End If
End Sub

Again thank you very much for the help, I can get the code to work a single number, but not multiple letters.
Reply With Quote
  #9  
Old 11-16-2011, 04:20 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you have a string then something like
Code:
  Dim message As String = "Hello World"

        Dim bytes(message.Length - 1) As Byte
        For i As Integer = 0 To message.Length - 1
            bytes(i) = Convert.ToByte(message(i))
        Next
should do the trick.

If you are 2008 or later then you could use Linq and just use
Code:
Dim message As String = "Hello World"

Dim bytes() = message.Select(Function(c) Convert.ToByte(c)).ToArray
I would also suggest you add Option Strict On to the top of your source file as relying on VB's automatic data type conversions can be a source of bugs that are very difficult to track down.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #10  
Old 11-16-2011, 05:52 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

So hopefully this would work:

Code:
Dim message as String
message = userinput.text

But how would I make it so it encrypts each of those numbers (the ASCII for the characters) individually and then separates each encrypted character in a textbox(EX. 2119 3111 2114 4100)?

Code:
Dim bytes(message.Length - 1) As Byte
        For i As Integer = 0 To message.Length - 1
            bytes(i) = Convert.ToByte(message(i))
            c = ((bytes(i)) ^ e) Mod n
        Next
And then something else to display each encrypted character. I'm jumping between a few projects so forgive me if I'm coming off as only semi-coherent.
Reply With Quote
  #11  
Old 11-16-2011, 07:13 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

The code you posted loops over the string, converts each character to a byte and then performs your encryption on this byte. You would then just need to do something with the variable c to have it displayed, you could simply call .ToString on c and append the result to a textbox.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #12  
Old 11-21-2011, 04:15 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

Thank you very much. One last thing I am having trouble with is I need to be able to decrypt the message, and I'm not sure how to perform backwards. To convert the ascii back into plaintext.

For i As Integer = 0 To message.Length - 1
bytes(i) = Convert.ToByte(message(i))
c = ((bytes(i)) ^ e) Mod n
Next
Reply With Quote
  #13  
Old 11-21-2011, 05:29 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

According to http://en.wikipedia.org/wiki/RSA_(algorithm)#Decryption the decryption should be m = c ^ d (mod n). I would imagine wherever you got your original encryption algorithm from would also have had the decryption.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #14  
Old 11-22-2011, 01:00 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

I understand what formulas I need to use, it is the coding that is giving me the problems.
Here are the last thing I need to get done and am having trouble with:

Encryption:
I need the program to take a string of characters (EX Word) and encrypt it, by performing the encryption process to each character individually (which the above code does) BUT I need it to display the encryption properly meaning for the message "Word" it needs to encrypt each character and display it as such: 1234 1234 1234 1234
- I can't seem to get it to display the encryption like this

*Further Explanation:

Current Code:
Code:
For i As Integer = 0 To m.Length - 1
            bytes(i) = Convert.ToByte(m(i))
            c = ((bytes(i)) ^ f) Mod n
            encryptresults.Text = c
        Next
This code will take the encryption of "Word" but only show the last encrypted character, "d". I need it to show all the encrypted letters, separated by a space.

Decryption:
Taking the encrypted message (EX 1234 1234 1234 1234) the code needs to decrypt each character (EX 1234) individually and recreate the word (EX Word).
- I am not sure how to get the program to decrypt each character individually as well as ignore the spaces inbetween and then recreate the word properly (EX Word not W o r d)

Last edited by nsa0513; 11-22-2011 at 01:06 PM.
Reply With Quote
  #15  
Old 11-22-2011, 01:18 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Code:
 encryptresults.Text = c
will replace whatever is in the textbox with the contents of the variable c, you should be appending to the textbox. Also you are doing a direct assignment of a long to a string, you really should have Option Strict On at the top of all your source files as it can prevent a whole load of silly errors.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #16  
Old 11-22-2011, 01:35 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

Thank you, I found the append command. But do you know how I can perform the decryption as stated above?

Code:
Dim bytes(m.Length - 1) As Byte

        For i As Integer = 0 To m.Length - 1
            bytes(i) = Convert.ToByte(m(i))
            c = ((bytes(i)) ^ f) Mod n
            If i = 0 Then
                encryptresults.Text = c
                encryptresults.AppendText(" ")
            Else
                encryptresults.AppendText(c)
                encryptresults.AppendText(" ")
            End If
        Next
Here is what I have for encryption. Which would make "word" = 14 12 9 1
How can I have it decrypt 14 12 9 1 and only decrypt the numbers, not the spaces.

Last edited by nsa0513; 11-22-2011 at 01:45 PM.
Reply With Quote
  #17  
Old 11-22-2011, 03:06 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You would need to reverse the encryption operation's steps.

You could take the contents of the textbox and split it based on the " " character and then for each string in the resulting array convert the string into a byte and then decrypt the byte.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #18  
Old 11-22-2011, 03:17 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

Ah okay thank you. Could you possibly help me with the code for that?
Reply With Quote
  #19  
Old 11-22-2011, 04:44 PM
PlausiblyDamp's Avatar
PlausiblyDampRaw RSA Encryption with VB PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Which bit of the code? You can split a string using the .Split method of a string, you can convert between strings and bytes using the Convert class
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #20  
Old 11-22-2011, 05:09 PM
nsa0513 nsa0513 is offline
Newcomer
 
Join Date: Nov 2011
Posts: 19
Default

That's all very over my head, as was most of this program. Could you please give me some examples at least?

Last edited by nsa0513; 11-22-2011 at 06:59 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB Raw RSA Encryption with VB
Raw RSA Encryption with VB
Raw RSA Encryption with VB
 
Raw RSA Encryption with VB
Raw RSA Encryption with VB
 
-->