mirror of
https://github.com/wumansgy/goEncrypt.git
synced 2025-09-26 19:51:27 +08:00
GO加密包
This commit is contained in:
@@ -3,7 +3,6 @@ package wmgocrypt
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"wmgocrypt/Tools"
|
||||
"bytes"
|
||||
"runtime"
|
||||
"fmt"
|
||||
@@ -35,7 +34,7 @@ func AesCBC_Encrypt(plainText,key []byte)[]byte{
|
||||
panic(err)
|
||||
}
|
||||
//2.分组填充数据 blockSize 16
|
||||
paddingText := Tools.PKCS5Padding(plainText, block.BlockSize())
|
||||
paddingText := PKCS5Padding(plainText, block.BlockSize())
|
||||
//3.创建使用cbc分组模式的blockMode接口
|
||||
iv :=[]byte("wumansgy12345678")//初始化向量,需要和block.blocksize长度一样
|
||||
blockMode := cipher.NewCBCEncrypter(block, iv)
|
||||
@@ -82,7 +81,7 @@ func AesCBC_Decrypt(cipherText,key []byte) []byte{
|
||||
} //防止用户输入两次密钥不一样,然后返回错误
|
||||
}()
|
||||
|
||||
plainText := Tools.PKCS5UnPadding(paddingText)
|
||||
plainText := PKCS5UnPadding(paddingText)
|
||||
|
||||
//5.返回明文
|
||||
return plainText
|
||||
|
@@ -3,7 +3,6 @@ package wmgocrypt
|
||||
import (
|
||||
"crypto/des"
|
||||
"crypto/cipher"
|
||||
"wmgocrypt/Tools"
|
||||
"bytes"
|
||||
"runtime"
|
||||
"fmt"
|
||||
@@ -39,7 +38,7 @@ func DesCBC_Encrypt(plainText ,key []byte)[]byte{//加密密钥是要8字节的
|
||||
panic(err)
|
||||
}
|
||||
//2.对明文进行分组填充处理
|
||||
paddingText := Tools.PKCS5Padding(plainText, block.BlockSize())
|
||||
paddingText := PKCS5Padding(plainText, block.BlockSize())
|
||||
|
||||
//3.指定使用哪种分组模式 返回一个密码分组链接模式的、底层用b加密的BlockMode接口,初始向量iv的长度必须等于b的块尺寸。
|
||||
iv:=[]byte("wumansgy") //初始化向量
|
||||
@@ -89,7 +88,7 @@ func DesCBC_Decrypt(cipherText ,key []byte) []byte{
|
||||
}
|
||||
} //防止用户输入两次密钥不一样,然后返回错误
|
||||
}()
|
||||
unPaddingText := Tools.PKCS5UnPadding(plainText)
|
||||
unPaddingText := PKCS5UnPadding(plainText)
|
||||
|
||||
return unPaddingText
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package Tools
|
||||
package wmgocrypt
|
||||
|
||||
import (
|
||||
"bytes"
|
87
RsaCrypt.go
Normal file
87
RsaCrypt.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package wmgocrypt
|
||||
|
||||
import (
|
||||
"encoding/pem"
|
||||
"crypto/x509"
|
||||
"crypto/rsa"
|
||||
"crypto/rand"
|
||||
)
|
||||
|
||||
/*
|
||||
@Time : 2018/11/2 19:04
|
||||
@Author : wuman
|
||||
@File : rsacrypt
|
||||
@Software: GoLand
|
||||
*/
|
||||
|
||||
/*
|
||||
用rsa加密操作
|
||||
*/
|
||||
|
||||
func RsaEncrypt(plainText ,key []byte)(cryptText []byte,err error){
|
||||
|
||||
/*file, _ := os.Open(path)
|
||||
defer file.Close()
|
||||
|
||||
//1.1 读取文件的内容
|
||||
// 获取文件的本身大小
|
||||
fileInfo, _ := file.Stat()
|
||||
buf := make([]byte,fileInfo.Size())
|
||||
//1.2 将文件内容读取到buf中
|
||||
file.Read(buf)
|
||||
|
||||
//2. pem 解码
|
||||
fmt.Println(buf)
|
||||
block, _ := pem.Decode(buf)*/
|
||||
//1. pem 解码
|
||||
block, _:= pem.Decode(key)
|
||||
|
||||
//2. block中的Bytes是x509编码的内容, x509解码
|
||||
publicKeyInterface,err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
if err!=nil{
|
||||
return []byte{},err //出错返回错误
|
||||
}
|
||||
//3.1 类型断言
|
||||
publicKey := publicKeyInterface.(*rsa.PublicKey)
|
||||
|
||||
//4. 使用公钥对明文进行加密
|
||||
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
|
||||
if err!=nil{
|
||||
return []byte{},err //出错返回错误
|
||||
}
|
||||
|
||||
return cipherText,nil
|
||||
}
|
||||
|
||||
func RsaDecrypt(cryptText ,key []byte)(plainText []byte,err error){
|
||||
|
||||
/*//1.打开文件
|
||||
file, _ := os.Open(path)
|
||||
defer file.Close()
|
||||
|
||||
//1.1 读取文件的内容
|
||||
// 获取文件的本身大小
|
||||
fileInfo, _ := file.Stat()
|
||||
buf := make([]byte,fileInfo.Size())
|
||||
//1.2 将文件内容读取到buf中
|
||||
file.Read(buf)
|
||||
|
||||
//2. pem 解码
|
||||
block, _ := pem.Decode(buf)*/
|
||||
//1. pem格式解码
|
||||
block, _ := pem.Decode(key)
|
||||
|
||||
//2.x509解码
|
||||
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err!=nil{
|
||||
return []byte{},err
|
||||
}
|
||||
|
||||
//3. 解密操作
|
||||
plainText,err= rsa.DecryptPKCS1v15(rand.Reader, privateKey, cryptText)
|
||||
if err!=nil{
|
||||
return []byte{},err
|
||||
}
|
||||
|
||||
return plainText,nil
|
||||
}
|
@@ -3,7 +3,6 @@ package wmgocrypt
|
||||
import (
|
||||
"crypto/des"
|
||||
"crypto/cipher"
|
||||
"wmgocrypt/Tools"
|
||||
"bytes"
|
||||
"runtime"
|
||||
"fmt"
|
||||
@@ -36,7 +35,7 @@ func TripleDesEncrypt(plainText,key []byte)[]byte{
|
||||
panic(err)
|
||||
}
|
||||
//2. 分组填充
|
||||
paddingText := Tools.PKCS5Padding(plainText, block.BlockSize())
|
||||
paddingText := PKCS5Padding(plainText, block.BlockSize())
|
||||
|
||||
//3.创建CBC分组模式blockMode
|
||||
iv :=[]byte("wumansgy")
|
||||
@@ -85,7 +84,7 @@ func TripleDesDecrypt(cipherText,key []byte) []byte{
|
||||
}
|
||||
} //防止用户输入两次密钥不一样,然后返回错误
|
||||
}()
|
||||
plainText := Tools.PKCS5UnPadding(paddingText)
|
||||
plainText := PKCS5UnPadding(paddingText)
|
||||
|
||||
return plainText
|
||||
}
|
Reference in New Issue
Block a user