Documentation ¶
Overview ¶
提供DES、3DES、AES加解密方法
提供RSA签名验签方法
Index ¶
- Constants
- func AesNewCBCDecrypter(content []byte, aesKey []byte) ([]byte, error)
- func AesNewCBCEncrypter(content []byte, aesKey []byte) ([]byte, error)
- func DesDecrypt(crypted, key []byte) ([]byte, error)
- func DesEncrypt(originData, key []byte) ([]byte, error)
- func Pkcs5Padding(cipherText []byte, blockSize int) []byte
- func Pkcs5UnPadding(origData []byte) []byte
- func RsaSign(originData []byte, priKey []byte, algorithm string) ([]byte, error)
- func RsaVerify(originData []byte, publicKey []byte, sign []byte, algorithm string) (bool, error)
- func TripleECBDesDecrypt(crypted, key []byte) ([]byte, error)
- func TripleECBDesEncrypt(originData, key []byte) ([]byte, error)
Examples ¶
Constants ¶
View Source
const ( SHA1 = "SHA1" SHA256 = "SHA256" )
Variables ¶
This section is empty.
Functions ¶
func AesNewCBCDecrypter ¶
AES解密方法
func AesNewCBCEncrypter ¶
AES加密方法
在某些场合,ECB方式不能提供严格的数据保密性,因此并不推荐用于密码协议中。
填充的blockSize为16
密钥长度需要是AES-128(16bytes)或者AES-256(32bytes)
原文必须填充至blockSize的整数倍,填充方法可以参考RFC5246
注意:正常来说,对IV有随机性要求,但没有保密性要求,所以常见的做法是将IV包含在加密文本当中。
Example ¶
//原文 originData := []byte("Hello,AES") //密钥 key := []byte("1234567890123456") //加密 encrypted, err := AesNewCBCEncrypter(originData, key) if err != nil { _ = fmt.Sprintf("AES encrypt error:%s", err.Error()) } //解密 result, err := AesNewCBCDecrypter(encrypted, key) if err != nil { _ = fmt.Sprintf("AES decrypt error%s", err.Error()) } fmt.Println(string(result))
Output:
func RsaSign ¶
RSA签名方法
支持SHA1和SHA256
Example ¶
//私钥 var priKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDWqjG6dpVIjV0JTlr5qytAUXM191ilTWT3Fd+dU/4wyMOh6duyPFZ5gLbUndDr7sjAVGISLPp4xMLDaU1jPex+9Me3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wdrVEOMShDyd3OZ6GWR6oWIM+1pXinLVeIBqSUzerxCM1oVg/DjPOfM1hK7qysHhAoGBAMa+zvkmuxNtvX+7dk/+myjEWtFaoOQHgkMnSmpxQ0vl6JrwwU4O1npUNg8vI3sV0HoSm/+oa254ddYB/JoQFGSnOA2fZXM667QSCcPXjPC42Clq/N0zouYK2VD7g78oycEIR3DR7VNN499rwdlAdHjcm5fUXaW3ENK0YWttwlg5AoGAGBtO5NDWfJTpJUOlAJmq4gOWI/FHczU5vaxmCMSPo65GLlecMXfoOWStfZFr9pHzO5fLNpnrJNClrumpoZM+3XYyzQW9bWRSeXuA2Jk2I7Y0oGgfkumN7+Oqp3Z9ul9Tsp3yns8skVyhhEk7LqMlHJBgJnotaI2sMlr36D6v6WA=" //公钥 var pubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1qoxunaVSI1dCU5a+asrQFFzNfdYpU1k9xXfnVP+MMjDoenbsjxWeYC21J3Q6+7IwFRiEiz6eMTCw2lNYz3sfvTHtx5xth9V/aaXSqCMmdzBJTty3VlURgS8Ul9o8437i8i/wDylkzNi8Pc4ruVq0MD+zaOcRqTk97EO7NbJchEAmmXhtoM1D7IPlUwQOrk87lbkAxUtBUi6x/bRlImq7R4+1KHF6TOB9kCiY+MDF01/Z4UdOGEgusqzbOAlD2xx7YOVvkx9KiqycZva5sMSeNlPK6eVJmZvj5tABogyJ2Zifz+Et24MnR3dYK5MEdNcDqtSOwy0xkBtGJAsOQIidQIDAQAB" priKeybyte, err := base64.StdEncoding.DecodeString(priKey) if err != nil { _ = fmt.Sprintf("priKey base64 decode error: %s", err.Error()) } pubKeybyte, err := base64.StdEncoding.DecodeString(pubKey) if err != nil { _ = fmt.Sprintf("pubkey base64 decode error: %s", err.Error()) } //原文 originData := []byte("Hello,RSA.") //使用私钥签名 sign, err := RsaSign(originData, priKeybyte, SHA256) if err != nil { _ = fmt.Sprintf("RSA sign by SHA256 error: %s", err.Error()) } //使用公钥验签 isOk, err := RsaVerify(originData, pubKeybyte, sign, SHA256) if err != nil { _ = fmt.Sprintf("RSA verify by SHA256 error: %s", err.Error()) } if !isOk { fmt.Println("RSA verify by SHA256 not pass") } else { fmt.Println("RSA verify by SHA256 pass") }
Output:
func TripleECBDesEncrypt ¶
3DES加密方法
ECB 加-解-加
Example ¶
//密钥 key := []byte("123456789012345678901234") //原文 origData := []byte("Hello,3DES") //3des加密 crypted, err := TripleECBDesEncrypt(origData, key) if err != nil { _ = fmt.Sprintf("TripleECBDesEncrypt error: %s", err.Error()) } //3des解密 result, err := TripleECBDesDecrypt(crypted, key) if err != nil { _ = fmt.Sprintf("TripleECBDesDecrypt error: %s", err.Error()) } fmt.Println(string(result))
Output:
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.