search
HomeBackend DevelopmentGolangCan golang code be encrypted?
Can golang code be encrypted?Dec 28, 2019 pm 02:38 PM
golang

Can golang code be encrypted?

Golang code encryption method:

DES encryption and decryption

The standard library in golang is in crypto/des There is an implementation of DES, but the description of the golang library is relatively simple. If you are not familiar with the encryption rules of DES, it is not easy to write the corresponding code. It is also easy to get confused when performing encryption and decryption between different languages ​​with a third party. ,An error occurred.

When different platforms and languages ​​are connected for DES encryption and decryption, you need to know what encryption mode and filling method the other party uses:

Windows default is CBC mode, CryptSetKeyParam function, openssl The function name directly indicates

In Java, if Cipher.getInstance() is not filled in, the default is DES/ECB/PKCS5Padding

The default in C# is CBC mode, PKCS7Padding(PKCS5Padding)

Golang provides CBC mode by default, so for ECB mode, you need to write your own code

PKCS5Padding and PKCS5Unpadding

    func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
        padding := blockSize - len(ciphertext)%blockSize
        padtext := bytes.Repeat([]byte{byte(padding)}, padding)
        return append(ciphertext, padtext...)
    }
 
    func PKCS5Unpadding(origData []byte) []byte {
        length := len(origData)
        unpadding := int(origData[length-1])
        return origData[:(length - unpadding)]
    }

ECB encryption mode

 
        block, err := des.NewCipher(key)
        if err != nil {
            ...
        }
        bs := block.BlockSize()
        src = PKCS5Padding(src, bs)
        if len(src)%bs != 0 {
            ....
        }
        out := make([]byte, len(src))
        dst := out
        for len(src) > 0 {
            block.Encrypt(dst, src[:bs])
            src = src[bs:]
            dst = dst[bs:]
        }
        ...
    }

RSA Encryption and decryption

Different from other languages ​​that have higher-level encapsulation by default, golang needs to be combined and encapsulated based on different concepts

PEM: usually ends with .pem Files are commonly used in key storage and X.509 certificate systems. The following is the PEM format under an X509 certificate:

-----BEGIN CERTIFICATE-----
    base64
-----END CERTIFICATE-----

PKCS: This is a huge system, and different keys use different pkcs file format. For example, the private key uses pkcs8.

X.509: This is a public key infrastructure (pki), which usually corresponds to PKIX in the IETF.

Note:

The pem file generated using openssl (such as openssl genrsa -out rsa_private_key.pem 1024) conforms to the PEM format, with -----BEGIN RSA PRIVATE KEY-- ---Beginning, -----END RSA PRIVATE KEY-----End.

It can also be converted to pkcs8:

openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt

After clarifying the above concepts and formats, it is relatively easy to write the public key and private key encryption and decryption methods corresponding to golang. The first is to Decode the pem file, then decode the corresponding password into a structure supported by golang, and then perform corresponding processing.

For private keys, you can perform the following operations to sign:

    block, _ := pem.Decode([]byte(key))
    if block == nil {       // 失败情况
        ....
    }
 
    private, err := x509.ParsePKCS8PrivateKey(block.Bytes)
    if err != nil {
        ...
    }
 
    h := crypto.Hash.New(crypto.SHA1)
    h.Write(data)
    hashed := h.Sum(nil)
 
    // 进行rsa加密签名
    signedData, err := rsa.SignPKCS1v15(rand.Reader, private.(*rsa.PrivateKey), crypto.SHA1, hashed)
    ...

For more golang knowledge, please pay attention to the

golang tutorial column.

The above is the detailed content of Can golang code be encrypted?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
go语言有没有缩进go语言有没有缩进Dec 01, 2022 pm 06:54 PM

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

聊聊Golang中的几种常用基本数据类型聊聊Golang中的几种常用基本数据类型Jun 30, 2022 am 11:34 AM

本篇文章带大家了解一下golang 的几种常用的基本数据类型,如整型,浮点型,字符,字符串,布尔型等,并介绍了一些常用的类型转换操作。

go语言为什么叫gogo语言为什么叫goNov 28, 2022 pm 06:19 PM

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

一文详解Go中的并发【20 张动图演示】一文详解Go中的并发【20 张动图演示】Sep 08, 2022 am 10:48 AM

Go语言中各种并发模式看起来是怎样的?下面本篇文章就通过20 张动图为你演示 Go 并发,希望对大家有所帮助!

tidb是go语言么tidb是go语言么Dec 02, 2022 pm 06:24 PM

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

聊聊Golang自带的HttpClient超时机制聊聊Golang自带的HttpClient超时机制Nov 18, 2022 pm 08:25 PM

​在写 Go 的过程中经常对比这两种语言的特性,踩了不少坑,也发现了不少有意思的地方,下面本篇就来聊聊 Go 自带的 HttpClient 的超时机制,希望对大家有所帮助。

go语言是否需要编译go语言是否需要编译Dec 01, 2022 pm 07:06 PM

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

golang map怎么删除元素golang map怎么删除元素Dec 08, 2022 pm 06:26 PM

删除map元素的两种方法:1、使用delete()函数从map中删除指定键值对,语法“delete(map, 键名)”;2、重新创建一个新的map对象,可以清空map中的所有元素,语法“var mapname map[keytype]valuetype”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),