Golang中的XOR密码,使用自定义base64编码/解码,关于填充的小问题

7fyelxc5  于 5个月前  发布在  Go
关注(0)|答案(1)|浏览(77)

我的代码的预期输出是"IA0aB1QMAFQVRQARFxcWAFQIFgcHBBQRWg==",但实际上它输出的是<<<<<<<<<<<<<<<<<<<<<<<<<<"IA0aB1QMAFQVRQARFxcWAFQIFgcHBBQRWgAA"
问题最有可能发生在func customBase64Encode,但我不能把我的头周围是填充不正确?
感谢任何想法

package main

import (
    "fmt"
)

const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

func customBase64Encode(input []byte) string {
    var result string

    for i := 0; i < len(input); i += 3 {
        // Create a 24-bit value from three 8-bit values
        var bits uint32
        for j := 0; j < 3 && i+j < len(input); j++ {
            bits |= uint32(input[i+j]) << (8 * (2 - j))
        }

        // Break it into four 6-bit values
        for j := 0; j < 4; j++ {
            index := (bits >> ((3 - j) * 6)) & 0x3F
            result += string(base64Chars[index])
        }
    }

    // Add padding if necessary
    padding := (4 - len(result)%4) % 4
    for i := 0; i < padding; i++ {
        result += "="
    }

    return result
}

func customBase64Decode(input string) []byte {
    var result []byte

    for i := 0; i < len(input); i += 4 {
        var val uint32

        // Create a 24-bit value from four 6-bit values
        for j := 0; j < 4; j++ {
            char := input[i+j]
            index := byteIndex(char, base64Chars)
            val = val<<6 | uint32(index)
        }

        // Break it into three 8-bit values
        for j := 0; j < 3; j++ {
            result = append(result, byte((val>>(8*(2-j)))&0xFF))
        }
    }

    return result
}

func byteIndex(char byte, chars string) int {
    for i := 0; i < len(chars); i++ {
        if chars[i] == char {
            return i
        }
    }
    return -1
}

func XOR(text string, encrypt bool, key string) string {
    // Convert the key to a byte array
    keyBytes := []byte(key)

    // If decrypting, decode the input text using custom Base64 decoding
    if !encrypt {
        decodedText := customBase64Decode(text)
        text = string(decodedText)
    }

    // Convert the text to a byte array
    textBytes := []byte(text)

    // Initialize result array
    resultBytes := make([]byte, len(textBytes))

    // Perform XOR operation
    for i := 0; i < len(textBytes); i++ {
        // Get the corresponding byte from the key, using modulo to repeat the key if necessary
        keyByte := keyBytes[i%len(keyBytes)]

        // Perform XOR
        if encrypt {
            resultBytes[i] = textBytes[i] ^ keyByte
        } else {
            // For decryption, XOR operation is the same as encryption
            resultBytes[i] = textBytes[i] ^ keyByte
        }
    }

    // If encrypting, encode the result using custom Base64 encoding
    if encrypt {
        resultText := customBase64Encode(resultBytes)
        return resultText
    }

    // If decrypting, return the result as a string
    return string(resultBytes)
}

func main() {
    // Example usage
    encryptedText := XOR("This is a secret message.", true, "test")
    fmt.Println(encryptedText) // should be "IA0aB1QMAFQVRQARFxcWAFQIFgcHBBQRWg==" but is "IA0aB1QMAFQVRQARFxcWAFQIFgcHBBQRWgAA" 

    decryptedText := XOR(encryptedText, false, "test")
    fmt.Println(decryptedText) // should be "This is a secret message." but is "This is a secret message.es"
}

字符串

nxagd54h

nxagd54h1#

如果不导入“encode/base64”,无法使代码工作

package main

import (
    "encoding/base64"
    "fmt"
)

func XOR(text string, encrypt bool, key string) string {
    // Convert the key to a byte array
    keyBytes := []byte(key)

    // If decrypting, decode the input text from base64
    if !encrypt {
        decodedText, err := base64.StdEncoding.DecodeString(text)
        if err != nil {
            fmt.Println("Error decoding base64:", err)
            return ""
        }
        text = string(decodedText)
    }

    // Convert the text to a byte array
    textBytes := []byte(text)

    // Initialize result array
    resultBytes := make([]byte, len(textBytes))

    // Perform XOR operation
    for i := 0; i < len(textBytes); i++ {
        // Get the corresponding byte from the key, using modulo to repeat the key if necessary
        keyByte := keyBytes[i%len(keyBytes)]

        // Perform XOR
        if encrypt {
            resultBytes[i] = textBytes[i] ^ keyByte
        } else {
            // For decryption, XOR operation is the same as encryption
            resultBytes[i] = textBytes[i] ^ keyByte
        }
    }

    // If encrypting, encode the result as base64
    if encrypt {
        resultText := base64.StdEncoding.EncodeToString(resultBytes)
        return resultText
    }

    // If decrypting, return the result as a string
    return string(resultBytes)
}

字符串

相关问题