Signed-off-by: Yuan Si <do4suki@gmail.com>
This commit is contained in:
2025-03-10 15:29:29 +08:00
committed by YumeMichi
parent 782c2d7ab0
commit 70398da4b9
31 changed files with 712 additions and 819 deletions
+6 -6
View File
@@ -10,32 +10,32 @@ var (
iv = []byte("12345678abcdefgh")
)
func Padding(plainText []byte, blockSize int) []byte {
func padding(plainText []byte, blockSize int) []byte {
n := blockSize - len(plainText)%blockSize
temp := bytes.Repeat([]byte{byte(n)}, n)
plainText = append(plainText, temp...)
return plainText
}
func UnPadding(cipherText []byte) []byte {
func unPadding(cipherText []byte) []byte {
end := cipherText[len(cipherText)-1]
cipherText = cipherText[:len(cipherText)-int(end)]
return cipherText
}
func AES_CBC_Encrypt(plainText []byte, key []byte) []byte {
func AESCBCEncrypt(plainText []byte, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
plainText = Padding(plainText, block.BlockSize())
plainText = padding(plainText, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, iv)
cipherText := make([]byte, len(plainText))
blockMode.CryptBlocks(cipherText, plainText)
return cipherText
}
func AES_CBC_Decrypt(cipherText []byte, key []byte) []byte {
func AESCBCDecrypt(cipherText []byte, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
@@ -43,6 +43,6 @@ func AES_CBC_Decrypt(cipherText []byte, key []byte) []byte {
plainText := make([]byte, len(cipherText))
blockMode := cipher.NewCBCDecrypter(block, iv)
blockMode.CryptBlocks(plainText, cipherText)
plainText = UnPadding(plainText)
plainText = unPadding(plainText)
return plainText
}
+4 -4
View File
@@ -13,7 +13,7 @@ import (
"runtime"
)
func RSA_Gen(bits int) {
func RSAGen(bits int) {
//get current path
_, currentpath, _, _ := runtime.Caller(0)
currentpath = filepath.Dir(currentpath)
@@ -75,7 +75,7 @@ func RSA_Gen(bits int) {
pem.Encode(publickeyfile, &publickeyblock)
}
func RSA_Encrypt(plainText []byte, publickeypath string) []byte {
func RSAEncrypt(plainText []byte, publickeypath string) []byte {
//open publickeyfile
publickeyfile, err := os.Open(publickeypath)
if err != nil {
@@ -110,7 +110,7 @@ func RSA_Encrypt(plainText []byte, publickeypath string) []byte {
return cipherText
}
func RSA_Decrypt(cipherText []byte) []byte {
func RSADecrypt(cipherText []byte) []byte {
//open privatekeyfile
privatekeyfile, err := os.Open(config.PrivateKeyPath)
if err != nil {
@@ -138,7 +138,7 @@ func RSA_Decrypt(cipherText []byte) []byte {
return plainText
}
func RSA_Sign_SHA1(cipherText []byte) []byte {
func RSASignSHA1(cipherText []byte) []byte {
//open privatekeyfile
privatekeyfile, err := os.Open(config.PrivateKeyPath)
if err != nil {