locked
Enciphering a string RRS feed

  • Question

  • Hi. I want to have a play with encrypting text. I have a string MyText = "Mary had a little lamb"

    test = MyText(0) = "M"

    I want to turn this into binary, make a few changes and put it back into the string

    Any help with this char to binary and back?

    New to VS and I am still using VS2008. Never got into it originaly.

    Imports System
    Imports System.IO
    Imports System.Text
    Public Class Form1
    
        Private Sub btnEncript_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncript.Click
            Dim MyText As String
            Dim values As String()
            Dim test As String
            MyText = txtText.Text
            values = MyText.Split(","c)
            test = MyText(0)
        End Sub
    End Class

    Monday, July 13, 2020 12:00 PM

All replies

  • What I mean is that "M" gets converted to "01001101"

    I could then swap some zeros and ones to get "01110001"

    And replace it back in the original string

    Hope this makes sense.

    Monday, July 13, 2020 12:25 PM
  • Try a simple encryption:

       Dim MyText As String = "Some text"

       Dim result As String = ""

       For Each c In MyText

          result &= ChrW(AscW(c) Xor 4)

       Next

    To decrypt, use a similar loop.

    Monday, July 13, 2020 5:52 PM
  • Hi Desmond56,

    As Viorel_ suggested, With XOR Encryption, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key.

    XOR Encryption

    Here's a document about encrypting data,and it explains symmetric encryption and asymmetric encryption.

    Encrypting Data

    Hope them could be helpful.

    Best Regards,

    Xingyu Zhao

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 14, 2020 8:27 AM
  • Hi

    Here is some code with a Functions to convert from string to binary and from binary to string. The initial string is in TextBox1,  Binary conversion is in TextBox2 and the conversion back from the binary in TextBox2 is shown in TextBox3 (should match TextBox1)

    Option Strict On
    Option Explicit Off
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim result As String = ConvToBinary(TextBox1.Text)
        TextBox2.AppendText(result)

        ' Here, maniputate the Binary and
        ' continue to make a String out of it
        ' if you didn't break it :)

        TextBox3.AppendText(BinaryToString(result))
      End Sub
      Public Function BinaryToString(ByVal Bin As String) As String
        Dim SB As New Text.StringBuilder
        Dim a() As String = Bin.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
        Dim Bytes(a.Length - 1) As Byte
        Dim c As Integer = 0
        For Each s As String In a
          Bytes(c) = Convert.ToByte(s, 2)
          c += 1
        Next
        Return System.Text.Encoding.ASCII.GetString(Bytes)
      End Function
      Public Function ConvToBinary(ByVal s As String) As String
        Dim SB As New Text.StringBuilder
        For Each c As Byte In System.Text.Encoding.ASCII.GetBytes(s)
          SB.Append(Convert.ToString(c, 2).PadLeft(8, "0"c) & " ")
        Next
        Return SB.ToString
      End Function
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, July 14, 2020 3:45 PM
  • Hi Desmond56,

    How is the question going? Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 17, 2020 7:10 AM