Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for SM4 #15

Closed
poundscaoying opened this issue Dec 7, 2017 · 7 comments
Closed

Test for SM4 #15

poundscaoying opened this issue Dec 7, 2017 · 7 comments

Comments

@poundscaoying
Copy link

I have noticed that the data used to encrypt in an4_test.go must be 16 or 16*n bytes long.And I have a try to use Encryption function here to encrypt some data like "Hello World",but it doesn't work with Decryption function here.So,does it work in GM module in bccsp for sm4 now? Thanks in advanced.

@poundscaoying
Copy link
Author

A mistake in writing.It's sm4_test.go .

@nnsgmsone
Copy link
Contributor

nnsgmsone commented Dec 8, 2017

You can use the sm4.Newcipher, Let me give you an example:

 func Sm4Encryption(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 Sm4Decryption(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
}
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)
    unpadding := int(src[length-1])
    return src[:(length - unpadding)]
}
func main() {
    originalText := "sysys"
    fmt.Println(originalText)
    mytext := []byte(originalText)
    key := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    iv := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    cryptoText, _ := Sm4Encryption(key, iv, mytext)
    fmt.Println(string(cryptoText))
    decryptedText, _ := Sm4Decryption(key, iv, cryptoText)
    fmt.Println(string(decryptedText))
}

@tjfoc tjfoc closed this as completed Dec 8, 2017
@tjfoc
Copy link
Owner

tjfoc commented Dec 8, 2017

please leave comments if still have questions/issues. thanks

@poundscaoying
Copy link
Author

I got it, thanks a lot.

@nnsgmsone
Copy link
Contributor

It's okay.

@QuincyWuzefeng
Copy link

nice

@xgswust
Copy link

xgswust commented Feb 18, 2020

the sm4-cbc is different form openssl sm4-cbc, why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants