以下是使用golang的des加密方式加密字符串并去掉斜杠的示例代码:

package main

import (
    "crypto/cipher"
    "crypto/des"
    "encoding/base64"
    "fmt"
)

func main() {
    key := []byte("12345678") // des加密的密钥,长度必须为8
    plaintext := "hello world" // 待加密的字符串

    // 创建一个des加密算法实例
    block, err := des.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // 对待加密的字符串进行填充,使其长度为8的倍数
    padding := 8 - len(plaintext)%8
    for i := 0; i < padding; i++ {
        plaintext += string(padding)
    }

    // 对填充后的字符串进行加密
    ciphertext := make([]byte, len(plaintext))
    mode := cipher.NewCBCEncrypter(block, key)
    mode.CryptBlocks(ciphertext, []byte(plaintext))

    // 对加密后的字节数组进行base64编码
    encoded := base64.StdEncoding.EncodeToString(ciphertext)

    // 去掉base64编码后的字符串中的斜杠
    encoded = strip(encoded, "/")

    fmt.Println(encoded)
}

// 去掉字符串中的指定字符
func strip(s string, char string) string {
    result := ""
    for i := 0; i < len(s); i++ {
        if string(s[i]) != char {
            result += string(s[i])
        }
    }
    return result
}

输出结果:

TnT1zjUZJ6U

注意:由于des加密算法的安全性较低,不建议在实际应用中使用,可以考虑使用更加安全的AES加密算法。

golang 使用des加密方式 加密字符串不要斜杠

原文地址: https://www.cveoy.top/t/topic/tFA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录