Home > Software engineering >  How to implement a simple letter and subtract shift?
How to implement a simple letter and subtract shift?

Time:09-28

Implement a simple letter to add and subtract shift, how has the highest efficiency? How to add and subtract Numbers, easy letters? Do not know to have a ready-made such a function? :
Objective: to simple shift of characters in a string encryption to decrypt,

Encryption is simply in the string into a, b, c for b and d to c, 1 to 0, 2 for 1...
That all minus 1 (letter of alphabet order check out 1 a). Then some special characters of special processing (a change -- - to $0 for Z, : change.)

Rule (alphanumeric one-to-one correspondence relationship:)
A - bcdefghijklmnopqrstuvw. Xyz: 1234567890
Encrypted one-to-one correspondence:
- $abcdefghijklmnopqrstuv9wxy. 012345678 z

For example,
86 baidu.com
Need to be encrypted
75 - a - hct9bnl

Decryption is, in turn, do not know to have a ready-made encryption function like this?

Thank you very much!


CodePudding user response:

As far as I know there is no ready-made, read the string - & gt; Identification of the first character - & gt; Coding processing (additive can reduce or replace number characters in pairs, look at your real demand) - & gt; Until the last second characters,

CodePudding user response:

Can be simple to add and subtract, but outside of alphanumeric symbols, not according to your idea change, but the increase or decrease in ASCII order,
 Const key As Byte=1 

Private Function Encode (ByVal strSrc As String) As String
Dim As Long I, TMP As String, b As Byte

For I=1 To Len (strSrc)
B=Asc (Mid (strSrc, I, 1))
If (b & lt; 32) Or (b & gt; 127) Then the Exit Function

TMP=TMP & amp; CRH (Asc (Mid (strSrc, I, 1)) + 127 - key) Mod 127)
Next I

Encode=TMP
End the Function

Private Function Decode (ByVal strSrc As String) As String
Dim As Long I, TMP As String, b As Byte

For I=1 To Len (strSrc)
B=Asc (Mid (strSrc, I, 1))
If b & lt; 32 Or b & gt; 127 Then the Exit Function

TMP=TMP & amp; CRH (Asc (Mid (strSrc, I, 1)) + 127 + key) Mod 127)
Next I

Decode=TMP
End the Function

Private Sub Text1_Change ()
Text2. Text=Encode (Text1. Text)
Text3. Text=Decode (Text1. Text)
End Sub

CodePudding user response:

By the look-up table method, of course, also can accurately achieve your design replacement:
 Const key As Byte=1 
Const strList As String="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. : $- abcdefghijklmnopqrstuvwxyz"
Private Function Encode (ByVal strSrc As String) As String
Dim As Long I, TMP As String, b As Byte

For I=1 To Len (strSrc)
B=InStr (strList, Mid (strSrc, I, 1))
If b=0 Then the Exit Function

B=(b + Len (strList) - key) Mod Len (strList)
If b=0 Then b=Len (strList)
TMP=TMP & amp; Mid (strList, b, 1)
Next I

Encode=TMP
End the Function

Private Function Decode (ByVal strSrc As String) As String
Dim As Long I, TMP As String, b As Byte

For I=1 To Len (strSrc)
B=InStr (strList, Mid (strSrc, I, 1))
If b=0 Then the Exit Function

B=(b + Len (strList) + key) Mod Len (strList)
If b=0 Then b=Len (strList)
TMP=TMP & amp; Mid (strList, b, 1)
Next I

Decode=TMP
End the Function

Private Sub Text1_Change ()
Text2. Text=Encode (Text1. Text)
Text3. Text=Decode (Text1. Text)
End Sub

Look-up table can only replace the characters in the table,
But look-up table has a potential advantages, that is, if the character is a random sequence in the table, the difficulty of the crack is much larger, equal to use the password table,
  • Related