form中有2个Command,2个textbox;
如何把textbox1里的字符简单加密后写到文件中,使文件打开后显示乱码,然后可以把被加密的字符解密后,读到textbox2中?不知道是否真的简单,我是凭想象。

解决方案 »

  1.   

    Public Function QpDecode(inString As String) As String
    Dim myB As Byte
    Dim myByte1 As Byte, myByte2 As Byte
    Dim convStr() As Byte
    Dim mOutByte As Byte
    Dim FinishPercent As Long
    Dim TotalB, k As Long
    Dim tmpByte As ByteconvStr = StrConv(inString, vbFromUnicode)TotalB = UBound(convStr)
    For k = 0 To TotalB
       myB = convStr(k)
       If myB = Asc("=") Then
          k = k + 1
          myByte1 = convStr(k)
          If myByte1 = &HA Then
          Else
             k = k + 1
             myByte2 = convStr(k)
             Call DecodeByte(myByte1, myByte2, mOutByte)
             If mOutByte >= 127 Then
                If tmpByte <> 0 Then
                   QpDecode = QpDecode & Chr(Val("&H" & Hex(tmpByte) & Hex(mOutByte)))
                   tmpByte = 0
                Else
                   tmpByte = mOutByte
                End If
             Else
                QpDecode = QpDecode & Chr(mOutByte)
                tmpByte = 0
             End If
          End If
        Else
            mOutByte = myB
            QpDecode = QpDecode & Chr(mOutByte)
        End If
    Next
    End Function
    Private Sub DecodeByte(mInByte1 As Byte, mInByte2 As Byte, mOutByte As Byte)
    Dim tbyte1 As Integer, tbyte2 As Integer
    If mInByte1 > Asc("9") Then
        tbyte1 = mInByte1 - Asc("A") + 10
    Else
        tbyte1 = mInByte1 - Asc("0")
    End If
    If mInByte2 > Asc("9") Then
        tbyte2 = mInByte2 - Asc("A") + 10
    Else
        tbyte2 = mInByte2 - Asc("0")
    End If
    mOutByte = tbyte1 * 16 + tbyte2
    End SubPrivate Sub EncodeByte(mInByte As Byte, mOutStr As String)
    If (mInByte >= 33 And mInByte <= 60) Or (mInByte >= 62 And mInByte <= 126) Then
        mOutStr = Chr(mInByte)
    Else
        If mInByte <= &HF Then
            mOutStr = "=0" & Hex(mInByte)
        Else
            mOutStr = "=" & Hex(mInByte)
        End If
    End If
    End SubPublic Function QpEncode(inString As String) As String
    Dim myB As Byte
    Dim convByte() As Byte
    Dim mOutStr As String
    Dim FinishPercent As Long
    Dim TotalB, k As LongconvByte = StrConv(inString, vbFromUnicode)TotalB = UBound(convByte)
    For k = 0 To TotalB
        myB = convByte(k)
        EncodeByte myB, mOutStr
        QpEncode = QpEncode & mOutStr
    Next
    End FunctionPrivate Sub Command1_Click()  '加密
    Text2 = QpEncode("加密")
    End SubPrivate Sub Command2_Click()  '解密
    Text1 = QpDecode(QpEncode("解密"))
    End Sub
      

  2.   

    Option Explicit' Encipher the text using the pasword.
    Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String)
    Const MIN_ASC = 32  ' Space.
    Const MAX_ASC = 126 ' ~.
    Const NUM_ASC = MAX_ASC - MIN_ASC + 1Dim offset As Long
    Dim str_len As Integer
    Dim i As Integer
    Dim ch As Integer    ' Initialize the random number generator.
        offset = NumericPassword(password)
        Rnd -1
        Randomize offset    ' Encipher the string.
        str_len = Len(from_text)
        For i = 1 To str_len
            ch = Asc(Mid$(from_text, i, 1))
            If ch >= MIN_ASC And ch <= MAX_ASC Then
                ch = ch - MIN_ASC
                offset = Int((NUM_ASC + 1) * Rnd)
                ch = ((ch + offset) Mod NUM_ASC)
                ch = ch + MIN_ASC
                to_text = to_text & Chr$(ch)
            End If
        Next i
    End Sub
    ' Encipher the text using the pasword.
    Private Sub Decipher(ByVal password As String, ByVal from_text As String, to_text As String)
    Const MIN_ASC = 32  ' Space.
    Const MAX_ASC = 126 ' ~.
    Const NUM_ASC = MAX_ASC - MIN_ASC + 1Dim offset As Long
    Dim str_len As Integer
    Dim i As Integer
    Dim ch As Integer    ' Initialize the random number generator.
        offset = NumericPassword(password)
        Rnd -1
        Randomize offset    ' Encipher the string.
        str_len = Len(from_text)
        For i = 1 To str_len
            ch = Asc(Mid$(from_text, i, 1))
            If ch >= MIN_ASC And ch <= MAX_ASC Then
                ch = ch - MIN_ASC
                offset = Int((NUM_ASC + 1) * Rnd)
                ch = ((ch - offset) Mod NUM_ASC)
                If ch < 0 Then ch = ch + NUM_ASC
                ch = ch + MIN_ASC
                to_text = to_text & Chr$(ch)
            End If
        Next i
    End Sub
    ' Translate a password into an offset value.
    Private Function NumericPassword(ByVal password As String) As Long
    Dim value As Long
    Dim ch As Long
    Dim shift1 As Long
    Dim shift2 As Long
    Dim i As Integer
    Dim str_len As Integer    str_len = Len(password)
        For i = 1 To str_len
            ' Add the next letter.
            ch = Asc(Mid$(password, i, 1))
            value = value Xor (ch * 2 ^ shift1)
            value = value Xor (ch * 2 ^ shift2)        ' Change the shift offsets.
            shift1 = (shift1 + 7) Mod 19
            shift2 = (shift2 + 13) Mod 23
        Next i
        NumericPassword = value
    End FunctionPrivate Sub cmdCipher_Click()
    Dim cipher_text As String    Cipher txtPassword.Text, txtPlain.Text, cipher_text
        txtCipher.Text = cipher_text
        txtPlain.Text = ""
    End SubPrivate Sub cmdDecipher_Click()
    Dim plain_text As String    Decipher txtPassword.Text, txtCipher.Text, plain_text
        txtPlain.Text = plain_text
        txtCipher.Text = ""
    End SubPrivate Sub txtPassword_Change()
        '如果password不为空,则可进行加密和解密
        If Len(txtPassword.Text) > 0 Then
            cmdCipher.Enabled = True
            cmdDecipher.Enabled = True
        Else
            cmdCipher.Enabled = False
            cmdDecipher.Enabled = False
        End If
    End Sub
    不是简单的加密解密,请参考!主要是我也没读懂,请指教!
      

  3.   

    按byte读取,查看byte值,byte的二进制形式是xxxxxxxx,随你改哪一位,这样文件内容就乱套了。例如:10000000 --> 00000000(减去128)  00000000 --> 10000000(加上128)。解密跟加密一样。