使用NodeJS(带Typescript)流和aes-gcm算法时,不支持的状态或无法验证数据错误

时间:2018-01-24 19:28:57

标签: node.js typescript stream cryptography aes-gcm

尝试通过Cipher / Decipher流加密和解密文件时,我总是遇到以下错误:Unsupported state or unable to authenticate data

这是代码(流和非流版本之间的比较):

非流版本

const key = Buffer.alloc(256 / 8);

const text = 'my secret message';
const encrypter = crypto.createCipheriv('aes-256-gcm', key, Buffer.alloc(16));

let encrypted = encrypter.update(text, 'utf8', 'hex');
encrypted += encrypter.final('hex');
const tag = encrypter.getAuthTag();

console.log('Encrypted!', encrypted);

const decrypter = crypto.createDecipheriv('aes-256-gcm', key, Buffer.alloc(16));
decrypter.setAuthTag(tag);
let decrypted = decrypter.update(encrypted, 'hex', 'utf8');
decrypted += decrypter.final('utf8');

console.log('Decrypted', decrypted);

完美打印Decrypted my secret message另一方面......

流版本

const key = Buffer.alloc(256 / 8);
const text = 'my secret message';

const cipher = crypto.createCipheriv('aes-256-gcm', key, Buffer.alloc(16));
let encrypted = '';
cipher.on('data', (data: Buffer) => 
{
    encrypted += data.toString('hex');
});

cipher.on('end', () => 
{
    const tag = cipher.getAuthTag();

    const decipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.alloc(16));
    decipher.setAuthTag(tag);

    let decrypted = '';
    decipher.on('readable', () => 
    {
        const data = decipher.read() as Buffer;
        if(data)
            decrypted += data.toString('utf8');
    });

    decipher.on('end', () => 
    {
        console.log(decrypted);
    });

    fromString(encrypted).pipe(decipher);
});

我假设从from2 package

的示例中选择了效用函数fromString
import * as from from 'from2';

function fromString(text: string) 
{
    return from((size, next) => 
    {
        if (text.length <= 0) 
            return next(null, null);

        const chunk = text.slice(0, size);
        text = text.slice(size);

        next(null, chunk);
    });
}

有关为何无法正常工作的任何提示? 非常感谢你。我非常坚持这一点。

1 个答案:

答案 0 :(得分:2)

实际上,以下代码在文件上运行正常。我不知道这和我发布的关于......的差异......

<div class="col-lg-6 col-sm-6">
    <div class="card hovercard">

        <div class="btn-pref btn-group btn-group-justified btn-group-lg" role="group" aria-label="...">
            <div class="btn-group" role="group">
                <button type="button" id="stars" class="btn btn-primary" href="#tab1" data-toggle="tab">
                    <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                    <div class="hidden-xs">Stars</div>
                </button>
            </div>
            <div class="btn-group" role="group">
                <button type="button" id="favorites" class="btn btn-default" href="#tab2" data-toggle="tab">
                    <span class="glyphicon glyphicon-heart" aria-hidden="true"></span>
                    <div class="hidden-xs">Favorites</div>
                </button>
            </div>
            <div class="btn-group" role="group">
                <button type="button" id="following" class="btn btn-default" href="#tab3" data-toggle="tab">
                    <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
                    <div class="hidden-xs">Following</div>
                </button>
            </div>
        </div>

        <div class="well">
            <div class="tab-content">
                <div class="tab-pane fade in active" id="tab1">
                    <h3>This is tab 1</h3>
                </div>
                <div class="tab-pane fade in" id="tab2">
                    <h3>This is tab 2</h3>
                </div>
                <div class="tab-pane fade in" id="tab3">
                    <h3>This is tab 3</h3>
                </div>
            </div>
        </div>

    </div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $(".btn-pref .btn").click(function () {
            $(".btn-pref .btn").removeClass("btn-primary").addClass("btn-default");
            // $(".tab").addClass("active"); // instead of this do the below 
            $(this).removeClass("btn-default").addClass("btn-primary");
        });
    });
</script>