AesCBCWithNoPadding_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package encryption
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "encoding/hex"
  6. "reflect"
  7. "testing"
  8. )
  9. func Test_aesCBCWithNoPadding_Decrypt(t *testing.T) {
  10. type fields struct {
  11. key []byte
  12. iv []byte
  13. }
  14. type args struct {
  15. ciphertext []byte
  16. }
  17. tests := []struct {
  18. name string
  19. fields fields
  20. args args
  21. want []byte
  22. wantErr bool
  23. }{
  24. // TODO: Add test cases.
  25. {
  26. name: "test",
  27. fields: fields{
  28. key: []byte("a2b1805169887fd9bca40e45"),
  29. iv: []byte("69887fd9bca40e45"),
  30. },
  31. args: args{
  32. ciphertext: []byte("69001dd634e3deb6357df6e6d7a867db"),
  33. },
  34. want: []byte("hello"),
  35. wantErr: false,
  36. },
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. b, _ := hex.DecodeString(string(tt.args.ciphertext))
  41. a := &aesCBCWithNoPadding{
  42. key: tt.fields.key,
  43. iv: tt.fields.iv,
  44. }
  45. got, err := a.Decrypt(b)
  46. //got, err := aesDecrypt(b, tt.fields.key, tt.fields.iv)
  47. if (err != nil) != tt.wantErr {
  48. t.Errorf("Decrypt() error = %v, wantErr %v", err, tt.wantErr)
  49. return
  50. }
  51. c := hex.EncodeToString(got)
  52. if !reflect.DeepEqual([]byte(c), tt.want) {
  53. t.Errorf("Decrypt() got = %v, want %v", got, tt.want)
  54. }
  55. })
  56. }
  57. }
  58. func Test_aesCBCWithNoPadding_Encrypt(t *testing.T) {
  59. type fields struct {
  60. key []byte
  61. iv []byte
  62. }
  63. type args struct {
  64. plaintext []byte
  65. }
  66. tests := []struct {
  67. name string
  68. fields fields
  69. args args
  70. want []byte
  71. wantErr bool
  72. }{
  73. // TODO: Add test cases.
  74. {
  75. name: "test",
  76. fields: fields{
  77. key: []byte("a2b1805169887fd9bca40e45"),
  78. iv: []byte("69887fd9bca40e45"),
  79. },
  80. args: args{
  81. []byte("hello"),
  82. },
  83. want: []byte("69001dd634e3deb6357df6e6d7a867db"),
  84. wantErr: false,
  85. },
  86. }
  87. for _, tt := range tests {
  88. t.Run(tt.name, func(t *testing.T) {
  89. if len(tt.args.plaintext)%aes.BlockSize != 0 {
  90. padding := aes.BlockSize - len(tt.args.plaintext)%aes.BlockSize
  91. tt.args.plaintext = append(tt.args.plaintext, bytes.Repeat([]byte{byte(0)}, padding)...)
  92. }
  93. //got, err := aesEncrypt(tt.args.plaintext, tt.fields.key, tt.fields.iv)
  94. a := &aesCBCWithNoPadding{
  95. key: tt.fields.key,
  96. iv: tt.fields.iv,
  97. }
  98. got, err := a.Encrypt(tt.args.plaintext)
  99. if (err != nil) != tt.wantErr {
  100. t.Errorf("Encrypt() error = %v, wantErr %v", err, tt.wantErr)
  101. return
  102. }
  103. c := hex.EncodeToString(got)
  104. t.Log("密文:", c, string(tt.want))
  105. if !reflect.DeepEqual([]byte(c), tt.want) {
  106. t.Errorf("Encrypt() got = %v, want %v", got, tt.want)
  107. }
  108. })
  109. }
  110. }