Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
C
Crypto
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Go
  • Crypto
  • Issues
  • #1

Closed
Open
Created Jun 10, 2020 by joker@jokerReporter

PKCS7UnPadding 新增錯誤檢查

function PKCS7UnPadding 改為:

func PKCS7UnPadding(plainTextWithPadding []byte) (plainTextWithoutPadding []byte, err error) {
	defer func() {
		if err := recover(); err != nil {
			plainTextWithoutPadding = nil
			err = fmt.Errorf("%+v", err)
		}
	}()

	length := len(plainTextWithPadding)
	unpadding := int(plainTextWithPadding[length-1])

	// Return plaintext without padding.
	plainTextWithoutPadding = plainTextWithPadding[:(length - unpadding)]
	return plainTextWithoutPadding, nil
}

function AesDecrypt 改為:

func AesDecrypt(cipherText, key []byte) ([]byte, error) {
	// Hexadecimal to byte.
	cipherText, err := hex.DecodeString(string(cipherText))
	if err != nil {
		return nil, err
	}

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

	blockSize := block.BlockSize()
	blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
	plainTextWithPadding := make([]byte, len(cipherText))
	blockMode.CryptBlocks(plainTextWithPadding, cipherText)
	plainText, err := PKCS7UnPadding(plainTextWithPadding)

	return plainText, err
}
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None