i'm trying encrypt data in go it's hardly ever correct cipher.blocksize
.
is there "built-in" way add padding or should using function add manually?
this solution now:
// encrypt() encrypts message, // message isn't proper length, add padding. func encrypt(msg []byte, key []byte) []byte { cipher, err := aes.newcipher(key) if err != nil { log.fatal(err) } if len(msg) < cipher.blocksize() { var endlength = cipher.blocksize() - len(msg) ending := make([]byte, endlength, endlength) msg = append(msg[:], ending[:]...) cipher.encrypt(msg, msg) } else { var endlength = len(msg) % cipher.blocksize() ending := make([]byte, endlength, endlength) msg = append(msg[:], ending[:]...) cipher.encrypt(msg, msg) } return msg }
looking @ package cipher appears may have add padding yourself, see pkcs#7 padding.
essentially add required padding bytes value of each byte number of padding byte added.
note need add padding consistently , means if data encrypted exact multiple of block size entire block of padding must added since there no way know data if padding has been added or not, common mistake try out-smart this. consider if last byte 0x00, padding or data?
Comments
Post a Comment