Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. func EncryptByCBCMode(key []byte, plainText string) ([]byte, error) {
  2. if len(plainText) % aes.BlockSize != 0 {
  3. panic("Plain text must be multiple of 128bit")
  4. }
  5.  
  6. block, err := aes.NewCipher(key); if err != nil {
  7. return nil, err
  8. }
  9.  
  10. cipherText := make([]byte, aes.BlockSize + len(plainText)) // 初期化ベクトルを保存するためにaes.BlockSizeを加えている
  11. iv := cipherText[:aes.BlockSize] // Unique iv is required
  12. _, err = rand.Read(iv); if err != nil {
  13. return nil, err
  14. }
  15.  
  16. cbc := cipher.NewCBCEncrypter(block, iv)
  17. cbc.CryptBlocks(cipherText[aes.BlockSize:], []byte(plainText))
  18.  
  19. return cipherText, nil
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement