國(guó)密即國(guó)家密碼局認(rèn)定的國(guó)產(chǎn)密碼算法。主要有SM1,SM2,SM3,SM4。密鑰長(zhǎng)度和分組長(zhǎng)度均為128位。
SM1 為對(duì)稱加密。其加密強(qiáng)度與AES相當(dāng)。該算法不公開,調(diào)用該算法時(shí),需要通過加密芯片的接口進(jìn)行調(diào)用。
SM2為非對(duì)稱加密,基于ECC。該算法已公開。由于該算法基于ECC,故其簽名速度與秘鑰生成速度都快于RSA。ECC 256位(SM2采用的就是ECC 256位的一種)安全強(qiáng)度比RSA 2048位高,但運(yùn)算速度快于RSA。
國(guó)家密碼管理局公布的公鑰算法,其加密強(qiáng)度為256位
SM3 消息摘要??梢杂肕D5作為對(duì)比理解。該算法已公開。校驗(yàn)結(jié)果為256位。
SM4 無線局域網(wǎng)標(biāo)準(zhǔn)的分組數(shù)據(jù)算法。對(duì)稱加密,密鑰長(zhǎng)度和分組長(zhǎng)度均為128位。
由于SM1、SM4加解密的分組大小為128bit,故對(duì)消息進(jìn)行加解密時(shí),若消息長(zhǎng)度過長(zhǎng),需要進(jìn)行分組,要消息長(zhǎng)度不足,則要進(jìn)行填充。
國(guó)際算法與國(guó)密算法分類
分組密碼算法(DES和SM4)、將明文數(shù)據(jù)按固定長(zhǎng)度進(jìn)行分組,然后在同一密鑰控制下逐組進(jìn)行加密,
公鑰密碼算法(RSA和SM2)、公開加密算法本身和公開公鑰,保存私鑰
摘要算法(SM3 md5) 這個(gè)都比較熟悉,用于數(shù)字簽名,消息認(rèn)證,數(shù)據(jù)完整性,但是sm3安全度比md5高
總得來說國(guó)密算法的安全度比較高,2010年12月推出,也是國(guó)家安全戰(zhàn)略,現(xiàn)在銀行都要要求國(guó)際算法改造,要把國(guó)際算法都給去掉
代碼實(shí)現(xiàn)
C 語言實(shí)現(xiàn)
https://github.com/guanzhi/GmSSL/
Go 語言
https://github.com/tjfoc/gmsm
https://github.com/ZZMarquis/gm
Java 語言
https://github.com/PopezLotado/SM2Java
測(cè)試
Go語言實(shí)現(xiàn),調(diào)用 gmsm
package main_test
import (
"bytes"
"crypto/cipher"
"fmt"
"github.com/tjfoc/gmsm/sm2"
"github.com/tjfoc/gmsm/sm3"
"github.com/tjfoc/gmsm/sm4"
"log"
"testing"
)
func Test_SM3(t *testing.T) {
date := "hello world"
s:= sm3.New()
s.Write([]byte(date))
sum := s.Sum(nil)
fmt.Println(sum)
fmt.Printf("%x\n",sum)
}
func Test_SM4(t *testing.T) {
// SM4私鑰只支持16bit
key := []byte("helloworldhellow")
iv := make([]byte, sm4.BlockSize)
data := []byte("Tongji Fintech Research Institute")
ciphertxt,err := sm4Encrypt(key,iv, data)
if err != nil{
log.Fatal(err)
}
fmt.Printf("加密結(jié)果: %x\n", ciphertxt)
fmt.Println(ciphertxt,len(ciphertxt))
data,err = sm4Decrypt(key,iv,ciphertxt)
if err != nil{
log.Fatal(err)
}
fmt.Printf("加密結(jié)果: %s\n", data)
}
func sm4Encrypt(key, iv, plainText []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData := pkcs5Padding(plainText, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv)
cryted := make([]byte, len(origData))
blockMode.CryptBlocks(cryted, origData)
return cryted, nil
}
func sm4Decrypt(key, iv, cipherText []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(cipherText))
blockMode.CryptBlocks(origData, cipherText)
origData = pkcs5UnPadding(origData)
return origData, nil
}
// pkcs5填充
func pkcs5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
func pkcs5UnPadding(src []byte) []byte {
length := len(src)
if(length==0){
return nil
}
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}
func Test_SM2(t *testing.T) {
priv, err := sm2.GenerateKey() // 生成密鑰對(duì)
if err != nil {
log.Fatal(err)
}
msg := []byte("Tongji Fintech Research Institute")
pub := &priv.PublicKey
ciphertxt, err := pub.Encrypt(msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("加密結(jié)果:%x\n",ciphertxt)
plaintxt,err := priv.Decrypt(ciphertxt)
if err != nil {
log.Fatal(err)
}
if !bytes.Equal(msg,plaintxt){
log.Fatal("原文不匹配")
}
r,s,err := sm2.Sign(priv, msg)
if err != nil {
log.Fatal(err)
}
isok := sm2.Verify(pub,msg,r,s)
fmt.Printf("Verified: %v\n", isok)
}