加密解密字符串

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 + 1

Dim 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 + 1

Dim 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 Function

Private Sub cmdCipher_Click()
Dim cipher_text As String

    Cipher txtPassword.Text, txtPlain.Text, cipher_text
    txtCipher.Text = cipher_text
End Sub

Private Sub cmdDecipher_Click()
Dim plain_text As String

    Decipher txtPassword.Text, txtCipher.Text, plain_text
    txtPlain.Text = plain_text
End Sub


Private Sub Form_Load()

End Sub

Private Sub txtPassword_Change()
    If Len(txtPassword.Text) > 0 Then
        cmdCipher.Enabled = True
        cmdDecipher.Enabled = True
    Else
        cmdCipher.Enabled = False
        cmdDecipher.Enabled = False
    End If
End Sub


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB源码--自定义数字&字符串加密工具 --VB源码 加密 解密 字符串 源码 *************以下为窗口及控件代码************ Private Sub Command1_Click() Label3.Caption = cipher(Text1.Text, Text2.Text) Label8.Caption = "加密完成!" End Sub Private Sub Command2_Click() Label4.Caption = Decipher(Text4.Text, Text3.Text) Label9.Caption = "解密完成!" End Sub Private Sub Label3_Click() Text3.Text = Label3.Caption End Sub Private Sub Label3_DblClick() Clipboard.SetText Label3.Caption Label8.Caption = "复制成功!" End Sub Private Sub Label4_DblClick() Clipboard.SetText Label4.Caption Label9.Caption = "复制成功!" End Sub ************以下为模块代码*************** ' Encipher the text using the pasword.加密 Public Function cipher(ByVal password As String, ByVal from_text As String) Const MIN_ASC = 32 ' Space. Const MAX_ASC = 126 ' ~. Const NUM_ASC = MAX_ASC - MIN_ASC + 1 Dim offset As Long Dim str_len As Integer Dim i As Integer Dim ch As Integer Dim to_text As String ' 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 cipher = to_text End Function ' Encipher the text using the pasword.解密 Public Function Decipher(ByVal password As String, ByVal from_text As String) Const MIN_ASC = 32 ' Space. Const MAX_ASC = 126 ' ~. Const NUM_ASC = MAX_ASC - MIN_ASC + 1 Dim offset As Long Dim str_len As Integer Dim i As Integer Dim ch As Integer Dim to_text As String ' 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 ***************** 省略部分代码(内详) ***************** If ch < 0 Then ch = ch + NUM_ASC ch = ch + MIN_ASC to_text = to_text & Chr$(ch) End If Next i Decipher = to_text End Function ' 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)) ***************** 省略部分代码(内详) ***************** Next i NumericPassword = value End Function
里提供了三种Python中常用的加密解密字符串的方法:DES、RSA和AES。 1. DES加密解密: 使用Crypto.Cipher库中的DES模块,需要注意的是,密钥必须为8位或16位bytes类型,加密文本必须为8的倍数。具体代码实现可以参考引用中的DesUtil类。 2. RSA加密解密: 使用rsa库进行加密解密,需要生成公钥和私钥,具体代码实现可以参考引用中的rsacrypt类。 3. AES加密解密: 使用pycryptodome库中的AES模块,需要注意的是,密钥必须为16、24或32位bytes类型,加密文本可以为任意长度。具体代码实现可以参考以下示例: ``` from Crypto.Cipher import AES import base64 class AesUtil(): """ AES加密解密 """ key = b'1234567890123456' # 密钥 16、24或32位bytes类型 iv = b'1234567890123456' # 初始向量 16位bytes类型 def __init__(self): self.aes = AES.new(self.key, AES.MODE_CBC, self.iv) # 创建一个AES实例 def pad(self, text): """ 加密函数,如果text不是16的倍数,那就补足为16的倍数 :param text: :return: """ while len(text) % 16 != 0: text += b' ' return text def encrypt(self, text): padded_text = self.pad(text) encrypted_text = self.aes.encrypt(padded_text) # 把加密后的字符串转化为base64编码的字符串 return base64.b64encode(encrypted_text).decode() def decrypt(self, text): # 先把base64编码的字符串转化为bytes类型 encrypted_text = base64.b64decode(text.encode()) decrypted_text = self.aes.decrypt(encrypted_text).rstrip(b' ') return decrypted_text.decode() aes = AesUtil() enc = aes.encrypt(b"我是一个兵!") print("加密后:%s" % enc) dec = aes.decrypt(enc) print("解密后:%s" % dec) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值