123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package encryption
- import (
- "bytes"
- "crypto/aes"
- "encoding/hex"
- "reflect"
- "testing"
- )
- func Test_aesCBCWithNoPadding_Decrypt(t *testing.T) {
- type fields struct {
- key []byte
- iv []byte
- }
- type args struct {
- ciphertext []byte
- }
- tests := []struct {
- name string
- fields fields
- args args
- want []byte
- wantErr bool
- }{
- // TODO: Add test cases.
- {
- name: "test",
- fields: fields{
- key: []byte("a2b1805169887fd9bca40e45"),
- iv: []byte("69887fd9bca40e45"),
- },
- args: args{
- ciphertext: []byte("69001dd634e3deb6357df6e6d7a867db"),
- },
- want: []byte("hello"),
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- b, _ := hex.DecodeString(string(tt.args.ciphertext))
- a := &aesCBCWithNoPadding{
- key: tt.fields.key,
- iv: tt.fields.iv,
- }
- got, err := a.Decrypt(b)
- //got, err := aesDecrypt(b, tt.fields.key, tt.fields.iv)
- if (err != nil) != tt.wantErr {
- t.Errorf("Decrypt() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- c := hex.EncodeToString(got)
- if !reflect.DeepEqual([]byte(c), tt.want) {
- t.Errorf("Decrypt() got = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_aesCBCWithNoPadding_Encrypt(t *testing.T) {
- type fields struct {
- key []byte
- iv []byte
- }
- type args struct {
- plaintext []byte
- }
- tests := []struct {
- name string
- fields fields
- args args
- want []byte
- wantErr bool
- }{
- // TODO: Add test cases.
- {
- name: "test",
- fields: fields{
- key: []byte("a2b1805169887fd9bca40e45"),
- iv: []byte("69887fd9bca40e45"),
- },
- args: args{
- []byte("hello"),
- },
- want: []byte("69001dd634e3deb6357df6e6d7a867db"),
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if len(tt.args.plaintext)%aes.BlockSize != 0 {
- padding := aes.BlockSize - len(tt.args.plaintext)%aes.BlockSize
- tt.args.plaintext = append(tt.args.plaintext, bytes.Repeat([]byte{byte(0)}, padding)...)
- }
- //got, err := aesEncrypt(tt.args.plaintext, tt.fields.key, tt.fields.iv)
- a := &aesCBCWithNoPadding{
- key: tt.fields.key,
- iv: tt.fields.iv,
- }
- got, err := a.Encrypt(tt.args.plaintext)
- if (err != nil) != tt.wantErr {
- t.Errorf("Encrypt() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- c := hex.EncodeToString(got)
- t.Log("密文:", c, string(tt.want))
- if !reflect.DeepEqual([]byte(c), tt.want) {
- t.Errorf("Encrypt() got = %v, want %v", got, tt.want)
- }
- })
- }
- }
|