← 返回列表

[其他原创] Bastion 棱堡,对称加密算法库,支持CTR、CBC、XTS、AEAD、HASH、HMAC、HKDF、PBKDF2

单站 52pojie.cn 入库 2026-11-18T09:31:14

| 查看: 3753|回复: 61 | 上一主题 下一主题 收起左侧 [其他原创] Bastion 棱堡,对称加密算法库,支持CTR、CBC、XTS、AEAD、HASH、HMAC、HKDF、PBKDF2 评价指数 1 [复制链接] |

| | 跳转到指定楼层 楼主 发表于 2026-10-6 17:54 回帖奖励 免费评分 参与人数 14吾爱币 +13热心值 +13收起 理由 zhaoyf18+ 1 + 1 我很赞同! ts73033+ 1 + 1 我很赞同! fengbolee+ 1 + 1 热心回复! gz7uuuuuuuuu+ 1 + 1 热心回复! Caisan+ 1 + 1 热心回复! Conight+ 1 + 1 我很赞同! ioyr5995+ 1 + 1 热心回复! anoliy+ 1 + 1 鼓励转贴优秀软件安全工具和文档! c3ecbb8d+ 1 谢谢@Thanks! AIways+ 1 + 1 再接再厉 allspark+ 1 + 1 用心讨论,共获提升! GDExecW+ 1 热心回复! 何西烛+ 1 + 1 用心讨论,共获提升! cfgrpg+ 1 + 1 鼓励转贴优秀软件安全工具和文档! 查看全部评分 |
| | 分享到朋友圈 发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |

| 本帖最后由 风之暇想 于 2026-10-6 18:00 编辑 |

Bastion 棱堡是基于 SPN(代换-置换网络)结构设计的 256 位分组密码算法库,融合 ARX(Add-Rotate-XOR)扩散组件与非循环仿射变换 S 盒;加密库提供分组密码(16 轮,支持 CTR/CBC/XTS 模式)、AEAD 认证加密(Encrypt-then-MAC)、HASH、HMAC、HKDF 密钥派生、PBKDF2 密钥派生、DeriveKey 密钥派生的密码学功能。

使用示例:

复制代码 隐藏代码

#include "BastionLib/Bastion.h"
#include <windows.h>
#include <bcrypt.h>
#include <cstring>
#include <cstdlib>
#include <vector>
// 用系统安全随机源填充缓冲区(BCryptGenRandom)
void random_bytes(uint8_t* buf, size_t len) {
    NTSTATUS st = BCryptGenRandom(nullptr, buf, (ULONG)len,
                                  BCRYPT_USE_SYSTEM_PREFERRED_RNG);
    if (st != 0) abort(); // 系统随机源失败直接终止,避免用弱随机数
}
int main() {
    // 准备密钥和 Nonce(调用系统密码学安全随机源生成)
    uint8_t key[32];
    random_bytes(key, sizeof(key));
    uint8_t nonce[Bastion::NONCE_SIZE]; // 24字节
    random_bytes(nonce, sizeof(nonce));
    // ===== 1. 分组密码 - 单块加密/解密 =====
    uint8_t blockPlain[32] = { /* 32字节明文块 */ };
    uint8_t blockCipher[32];
    Bastion_BlockEncrypt(blockPlain, key, 32, blockCipher);
    uint8_t blockDecrypted[32];
    Bastion_BlockDecrypt(blockCipher, key, 32, blockDecrypted);
    // ===== 2. CTR流加密(加解密同一操作) =====
    std::vector<uint8_t> data = {'H', 'e', 'l', 'l', 'o'};
    std::vector<uint8_t> ctrOut(data.size());
    Bastion_CTRCryptBuffer(key, 32, nonce, 24, data.data(), data.size(), ctrOut.data());
    // ctrOut为密文,再调用一次即还原明文
    Bastion_CTRCryptBuffer(key, 32, nonce, 24, ctrOut.data(), ctrOut.size(), data.data());
    // ===== 3. CBC模式(PKCS#7填充) =====
    uint8_t iv[32];
    random_bytes(iv, sizeof(iv));
    std::vector<uint8_t> cbcOut(data.size() + 32);
    int cbcOutLen = (int)cbcOut.size();
    Bastion_CBCEncryptBuffer(key, 32, iv, 32, data.data(), data.size(), cbcOut.data(), &cbcOutLen);
    std::vector<uint8_t> cbcPlain(cbcOutLen);
    int cbcPlainLen = (int)cbcPlain.size();
    Bastion_CBCDecryptBuffer(key, 32, iv, 32, cbcOut.data(), cbcOutLen, cbcPlain.data(), &cbcPlainLen);
    // ===== 4. XTS模式(磁盘加密,支持密文窃取) =====
    uint8_t key2[32];
    random_bytes(key2, sizeof(key2));
    std::vector<uint8_t> xtsCipher(data.size());
    Bastion_XTSEncrypt(key, 32, key2, 32, 0, data.data(), data.size(), xtsCipher.data());
    std::vector<uint8_t> xtsPlain(data.size());
    Bastion_XTSDecrypt(key, 32, key2, 32, 0, xtsCipher.data(), xtsCipher.size(), xtsPlain.data());
    // ===== 5. AEAD认证加密(输出 ciphertext || tag) =====
    const uint8_t ad[] = "additional-data";
    uint8_t aeadOut[256];
    int aeadOutLen = sizeof(aeadOut);
    Bastion_AEADSeal(key, 32, nonce, 24, ad, sizeof(ad) - 1,
                     data.data(), data.size(), aeadOut, &aeadOutLen);
    uint8_t aeadPlain[256];
    int aeadPlainLen = sizeof(aeadPlain);
    Bastion_AEADOpen(key, 32, nonce, 24, ad, sizeof(ad) - 1,
                     aeadOut, aeadOutLen, aeadPlain, &aeadPlainLen);
    // ===== 6. HASH =====
    const char* message = "Hello, Bastion!";
    uint8_t hash[32];
    Bastion_Hash((const uint8_t*)message, strlen(message), hash);
    // ===== 7. HMAC =====
    uint8_t hmacKey[] = "secret-key";
    uint8_t hmacOut[32];
    Bastion_HMAC(hmacKey, sizeof(hmacKey) - 1,
                 (const uint8_t*)message, strlen(message), hmacOut);
    // ===== 8. HKDF密钥派生 =====
    uint8_t salt[32] = { /* 盐值 */ };
    uint8_t info[] = "my-app";
    uint8_t derivedKey[32];
    Bastion_HKDF(key, 32, salt, 32, info, sizeof(info) - 1, 32, derivedKey);
    // ===== 9. PBKDF2密钥派生 =====
    const char* password = "user-password";
    uint8_t pbkdf2Salt[] = "random-salt";
    uint8_t keyFromPassword[32];
    Bastion_PBKDF2((const uint8_t*)password, strlen(password),
                   pbkdf2Salt, sizeof(pbkdf2Salt) - 1,
                   10000, 32, keyFromPassword);
    // ===== 10. 密码学安全随机数(系统安全随机源) =====
    uint8_t randomBytes[64];
    random_bytes(randomBytes, sizeof(randomBytes));
    return 0;
}

复制代码 隐藏代码

package main
import (
    "bytes"
    "crypto/rand"
    "fmt"
    bastion "bastion/bastion"
)
// 从系统安全随机源取 n 字节,失败直接终止
func mustRandom(n int) []byte {
    b := make([]byte, n)
    if _, err := rand.Read(b); err != nil {
        panic(err)
    }
    return b
}
func main() {
    // 准备密钥和Nonce(调用系统安全随机源)
    key := mustRandom(32)
    nonce := mustRandom(24) // 24字节
    // ===== 1. 分组密码 - 单块加密/解密 =====
    blockPlain := make([]byte, 32)
    c, _ := bastion.NewCipher(key)
    blockCipher := make([]byte, 32)
    c.Encrypt(blockCipher, blockPlain)
    blockDecrypted := make([]byte, 32)
    c.Decrypt(blockDecrypted, blockCipher)
    fmt.Printf("Block decrypted match: %v\n", bytes.Equal(blockPlain, blockDecrypted))
    // ===== 2. CTR流加密(加解密同一操作) =====
    plaintext := []byte("Secret message")
    ciphertext, _ := bastion.CTREncrypt(key, nonce, plaintext)
    decrypted, _ := bastion.CTRDecrypt(key, nonce, ciphertext)
    fmt.Printf("Decrypted: %s\n", decrypted)
    // ===== 3. CBC模式(PKCS#7填充) =====
    iv := mustRandom(32)
    cbcCiphertext, _ := bastion.CBCEncryptBytes(key, iv, plaintext)
    cbcDecrypted, _ := bastion.CBCDecryptBytes(key, iv, cbcCiphertext)
    fmt.Printf("CBC Decrypted: %s\n", cbcDecrypted)
    // ===== 4. XTS模式(key为64字节:前32字节key1 + 后32字节key2) =====
    xtsKey := mustRandom(64)
    xtsCiphertext, _ := bastion.XTSEncrypt(xtsKey, plaintext, 0)
    xtsDecrypted, _ := bastion.XTSDecrypt(xtsKey, xtsCiphertext, 0)
    fmt.Printf("XTS Decrypted: %s\n", xtsDecrypted)
    // ===== 5. AEAD认证加密(输出 ciphertext || tag) =====
    aad := []byte("context info")
    sealed, _ := bastion.GCMSeal(key, nonce, plaintext, aad)
    opened, _ := bastion.GCMOpen(key, nonce, sealed, aad)
    fmt.Printf("AEAD Decrypted: %s\n", opened)
    // ===== 6. HASH =====
    message := []byte("Hello, Bastion!")
    hash := bastion.Sum(message)
    fmt.Printf("Hash: %x\n", hash)
    // ===== 7. HMAC =====
    hmacKey := []byte("secret-key")
    hmacResult := bastion.Compute(hmacKey, message)
    fmt.Printf("HMAC: %x\n", hmacResult)
    // ===== 8. HKDF密钥派生 =====
    salt := []byte("random-salt")
    info := []byte("my-app")
    derivedKey, _ := bastion.HKDF(key, salt, info, 32)
    fmt.Printf("Derived Key: %x\n", derivedKey)
    // ===== 9. PBKDF2密钥派生 =====
    password := []byte("user-password")
    pbkdf2Salt := []byte("random-salt")
    keyFromPassword, _ := bastion.PBKDF2(password, pbkdf2Salt, 10000, 32)
    fmt.Printf("Key from password: %x\n", keyFromPassword)
    // ===== 10. DeriveKey密钥派生(BLAKE3风格) =====
    derived := bastion.DeriveKey("my-app", key, 32)
    fmt.Printf("DeriveKey: %x\n", derived)
    // ===== 11. 密码学安全随机数(系统安全随机源) =====
    randomBytes := mustRandom(64)
    fmt.Printf("Random bytes: %x\n", randomBytes)
}

https://fzxx.github.io/Bastion/

CDN引用

复制代码 隐藏代码
<script src="https://cdn.jsdelivr.net/gh/fzxx/Bastion/JS/Bastion.js"></script> 复制代码 隐藏代码
<script src="https://cdn.statically.io/gh/fzxx/Bastion@main/JS/Bastion.js"></script> 复制代码 隐藏代码

// 浏览器环境
// <script src="Bastion.js"></script>
// Node.js环境
// const Bastion = require('./Bastion.js');
// const crypto = require('crypto').webcrypto; // Node.js 19以下无全局crypto,需这样显式引入
// 准备密钥和Nonce
const key = crypto.getRandomValues(new Uint8Array(32));
const nonce = crypto.getRandomValues(new Uint8Array(24));
// ===== 1. 分组密码 - 单块加密/解密 =====
const blockPlain = new Uint8Array(32); // 32字节明文块
const blockCipher = Bastion.blockEncrypt(key, blockPlain);
const blockDecrypted = Bastion.blockDecrypt(key, blockCipher);
// ===== 2. CTR流加密(加解密同一操作) =====
const plaintext = new TextEncoder().encode("Secret message");
const ciphertext = Bastion.ctrCrypt(key, nonce, plaintext);
const decrypted = Bastion.ctrCrypt(key, nonce, ciphertext);
console.log("Decrypted:", new TextDecoder().decode(decrypted));
// ===== 3. CBC模式(PKCS#7填充) =====
const iv = crypto.getRandomValues(new Uint8Array(32));
const cbcCiphertext = Bastion.cbcEncrypt(key, iv, plaintext);
const cbcDecrypted = Bastion.cbcDecrypt(key, iv, cbcCiphertext);
console.log("CBC Decrypted:", new TextDecoder().decode(cbcDecrypted));
// ===== 4. XTS模式(磁盘加密,支持密文窃取) =====
const key1 = crypto.getRandomValues(new Uint8Array(32)); // 数据加密密钥
const key2 = crypto.getRandomValues(new Uint8Array(32)); // Tweak密钥
const xtsCiphertext = Bastion.xtsEncrypt(key1, key2, 0, plaintext);
const xtsDecrypted = Bastion.xtsDecrypt(key1, key2, 0, xtsCiphertext);
console.log("XTS Decrypted:", new TextDecoder().decode(xtsDecrypted));
// ===== 5. AEAD认证加密(输出 ciphertext || tag) =====
const ad = new TextEncoder().encode("context info");
const aeadCiphertext = Bastion.aeadEncrypt(key, nonce, plaintext, ad);
const aeadDecrypted = Bastion.aeadDecrypt(key, nonce, aeadCiphertext, ad);
console.log("AEAD Decrypted:", new TextDecoder().decode(aeadDecrypted));
// ===== 6. HASH =====
const message = new TextEncoder().encode("Hello, Bastion!");
const hash = Bastion.hash(message);
console.log("Hash:", Array.from(hash).map(b => b.toString(16).padStart(2, '0')).join(''));
// ===== 7. HMAC =====
const hmacKey = new TextEncoder().encode("secret-key");
const hmacResult = Bastion.hmac(hmacKey, message);
console.log("HMAC:", Array.from(hmacResult).map(b => b.toString(16).padStart(2, '0')).join(''));
// ===== 8. HKDF密钥派生 =====
const salt = new TextEncoder().encode("random-salt");
const info = new TextEncoder().encode("my-app");
const derivedKey = Bastion.hkdf(key, salt, info, 32);
console.log("Derived Key:", Array.from(derivedKey).map(b => b.toString(16).padStart(2, '0')).join(''));
// ===== 9. PBKDF2密钥派生 =====
const password = new TextEncoder().encode("user-password");
const pbkdf2Salt = new TextEncoder().encode("random-salt");
const keyFromPassword = Bastion.pbkdf2(password, pbkdf2Salt, 10000, 32);
console.log("Key from password:", Array.from(keyFromPassword).map(b => b.toString(16).padStart(2, '0')).join(''));
// ===== 10. DeriveKey密钥派生(BLAKE3风格) =====
const derived = Bastion.deriveKey("my-app", key, 32);
console.log("DeriveKey:", Array.from(derived).map(b => b.toString(16).padStart(2, '0')).join(''));
// ===== 11. 密码学安全随机数(Web Crypto 标准随机源) =====
const randomBytes = crypto.getRandomValues(new Uint8Array(64));
console.log("Random bytes:", Array.from(randomBytes).map(b => b.toString(16).padStart(2, '0')).join(''));

复制代码 隐藏代码

use std::io::Cursor;
use getrandom::getrandom; // 系统安全随机源
fn main() -> std::io::Result<()> {
    // 准备密钥和Nonce(调用系统密码学安全随机数)
    let mut key = [0u8; bastion::KEY_SIZE];     // 32字节
    getrandom(&mut key).expect("RNG 失败");
    let mut nonce = [0u8; bastion::NONCE_SIZE]; // 24字节,CTR/AEAD要求严格24字节
    getrandom(&mut nonce).expect("RNG 失败");
    // ===== 1. 分组密码 - 单块加密/解密 =====
    let block_plain = [7u8; 32]; // 32字节明文块
    let cipher = bastion::BlockCipher::new(&key);
    let block_cipher = cipher.encrypt_block(&block_plain);
    let block_decrypted = cipher.decrypt_block(&block_cipher);
    assert_eq!(block_plain, block_decrypted);
    // ===== 2. CTR流加密(加解密同一操作,输入输出走流式) =====
    let plaintext = b"Secret message";
    let mut ciphertext = Vec::new();
    bastion::ctr_crypt(&key, &nonce, &mut Cursor::new(plaintext), &mut ciphertext)?;
    let mut decrypted = Vec::new();
    bastion::ctr_crypt(&key, &nonce, &mut Cursor::new(&ciphertext[..]), &mut decrypted)?;
    assert_eq!(plaintext, &decrypted[..]);
    // ===== 3. CBC模式(PKCS#7填充,IV固定32字节) =====
    let mut iv = [0u8; bastion::BLOCK_SIZE];
    getrandom(&mut iv).expect("RNG 失败");
    let mut cbc_ciphertext = Vec::new();
    bastion::cbc_encrypt(&key, &iv, &mut Cursor::new(plaintext), &mut cbc_ciphertext)?;
    let mut cbc_decrypted = Vec::new();
    bastion::cbc_decrypt(&key, &iv, &mut Cursor::new(&cbc_ciphertext[..]), &mut cbc_decrypted)?;
    assert_eq!(plaintext, &cbc_decrypted[..]);
    // ===== 4. XTS模式(磁盘加密,key为64字节:前32字节key1 + 后32字节key2,数据至少一个完整块32字节) =====
    let mut xts_key = [0u8; 64];
    getrandom(&mut xts_key).expect("RNG 失败");
    let sector_data = [42u8; 64];
    let xts_ciphertext = bastion::xts_encrypt(&xts_key, §or_data, 0);
    let xts_decrypted = bastion::xts_decrypt(&xts_key, &xts_ciphertext, 0);
    assert_eq!(sector_data, &xts_decrypted[..]);
    // ===== 5. AEAD认证加密(输出 密文 || tag,tag固定16字节) =====
    let aad = b"context info";
    let mut sealed = Vec::new();
    bastion::aead_seal(&key, &nonce, &mut Cursor::new(plaintext), &mut sealed, aad)?;
    let mut opened = Vec::new();
    bastion::aead_open(&key, &nonce, &mut Cursor::new(&sealed[..]), &mut opened, aad)?;
    assert_eq!(plaintext, &opened[..]);
    // ===== 6. HASH =====
    let message = b"Hello, Bastion!";
    let hash = bastion::hash(&mut Cursor::new(message))?;
    // ===== 7. HMAC =====
    let hmac_key = b"secret-key";
    let hmac_result = bastion::hmac(hmac_key, &mut Cursor::new(message))?;
    // ===== 8. HKDF密钥派生 =====
    let salt = b"random-salt";
    let info = b"my-app";
    let derived_key = bastion::hkdf(&key, salt, info, 32);
    // ===== 9. PBKDF2密钥派生 =====
    let password = b"user-password";
    let pbkdf2_salt = b"random-salt";
    let key_from_password = bastion::pbkdf2(password, pbkdf2_salt, 10000, 32);
    // ===== 10. DeriveKey密钥派生(BLAKE3风格) =====
    let derived = bastion::derive_key("my-app", &key, 32);
    // ===== 11. 密码学安全随机数(系统安全随机源) =====
    let mut random_bytes = [0u8; 64];
    getrandom(&mut random_bytes).expect("RNG 失败");
    Ok(())
}

看看有没有人能破解出明文、密钥或者篡改认证标签

Nonce

复制代码 隐藏代码
30c9947b9a7a890097cb5d5db091535c3fa42e5862c36ea2

密文

复制代码 隐藏代码
ce09131150fc8d14580499c6379b0c75e994b73a78d684869ba79cec57cf519ab41acb25201d3b14d5ab83f9f8cdf3b1fbc70e5060de05bbcf92f8398eb8e1540e743c3af05c9c

复制代码 隐藏代码
f6b6960862c55aefb45ed839d225ef63179c60b76c3b3494

复制代码 隐藏代码
fb3b913fed909b2195a028575cb108db8dbc78994f4be5129ded6a3624552f00fb7fba76c125e4245c85454a64caa1b7120244f80ce7234d2d5a77ec9606d81766c79a0dd1723719329cce0b1dbd93633d43ef11707dd5e4509fc4462482cb3b9fc89a94faebeba482fe40cb62b67ab131056e243649f7178ed717f891251b24cd93f3f93ff476dbc5c6ce3b58e1f1019a1e2a4911f13e89bb912cec9a5e1126e26fb6b2e9001629034646b78e7201520d6fc942eb5bd69e150d281eb05b17e2070869aaa1177133c9133e762b2a70fd229ed21e7e82ed3f6950069bd8

| 头像被屏蔽 | 推荐 发表于 2026-10-7 11:48 免费评分 参与人数 1吾爱币 -15违规 +1收起 理由 风之暇想-15 + 1 请勿灌水,提高帖子质量是每位会员应尽的义务! 查看全部评分 |

| 工程量确实大,四种语言实现加完整的模式矩阵,respect。两点看法:一,自研分组密码最难的不是实现而是密码分析。16轮SPN配ARX扩散听起来合理,但在公开差分/线性分析数据之前,实际强度只能算未知。挑战向量是好开始,更有说服力的是可复现的分析,比如S盒的差分均匀度、非线性度,以及reduced-round攻击实验。二,XTS带密文窃取、AEAD用Encrypt-then-MAC,模式选型到位;但真上生产环境,建议优先用经过多年分析的标准算法。另请教:密钥扩展怎么做?示例直接传32字节密钥,是轮密钥还是经过密钥编排?有没有KAT测试向量,方便交叉验证各语言实现的一致性? |

| | 推荐 发表于 2026-10-29 23:06 |

| 本帖最后由 Kolomina 于 2026-10-29 23:30 编辑 |

漏洞 分类 「正确调用就安全」?
V-1 padding oracle 模式/误用 是(加 ETM 即消除)
V-2 CBC 无 MAC 模式/误用 是(加 ETM 即消除)
V-3 nonce 复用 模式/误用 是(随机 nonce + 不复用)
V-4 流式丢尾 实现缺陷 否(代码漏了 carry!=0 检查)
V-5 AEAD 无 outCap 实现缺陷 否(API 设计无法校验)
V-6 密钥扩展非单射 算法缺陷 否(模加映射碰撞)
V-7 失败不清零 实现缺陷 部分(调用方检查 RC 可避免,但库应主动清零)
V-8 无安全下界 算法缺陷 否
V-9 错误文本区分 模式/误用 是(不透出异常文本)

| | 沙发 发表于 2026-10-6 17:59 |

| 成熟加密算法几乎不可能篡改出合法的认证签吧 |

| | 3# 发表于 2026-10-6 18:50 |

| 总版主的开源地址,总得去Star吧 |

| | 4# 发表于 2026-10-6 19:01 |

| Bastion 棱堡,对称加密算法库 |

| | 5# 发表于 2026-10-6 19:03 |

| 感谢分享Bastion 棱堡,对称加密算法库,支持CTR、CBC、XTS、AEAD、HASH、HMAC、HKDF、PBKDF2 |

| | 6# 发表于 2026-10-6 19:08 |

| 下载保存了,感谢分享 |

| | 7# 发表于 2026-10-6 19:12 |

| 谢谢分享 |

| | 8# 发表于 2026-10-6 19:15 |

| 速度对比呢? |

| | 9# 发表于 2026-10-6 19:24 |

| 瞧瞧瞧瞧瞧瞧瞧瞧 |

| | 10# 发表于 2026-10-6 19:32 |

| 感谢分享Bastion 棱堡,我是小白,还需要多学习。 |

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn

GMT+8, 2026-11-18 07:54

Powered by Discuz!

原文链接:https://www.52pojie.cn/thread-2131143-1-1.html