github.com/fastwego/offiaccount@v1.0.1/util/aes_crypto_test.go (about)

     1  // Copyright 2014 chanxuehong(chanxuehong@gmail.com)
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/base64"
    20  	"fmt"
    21  	"testing"
    22  )
    23  
    24  func TestAESEncryptMsg(t *testing.T) {
    25  	appId := "wx45f133bf6fce646e"
    26  	encodingAESKey := "AdiqDDDvUNCeE1ZW5XJmjf9fqNBJpGBs4vL4cHKmHBS"
    27  
    28  	random := []byte("cc9632a98304f81c")
    29  	plaintext := []byte(`<xml><ToUserName><![CDATA[gh_b1eb3f8bd6c6]]></ToUserName>
    30  <FromUserName><![CDATA[okPEat9FRX96xG8JQvTxHLpzDV64]]></FromUserName>
    31  <CreateTime>1458889120</CreateTime>
    32  <MsgType><![CDATA[text]]></MsgType>
    33  <Content><![CDATA[test text message]]></Content>
    34  <MsgId>6265881059295447969</MsgId>
    35  </xml>`)
    36  
    37  	wantBase64Ciphertext := "gsKjKVChgrDDifoOdfrL/MWujrKxnSR1jBopv0zEdHFQXcx5I0bf4UxIRjektLHEziRxpU5zTw0s+gYF3WOFjk" +
    38  		"In30gzQl9XNKr2A+DCHVG05I2EcbOnnAR5EYgjGLAipra5nOfCPPIRFQTZ7SdanniXX73YOiCAKJlNH21+PApcYu4rPXxJ4eTXbBLwmfnS" +
    39  		"l7iojDX1LQIcC3FYmaapMQq/u+sJGsxshp4dLXJ6A5Ji3cSYAzXRVIxbNlHN1MWfdcZ0O5+ZtOU6dZiD8hJ4kkxh05EfOedWFjUy7ZhmXg" +
    40  		"rpZ4WpYPsrythXHE2Bg1Ohz8uf5h5X31yuU/3FPoa8rD21pfZnAjBT1QCkn6MxtL5lR+yoQReLwElVbtB6yJFPIZ+n9Qh/yKfIasxkzgIE" +
    41  		"o0pwPEbS17WCTyvRItJtU6tlo3rawJX+fsV63tKIfmLZjyZPDlV9/ka/nA/DT0KODw=="
    42  
    43  	base64Ciphertext := AESEncryptMsg(random, plaintext, appId, encodingAESKey)
    44  	if base64Ciphertext != wantBase64Ciphertext {
    45  		t.Errorf("tests AESEncryptMsg failed,\nhave: %s\nwant: %s\n", base64Ciphertext, wantBase64Ciphertext)
    46  		return
    47  	}
    48  }
    49  
    50  func TestAESDecryptMsg(t *testing.T) {
    51  	encodingAesKey := "AdiqDDDvUNCeE1ZW5XJmjf9fqNBJpGBs4vL4cHKmHBS"
    52  
    53  	wantRandom := []byte("cc9632a98304f81c")
    54  	wantPlaintext := []byte(`<xml><ToUserName><![CDATA[gh_b1eb3f8bd6c6]]></ToUserName>
    55  <FromUserName><![CDATA[okPEat9FRX96xG8JQvTxHLpzDV64]]></FromUserName>
    56  <CreateTime>1458889120</CreateTime>
    57  <MsgType><![CDATA[text]]></MsgType>
    58  <Content><![CDATA[test text message]]></Content>
    59  <MsgId>6265881059295447969</MsgId>
    60  </xml>`)
    61  	wantAppId := []byte("wx45f133bf6fce646e")
    62  
    63  	base64Ciphertext := "gsKjKVChgrDDifoOdfrL/MWujrKxnSR1jBopv0zEdHFQXcx5I0bf4UxIRjektLHEziRxpU5zTw0s+gYF3WOFjk" +
    64  		"In30gzQl9XNKr2A+DCHVG05I2EcbOnnAR5EYgjGLAipra5nOfCPPIRFQTZ7SdanniXX73YOiCAKJlNH21+PApcYu4rPXxJ4eTXbBLwmfnS" +
    65  		"l7iojDX1LQIcC3FYmaapMQq/u+sJGsxshp4dLXJ6A5Ji3cSYAzXRVIxbNlHN1MWfdcZ0O5+ZtOU6dZiD8hJ4kkxh05EfOedWFjUy7ZhmXg" +
    66  		"rpZ4WpYPsrythXHE2Bg1Ohz8uf5h5X31yuU/3FPoa8rD21pfZnAjBT1QCkn6MxtL5lR+yoQReLwElVbtB6yJFPIZ+n9Qh/yKfIasxkzgIE" +
    67  		"o0pwPEbS17WCTyvRItJtU6tlo3rawJX+fsV63tKIfmLZjyZPDlV9/ka/nA/DT0KODw=="
    68  	random, plaintext, appId, err := AESDecryptMsg(base64Ciphertext, encodingAesKey)
    69  	if err != nil {
    70  		t.Error(err)
    71  		return
    72  	}
    73  	if !bytes.Equal(random, wantRandom) {
    74  		t.Errorf("tests AESDecryptMsg failed,\nhave random: %s\nwant random: %s\n", random, wantRandom)
    75  	}
    76  	if !bytes.Equal(plaintext, wantPlaintext) {
    77  		fmt.Println(len(plaintext), " == ", len(wantPlaintext))
    78  		t.Errorf("tests AESDecryptMsg failed,\nhave plaintext: %s\nwant plaintext: %s\n", plaintext, wantPlaintext)
    79  	}
    80  	if !bytes.Equal(appId, wantAppId) {
    81  		t.Errorf("tests AESDecryptMsg failed,\nhave appid: %s\nwant appid: %s\n", appId, wantAppId)
    82  	}
    83  }
    84  
    85  func TestAESDecryptData(t *testing.T) {
    86  	aesKey, err := base64.StdEncoding.DecodeString("HogNecGqZeDxFIDGjBwWKw==")
    87  	aesIv, err := base64.StdEncoding.DecodeString("aoZqkfGDWwqj6rFgWdafyw==")
    88  
    89  	base64Ciphertext := "4B9B1aknFM6yQjAh9mFxH3iN4PZXUGfpBLB98CRAVZzNfUI4J1WUur70+NSH/5MXcCidrt44hi6dkByTtRPxrTIV1BOOTvaa2G5NWunTXZJ37/Oq0ezydbDal5v+X3bvVVeFR6MkhYI+hT9xVl2XnE/Bzon2gIq9F9Fy8Yny0VPqsQ95xUyXnN3/IuhiquR1pAgKjDK3kgCoqhUVNa0dRRQQgTNIpy1djbLfyErPXGTXe1qhAj7RvDdJtRloEfg63JgaB/QTR2BLEGT7/GfHnwROfngxa3esGDeBr9Mtav67R4PjYESVFtH2Yf2npaDWAvAMvr+8hNVd2tjy/3HgImctZWo7bh1OHa/ktH4wKYVbTgxZPg/lgTWC+zsl1Z9g07vWQyGpaA11HODDGn1Kdvh6esiY7T3JQ15nKs1rdyXigNQFb9+kicPiInKVfOiuMcGEazli5/UQHF5rnuWhkg/wy+PRbZybR18lLh5d+EW1CnK8gNcQblDJPvx6QFcFhPxr28WkEd7ys0BZQGCIkQ=="
    90  	ciphertext, err := base64.StdEncoding.DecodeString(base64Ciphertext)
    91  	if err != nil {
    92  		t.Error(err)
    93  		return
    94  	}
    95  
    96  	raw, err := AESDecryptData(ciphertext, aesKey, aesIv)
    97  
    98  	fmt.Println(string(raw), err)
    99  	if err != nil {
   100  		t.Error(err)
   101  		return
   102  	}
   103  
   104  }