openssl

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

README

openssl

一、使用方法
1、AES的用法
package test

import (
    "github.com/daimayun/go/function"
    "github.com/daimayun/go/openssl"
    "github.com/daimayun/go/openssl/aes"
)

// AesECBEncrypt AES的ECB模式加密
func AesECBEncrypt(data, key string) (string, error) {
    res, err := aes.ECBEncrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return function.Base64Encode(string(res)), nil
}

// AesECBDecrypt AES的ECB模式解密
func AesECBDecrypt(data, key string) (string, error) {
    data = function.Base64Decode(data)
    res, err := aes.ECBDecrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return string(res), nil
}
2、DES的用法
package test

import (
    "github.com/daimayun/go/function"
    "github.com/daimayun/go/openssl"
    "github.com/daimayun/go/openssl/des"
)

// DesECBEncrypt DES的ECB模式加密
func DesECBEncrypt(data, key string) (string, error) {
    res, err := des.ECBEncrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return function.Base64Encode(string(res)), nil
}

// DesECBDecrypt DES的ECB模式解密
func DesECBDecrypt(data, key string) (string, error) {
    data = function.Base64Decode(data)
    res, err := des.ECBDecrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return string(res), nil
}
3、DES3的用法
package test

import (
    "github.com/daimayun/go/function"
    "github.com/daimayun/go/openssl"
    "github.com/daimayun/go/openssl/des3"
)

// Des3ECBEncrypt DES3的ECB模式加密
func Des3ECBEncrypt(data, key string) (string, error) {
    res, err := des3.ECBEncrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return function.Base64Encode(string(res)), nil
}

// Des3ECBDecrypt DES3的ECB模式解密
func Des3ECBDecrypt(data, key string) (string, error) {
    data = function.Base64Decode(data)
    res, err := des3.ECBDecrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return string(res), nil
}
二、特别注意
1、实现php的加解密方式 openssl_encrypt($data, 'DES-EDE', $key)
package test

import (
    "github.com/daimayun/go/function"
    "github.com/daimayun/go/openssl"
    "github.com/daimayun/go/openssl/des3"
)

// Des3ECBEncrypt DES3的ECB模式加密
func Des3ECBEncrypt(data, key string) (string, error) {
    res, err := des3.ECBEncrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return function.Base64Encode(string(res)), nil
}

// Des3ECBDecrypt DES3的ECB模式解密
func Des3ECBDecrypt(data, key string) (string, error) {
    data = function.Base64Decode(data)
    res, err := des3.ECBDecrypt([]byte(data), []byte(key), openssl.PKCS7)
    if err != nil {
        return "", err
    }
    return string(res), nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CBCDecrypt added in v0.9.9

func CBCDecrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

CBCDecrypt CBC模式的解密

func CBCEncrypt added in v0.9.9

func CBCEncrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

CBCEncrypt CBC模式的加密

func CFBDecrypt added in v0.9.9

func CFBDecrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

CFBDecrypt CFB模式的解密

func CFBEncrypt added in v0.9.9

func CFBEncrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

CFBEncrypt CFB模式的加密

func CTRDecrypt added in v0.9.9

func CTRDecrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

CTRDecrypt CTR模式的解密

func CTREncrypt added in v0.9.9

func CTREncrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

CTREncrypt CTR模式的加密

func ECBDecrypt added in v0.9.9

func ECBDecrypt(block cipher.Block, src []byte, padding PaddingType) ([]byte, error)

ECBDecrypt ECB模式的解密

func ECBEncrypt added in v0.9.9

func ECBEncrypt(block cipher.Block, src []byte, padding PaddingType) ([]byte, error)

ECBEncrypt ECB模式的加密

func NewECBDecrypt added in v0.9.9

func NewECBDecrypt(b cipher.Block) cipher.BlockMode

NewECBDecrypt returns a BlockMode which decrypts in electronic code book mode, using the given Block.

func NewECBEncrypt added in v0.9.9

func NewECBEncrypt(b cipher.Block) cipher.BlockMode

NewECBEncrypt returns a BlockMode which encrypts in electronic code book mode, using the given Block.

func OFBDecrypt added in v0.9.9

func OFBDecrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

OFBDecrypt OFB模式的解密

func OFBEncrypt added in v0.9.9

func OFBEncrypt(block cipher.Block, data, iv []byte, padding PaddingType) ([]byte, error)

OFBEncrypt OFB模式的加密

func PKCS5Padding added in v0.9.9

func PKCS5Padding(src []byte, blockSize int) []byte

func PKCS5UnPadding added in v0.9.9

func PKCS5UnPadding(src []byte) ([]byte, error)

func PKCS7Padding added in v0.9.9

func PKCS7Padding(src []byte, blockSize int) []byte

func PKCS7UnPadding added in v0.9.9

func PKCS7UnPadding(src []byte) ([]byte, error)

func Padding added in v0.9.9

func Padding(padding PaddingType, src []byte, blockSize int) []byte

func UnPadding added in v0.9.9

func UnPadding(padding PaddingType, src []byte) ([]byte, error)

func ZerosPadding added in v0.9.9

func ZerosPadding(src []byte, blockSize int) []byte

func ZerosUnPadding added in v0.9.9

func ZerosUnPadding(src []byte) ([]byte, error)

Types

type PaddingType added in v0.9.9

type PaddingType string

PaddingType 填充类型

var (
	PKCS5 PaddingType = "PKCS5"
	PKCS7 PaddingType = "PKCS7"
	Zero  PaddingType = "Zero"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL