Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func EncryptByCBCMode(key []byte, plainText string) ([]byte, error) {
- if len(plainText) % aes.BlockSize != 0 {
- panic("Plain text must be multiple of 128bit")
- }
- block, err := aes.NewCipher(key); if err != nil {
- return nil, err
- }
- cipherText := make([]byte, aes.BlockSize + len(plainText)) // 初期化ベクトルを保存するためにaes.BlockSizeを加えている
- iv := cipherText[:aes.BlockSize] // Unique iv is required
- _, err = rand.Read(iv); if err != nil {
- return nil, err
- }
- cbc := cipher.NewCBCEncrypter(block, iv)
- cbc.CryptBlocks(cipherText[aes.BlockSize:], []byte(plainText))
- return cipherText, nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement