海运的博客

php openssl chacha20加密

发布时间:May 3, 2020 // 分类: // No Comments

ietf版chacha20 nonce为12字节,openssl_encrypt传递iv参数最低16字节,将nonce前面以\0补充到16字节。

<?php
$key = hash('sha256', "pass", true);
$nonce = random_bytes(12);
$msg = "message";
$cipher_str = openssl_encrypt($msg, 'chacha20', $key, OPENSSL_NO_PADDING, "\0\0\0\0".$nonce);
echo "cipher hex: " . bin2hex($cipher_str) . PHP_EOL;
$plain_str = openssl_decrypt($cipher_str, 'chacha20', $key, OPENSSL_NO_PADDING, "\0\0\0\0".$nonce);
echo "plain text: ".$plain_str.PHP_EOL;

golang XChaCha20/ChaCha20/XChaCha20-Poly1305/ChaCha20-Poly1305加密

发布时间:May 2, 2020 // 分类: // No Comments

ChaCha20和XChaCha20,NewUnauthenticatedCipher传入nonce值为12字节时使用ChaCha20,24字节时使用XChaCha20加密方法:

package main

import (
        "crypto/sha256"
        "fmt"
        "io"
        "crypto/rand"
        //"encoding/hex"
        "golang.org/x/crypto/chacha20"
)

func main() {

        pass := "Hello"
        msg := []byte("Pass")
        //msg, _ := hex.DecodeString("e07a6838")

        key := sha256.Sum256([]byte(pass))

        //nonce := make([]byte, chacha20.NonceSize)
        nonce := make([]byte, chacha20.NonceSizeX)
        if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
                panic(err.Error())
        }

        cip, _ := chacha20.NewUnauthenticatedCipher(key[:], nonce)
        ciphertext := make([]byte, len(msg))
        plaintext  := make([]byte, len(msg))
        cip.XORKeyStream(ciphertext, msg)

        cip2, _ := chacha20.NewUnauthenticatedCipher(key[:], nonce)
        cip2.XORKeyStream(plaintext, ciphertext)

        fmt.Printf("Message:\t%s\n", msg)
        fmt.Printf("Passphrase:\t%s\n", pass)
        fmt.Printf("Key:\t%x\n", key)
        fmt.Printf("Nonce:\t%x\n", nonce)
        fmt.Printf("Cipher stream:\t%x\n", ciphertext)
        fmt.Printf("Plain text:\t%s\n", plaintext)

}

XChaCha20-Poly1305和ChaCha20-Poly1305加密,分别调用NewX和New初始化,nonce同上ChaCha20和XChaCha20的大小。

package main

import (
        "crypto/rand"
        "crypto/sha256"
        "fmt"
        "golang.org/x/crypto/chacha20poly1305"
        "io"
)

func main() {

        pass := "Hello"
        msg := "Pass"

        key := sha256.Sum256([]byte(pass))
        //aead, _ := chacha20poly1305.NewX(key[:])
        aead, _ := chacha20poly1305.New(key[:])

        //nonce := make([]byte, chacha20poly1305.NonceSizeX)
        nonce := make([]byte, chacha20poly1305.NonceSize)
        if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
                panic(err.Error())
        }

        ciphertext := aead.Seal(nil, nonce, []byte(msg), nil)
        plaintext, _ := aead.Open(nil, nonce, ciphertext, nil)

        fmt.Printf("Message:\t%s\n", msg)
        fmt.Printf("Passphrase:\t%s\n", pass)
        fmt.Printf("Key:\t%x\n", key)
        fmt.Printf("Nonce:\t%x\n", nonce)
        fmt.Printf("Cipher stream:\t%x\n", ciphertext)
        fmt.Printf("Plain text:\t%s\n", plaintext)

}
分类
最新文章
最近回复
  • fengfeng: N1 armbian 能有编译下内核吗。。我要开启can 不懂怎么操作
  • 1: 方法一ngtcp2要改下:./configure PKG_CONFIG_PATH=/usr/l...
  • 海运: 关闭服务器
  • 海风: override.battery.charge.low以及override.battery.r...
  • koldjf: 不能过滤
  • 杰迪武士: 此文甚好甚强巨,依照此文在树莓派2 + Rasbian上部署成功 感谢博主美文共赏
  • 海运: ups不知有没选项可设置此参数,不过你可以在另外一台电脑上安装nut客户端自动关机。
  • kgami: 想请教一下,设置了的电脑自动关机之后,几秒后UPS怎么也跟着关机了,导致另外一台电脑没关机就断...
  • 海运: 写的很详细了啊,/etc/nut/hosts.conf用以nut-cgi连接nut服务器参数,...
  • ryan: 请问下nginx配置好了,怎么和这个nut链接呢?最后可视化管理这块能给个详细一点的教程么?谢谢。