Sunday, May 19, 2024
HomeGolangCan anybody find out about encryption decryption utilizing crypto bundle base64 with...

Can anybody find out about encryption decryption utilizing crypto bundle base64 with similar key? – Getting Assist


Can anybody find out about encryption decryption utilizing crypto bundle base64 with similar key ??

base64 is just not an encryption it’s an encoding., and it doesn’t have any notion of a key. Are you able to please elaborate in your query?

Sure , i do know base64 is encoding however i exploit this perform for getting and inserting doc in my database however when i exploit this give me error in surprising EOF , so i debug this and located key’s mismatched in each perform utilization so can any thought learn how to set related key in each perform???
`func encrypt(keyString string, stringToEncrypt string) (encryptedString string) {
// convert key to bytes
key, _ := hex.DecodeString(keyString)
plaintext := byte(stringToEncrypt)

//Create a brand new Cipher Block from the important thing
block, err := aes.NewCipher(key)
if err != nil {
	panic(err.Error())
}

// The IV must be distinctive, however not safe. Due to this fact it's normal to
// embody it originally of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
	panic(err)
}

stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

// convert to base64
return base64.URLEncoding.EncodeToString(ciphertext)

}

// decrypt from base64 to decrypted string
func decrypt(keyString string, stringToDecrypt string) string {
key, _ := hex.DecodeString(keyString)
ciphertext, _ := base64.URLEncoding.DecodeString(stringToDecrypt)

block, err := aes.NewCipher(key)
if err != nil {
	panic(err)
}

// The IV must be distinctive, however not safe. Due to this fact it's normal to
// embody it originally of the ciphertext.
if len(ciphertext) < aes.BlockSize {
	panic("ciphertext too quick")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]

stream := cipher.NewCFBDecrypter(block, iv)

// XORKeyStream can work in-place if the 2 arguments are the identical.
stream.XORKeyStream(ciphertext, ciphertext)

return fmt.Sprintf("%s", ciphertext)

}

`

I didnt do a deep Evaluation, although on a fast look it appears as should you aren’t prepending the IV to the output when encrypting, however attempt to learn it from the enter when decrypting.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments