kwm_cipher.go 607 B

1234567891011121314151617181920212223242526272829303132
  1. package kwm
  2. import (
  3. "encoding/binary"
  4. "strconv"
  5. )
  6. type kwmCipher struct {
  7. mask []byte
  8. }
  9. func newKwmCipher(key []byte) *kwmCipher {
  10. return &kwmCipher{mask: generateMask(key)}
  11. }
  12. func generateMask(key []byte) []byte {
  13. keyInt := binary.LittleEndian.Uint64(key)
  14. keyStr := strconv.FormatUint(keyInt, 10)
  15. keyStrTrim := padOrTruncate(keyStr, 32)
  16. mask := make([]byte, 32)
  17. for i := 0; i < 32; i++ {
  18. mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
  19. }
  20. return mask
  21. }
  22. func (c kwmCipher) Decrypt(buf []byte, offset int) {
  23. for i := range buf {
  24. buf[i] ^= c.mask[(offset+i)&0x1F] // equivalent: [i % 32]
  25. }
  26. }