You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1.2 KiB
31 lines
1.2 KiB
package sra
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func Test_RSA(t *testing.T) {
|
|
//rsa 密钥文件产生
|
|
fmt.Println("-------------------------------获取RSA公私钥-----------------------------------------")
|
|
prvKey, pubKey, _ := GenRsaKey(1204)
|
|
fmt.Println(string(prvKey))
|
|
fmt.Println(string(pubKey))
|
|
|
|
fmt.Println("-------------------------------进行签名与验证操作-----------------------------------------")
|
|
var data = "卧了个槽,这么神奇的吗??!!! ԅ(¯﹃¯ԅ) !!!!!!)"
|
|
fmt.Println("对消息进行签名操作...")
|
|
signData, _ := RsaSignWithSha256([]byte(data), prvKey)
|
|
fmt.Println("消息的签名信息: ", hex.EncodeToString(signData))
|
|
fmt.Println("\n对签名信息进行验证...")
|
|
if ok, _ := RsaVerySignWithSha256([]byte(data), signData, pubKey); ok {
|
|
fmt.Println("签名信息验证成功,确定是正确私钥签名!!")
|
|
}
|
|
|
|
fmt.Println("-------------------------------进行加密解密操作-----------------------------------------")
|
|
ciphertext, _ := RsaEncrypt([]byte(data), pubKey)
|
|
fmt.Println("公钥加密后的数据:", hex.EncodeToString(ciphertext))
|
|
sourceData, _ := RsaDecrypt(ciphertext, prvKey)
|
|
fmt.Println("私钥解密后的数据:", string(sourceData))
|
|
}
|
|
|