lofiCafe / logs

0 stars 0 forks source link

sm4 #8

Open lofiCafe opened 3 years ago

lofiCafe commented 3 years ago

https://github.com/tjfoc/gmsm/blob/6f3ec5ee0ff656730d79e8db02a32ad8e5aaaafa/API%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E.md

lofiCafe commented 3 years ago
package main

import (
    "io/ioutil"
    "log"

    "github.com/piligo/gmsm/sm2"
    "github.com/piligo/gmsm/sm4"
)

func generateKeyPair(privateKey string, publicKey string) (bool, error) {
    priv, err := sm2.GenerateKey()
    if err != nil {
        log.Fatalf("func sm2.GenerateKey() failed!\n")
        return false, err
    }
    pub := &priv.PublicKey

    emptyBytes := []byte{0}

    isPrivateKeyPemOk, err := sm2.WritePrivateKeytoPem(privateKey, priv, emptyBytes)
    if !isPrivateKeyPemOk {
        log.Fatalf("WritePrivateKeytoPem failed!\n")
        return false, err
    }

    isPublicKeyPemOk, err := sm2.WritePublicKeytoPem(publicKey, pub, emptyBytes)
    if !isPublicKeyPemOk {
        log.Fatalf("WritePublicKeytoPem failed!\n")
        return false, err
    }

    return true, nil
}

func loadPrivatePemKey(privateKey string) (*sm2.PrivateKey, error) {
    emptyBytes := []byte{0}
    return sm2.ReadPrivateKeyFromPem(privateKey, emptyBytes)
}

func loadPublicPemKey(publicKey string) (*sm2.PublicKey, error) {
    emptyBytes := []byte{0}
    return sm2.ReadPublicKeyFromPem(publicKey, emptyBytes)
}

func encryptDataWithSm2(pub *sm2.PublicKey, plain []byte) (bool, []byte) {
    cipher, err := pub.Encrypt(plain)
    if err != nil {
        log.Fatalf("Encrypt bytes with sm2 public key failed!\n")
        log.Fatalf("error detail! : %v\n", err)
        return false, nil
    }

    return true, cipher
}

func decryptDataWithSm2(priv *sm2.PrivateKey, cipher []byte) (bool, []byte) {
    clear, err := priv.Decrypt(cipher)
    if err != nil {
        log.Fatalf("Decrypt bytes with sm2 private key failed!\n")
        log.Fatalf("error detail! : %v\n", err)
        return false, nil
    }

    return true, clear
}

func encryptFileWithSm4(key []byte, iv []byte, file string) (bool, []byte) {
    // sm4Ecb模式pksc7填充加密

    err := sm4.SetIV(iv)
    if err != nil {
        log.Fatalf("setiv failed! %v\n", err)
        return false, nil
    }

    fbytes, err := ioutil.ReadFile(file)
    if err != nil {
        log.Fatalf("failed to read file %s\n", file)
        return false, nil
    }
    ecbMsg, err := sm4.Sm4Ecb(key, fbytes, true)

    if err != nil {
        log.Fatalf("sm4 encrypt data failed! %v\n", err)
        return false, nil
    }

    return true, ecbMsg

}

func decryptFileWithSm4(key []byte, iv []byte, file string) (bool, []byte) {
    //sm4Ecb模式pksc7填充解密
    err := sm4.SetIV(iv)
    if err != nil {
        log.Fatalf("setiv failed! %v\n", err)
        return false, nil
    }

    fbytes, err := ioutil.ReadFile(file)
    if err != nil {
        log.Fatalf("failed to read file %s\n", file)
        return false, err
    }
    ecbDec, err := sm4.Sm4Ecb(key, fbytes, false)

    if err != nil {
        log.Fatalf("sm4 decrypt data failed! %v\n", err)
        return false, nil
    }

    return true, ecbDec
}
lofiCafe commented 3 years ago
package main

import (
    "fmt"
    "log"
    "os"
)

func main() {
    privateKey := "./sm2_private.pem"
    publicKey := "./sm2_public.pem"

    // 生成SM2密钥对
    genok, err := generateKeyPair(privateKey, publicKey)
    if !genok {
        log.Fatalf("Crypto Manager generate key pair failed! error: %v\n", err)
        os.Exit(-1)
    }

    clearMessage := "Spot market sm2 key tools create by dingnan"
    clearSlice := []byte(clearMessage)

    // load public key from pem key file
    publicSm2Key, err := loadPublicPemKey(publicKey)
    if err != nil {
        log.Fatalf("load public key from file failed! error: %v\n", err)
    }

    privateSm2Key, err := loadPrivatePemKey(privateKey)
    if err != nil {
        log.Fatalf("load private key from file failed! error: %v\n", err)
    }

    // SM2加密
    encOk, cipher := encryptDataWithSm2(publicSm2Key, clearSlice)
    if !encOk {
        log.Fatalf("xxxxx")
    }

    // SM2解密
    decOk, clear := decryptDataWithSm2(privateSm2Key, cipher)
    if !decOk {
        log.Fatalf("decryptDataWithSm2 failed!\n")
    }

    clearMessageStr := string(clear)
    fmt.Println(clearMessageStr)

    // SM4 文件加密

}