Rumah > pembangunan bahagian belakang > tutorial php > node.js - php与nodejs的加密数据互通

node.js - php与nodejs的加密数据互通

WBOY
Lepaskan: 2016-06-06 20:52:13
asal
1186 orang telah melayarinya

nodejs的加密解密代码示例如下:

#!/usr/bin/env node
    var crypto = require('crypto');
    //解密
    function decode(cryptkey, iv, secretdata) {
        var 
        decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv),
        decoded  = decipher.update(secretdata, 'base64', 'utf8');
        
        decoded += decipher.final( 'utf8' );
        return decoded;
    }
    //解密
    function encode(cryptkey, iv, cleardata) {
        var 
        encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv),
        encoded  = encipher.update(cleardata, 'utf8', 'base64');

        encoded += encipher.final( 'base64' );
        return encoded;
    }

    var 
    cryptkey   = crypto.createHash('sha256').update('__tazai_wolf__key').digest(),
    iv         = '1234567890000000',
    buf        = "Hello World",
    enc        = encode( cryptkey, iv, buf );

    var dec        = decode(cryptkey, iv, enc);

    function b64enc(data) {
        var b   = new Buffer(data, 'binary');
        return b.toString('base64');
    }

console.warn("Encoded length: ", enc);
console.warn("Decoded all: " + dec);
Salin selepas log masuk
Salin selepas log masuk

请问php能相应的加密解密代码应该如何写?
要求能解上面加密后的如:Gpkr1WGBFhMvNd/Hr0eaBg==
也能加密数据给nodejs进行解密。

回复内容:

nodejs的加密解密代码示例如下:

#!/usr/bin/env node
    var crypto = require('crypto');
    //解密
    function decode(cryptkey, iv, secretdata) {
        var 
        decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv),
        decoded  = decipher.update(secretdata, 'base64', 'utf8');
        
        decoded += decipher.final( 'utf8' );
        return decoded;
    }
    //解密
    function encode(cryptkey, iv, cleardata) {
        var 
        encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv),
        encoded  = encipher.update(cleardata, 'utf8', 'base64');

        encoded += encipher.final( 'base64' );
        return encoded;
    }

    var 
    cryptkey   = crypto.createHash('sha256').update('__tazai_wolf__key').digest(),
    iv         = '1234567890000000',
    buf        = "Hello World",
    enc        = encode( cryptkey, iv, buf );

    var dec        = decode(cryptkey, iv, enc);

    function b64enc(data) {
        var b   = new Buffer(data, 'binary');
        return b.toString('base64');
    }

console.warn("Encoded length: ", enc);
console.warn("Decoded all: " + dec);
Salin selepas log masuk
Salin selepas log masuk

请问php能相应的加密解密代码应该如何写?
要求能解上面加密后的如:Gpkr1WGBFhMvNd/Hr0eaBg==
也能加密数据给nodejs进行解密。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan