Question

The Asc function in VB takes a character and return an integer that represents that character. It does not matter what the integer representing the character actually is, but what matters is this: Asc...

The Asc function in VB takes a character and return an integer that represents that character. It does not matter what the integer representing the character actually is, but what matters is this: Asc("a") is 1 less than Asc("b"), so that: x=Asc("a") thisLetter = x+1 # thisLetter is the Asc("b") This is a powerful fact that is used in encryption techniques, so data transferred over the web is 'deciphered so it is unreadable to others. To decipher data, we take a string and change it into another string by adding a constant to its asc (short for ascii) value. To cipher and decipher data, we need two functions: one to take a string, and change it to something else. For this we need to use the asc function, and add the 'key' to result in a new string. For example, say x is one letter to be ciphered, and the key is 3. We can use: Newx=asc(x)+3 Newx will be an integer. To find out what letter that integer represents you can use the chr function as in: chr(x). Write a function cipher that takes a string and a key (integer). The function ciphers the string into another string and returns the new string. Note that when we reach 'z', and we want to add the key, we must 'roll' into the alphabet one more time, hence asc(''z''+3 should give us asc(''c''). Write another function to decipher (do the opposite of the previous function), given a string and a key, it returns the deciphered string. In the form, write an event handler for a button to get a string, and an integer as input, then call the cipher function and show its output. Then call the decipher function and display its output. The decipher output should match the original string.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The below program would help to provide an idea on how to solve the stated problem

================================VB code=====================================

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions
Imports System.Integer

Namespace StringCipherDecipher
Public Module Program
  
' The below function will check if the string has valid characters
Function CheckCharacters(ByVal WordToCheck As String)
Dim i As Integer
For i = 0 To WordToCheck.Length - 1
If Char.IsLetter(WordToCheck.Chars(i)) Then
Return True
End If
Next
Return False
End Function

'Below function will convert each character of the string to a new character adding the key to its Asc value

Function Cipher(ByVal strWord As String, ByVal strKey As Integer)
   Dim Newx As Integer
   Dim NewChar As String
Dim i As Integer

   NewChar=""

   For i = 0 To strWord.Length - 1
If strWord.Chars(i) = "z" Then
   Newx = Asc( "a")+strKey
Else
   Newx = Asc(strWord.Chars(i))+strKey

End If
   NewChar = NewChar + Chr(Newx)
Next
return NewChar
End Function

'Below function will convert each character of the string to a new character subtracting the key to its Asc value, opposite of cipher function

Function Decipher(ByVal strWord As String, ByVal strKey As Integer)
   Dim Newx As Integer
   Dim NewChar As String
Dim i As Integer

   NewChar=""

   For i = 0 To strWord.Length - 1   

If strWord.Chars(i) = "a" Then
   Newx = Asc( "z") - strKey
Else
   Newx = Asc(strWord.Chars(i)) - strKey

End If
   NewChar = NewChar + Chr(Newx)
Next
return NewChar

End Function

'Below function is the event handler that will trigger the input and cipher and decipher of entered string

Private Sub btnCipherDecipher_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnCipherDecipher.Click
Dim strWord As String
   Dim strKey As Integer

Dim CipherString As String

Dim DecipherString As String

strWord = InputBox("Enter a string to cipher", "Cipher String") 'Prompting to enter cipher string
strKey = InputBox("Enter the keyword to cipher string", "Cipher String Key") 'Prompting to enter cipher key

   If CheckCharacters(strWord) And IsNumeric(strKey) Then

  CipherString = Cipher(strWord, strKey) 'calling Cipher function passing the entered string with key
MsgBox("Cipher string : " + CipherString )

DecipherString = Decipher(CipherString , strKey) 'calling Decipher function passing the cipher string with key
   MsgBox("Decipher string : " + DecipherString )
Else
MsgBox("You provided an invalid value", _
MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, _
"Cipher string")
End If

End Sub

================================End=========================================================

Add a comment
Know the answer?
Add Answer to:
The Asc function in VB takes a character and return an integer that represents that character. It does not matter what the integer representing the character actually is, but what matters is this: Asc...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9.

     4.18 LAB: Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Exx If the input is: 1995 the output is: yes Ex If the input is: 42,000 1995! the output is no Hint: Use a loop and the Character isDigitO function. 418.1: LAB: Checker for integer string

  • Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing...

    Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing two functions that will output the number of vowels and consonants in a user inputted string. We’ll be using functions from the cstring library, so be sure to include it! We’ll be calling those functions method_one and method_two. To start off, declare a character array (with no size) called vowels and initialize it with “aeiouyAEIOUY” in our main function. Declare a character array with...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...

    JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the input message so that it’s easier to work with. Write a method called normalizeText which does the following: Removes all the spaces from your text Remove any punctuation (. , : ; ’ ” ! ? ( ) ) Turn all lower-case letters into upper-case letters Return the result. The call normalizeText(“This is some \“really\” great. (Text)!?”) should return “THISISSOMEREALLYGREATTEXT” Part 2 - Obfuscation...

  • In a main function, use a menu system to exercise the usage of all of these...

    In a main function, use a menu system to exercise the usage of all of these functions. The menu should be in a continuous loop to allow the user to test all functions. 1. Write a recursive function for the following function that does not use recursion. So, we want to implement the same function using recursion. void what(int x) { while (x > 0) { cout<<”say what\n”; x--; } //x > 0 }//what 2. Write a recursive function that...

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers...

    Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers (the "backwards" cipher, the Rail Fence cipher, and the Column Transposition cipher) where the ciphertext is created via an altered presentation of the plaintext. The algorithm for each is detailed in the function descriptions in this section. (13 points) def backwards_cipher(plaintext, key): • Parameter(s): plaintext ----- a string; the message to be encrypted key ----- an integer; the number to control this cipher •...

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT