简体   繁体   English

如何在区块链中解密?

[英]How to decrypt in BlockChain?

I have been asked to use BlockChain for a web app I am building and I did not hear about it before, after searching some information about it, now I understand what it is about.我被要求将 BlockChain 用于我正在构建的 web 应用程序,我之前没有听说过它,在搜索了一些关于它的信息之后,现在我明白了它是关于什么的。 So, basically it encrypts some data in blocks and in this way, the data is safe.因此,基本上它以块的形式加密了一些数据,这样,数据是安全的。

For instance, I have this code I took from internet:例如,我有这个从互联网上获取的代码:

const SHA256 = require('crypto-js/sha256')

class Block {
    constructor(timestamp, data) {
        this.index = 0;
        this.timestamp = timestamp;
        this.data = data;
        this.previousHash = "0";
        this.hash = this.calculateHash();
        this.nonce = 0;
    }

    calculateHash() {
        return SHA256(this.index + this.previousHash + this.timestamp + this.data + this.nonce).toString();
    }

    mineBlock(difficulty) {

    }
}

class Blockchain{
    constructor() {
        this.chain = [this.createGenesis()];
    }

    createGenesis() {
        return new Block(0, "01/01/2017", "Genesis block", "0")
    }

    latestBlock() {
        return this.chain[this.chain.length - 1]
    }

    addBlock(newBlock){
        newBlock.previousHash = this.latestBlock().hash;
        newBlock.hash = newBlock.calculateHash();
        this.chain.push(newBlock);
    }

    checkValid() {
        for(let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];

            if (currentBlock.hash !== currentBlock.calculateHash()) {
                return false;
            }

            if (currentBlock.previousHash !== previousBlock.hash) {
                return false;
            }
        }

        return true;
    }
}

let jsChain = new Blockchain();
jsChain.addBlock(new Block("12/25/2017", {amount: 5}));
jsChain.addBlock(new Block("12/26/2017", {amount: 10}));

And this is the result:这是结果: 在此处输入图像描述

As you can see, we have encrypted "Genesis block" in blocks (if I am not wrong).如您所见,我们已经在块中加密了“创世块”(如果我没记错的话)。

Ok so, what if I want to decrypt the data in order to get "Genesis block" back?好的,如果我想解密数据以获取“创世块”怎么办? Will this be possible?这可能吗?

I am new at this, so I find it a bit confusing... I understand what it is about but I do not know how I would implement it to my web app.我是新手,所以我觉得有点困惑......我了解它的含义,但我不知道如何将它实施到我的 web 应用程序中。 My web app basically gets some information from the database, shows that information to the final user and the final user sends an email to a customer who will have to pay through a link.我的 web 应用程序基本上从数据库中获取一些信息,向最终用户显示这些信息,最终用户将 email 发送给必须通过链接付款的客户。

Blockchains are not about encrypting data to keep it "safe";区块链不是要加密数据以保证其“安全”; they are about creating an auditable ledger of changes to some state that needs to be agreed by multiple parties.他们是关于创建一个需要多方同意的 state 更改的可审计分类帐。 In the case of bitcoin, they represent the distribution of currency across different wallets, and record transactions which change that distribution.就比特币而言,它们代表了货币在不同钱包中的分布,并记录了改变这种分布的交易。

Blockchains are a very specialized technology, and while there are a few situations where they are useful, there are also a lot people searching for problems to fit the solution.区块链是一项非常专业的技术,虽然在少数情况下它们很有用,但也有很多人在寻找适合解决方案的问题。 You're better off analysing your actual problems first, and then looking around for technologies that solve those problems.你最好先分析你的实际问题,然后寻找解决这些问题的技术。

In this case, your number one priority should be general security - if someone manages to run an UPDATE on your internal database, something has already gone very wrong.在这种情况下,您的第一要务应该是一般安全性——如果有人设法在您的内部数据库上运行UPDATE ,那么已经出现了非常严重的问题。 As part of "defence in depth", you might also want an audit trail of changes that have been made.作为“纵深防御”的一部分,您可能还需要对已进行的更改进行审计跟踪。 That might mean picking a database technology that forms an append-only ledger, but unlike a traditional blockchain, you probably don't need that to be based on distributed consensus.这可能意味着选择一种数据库技术,forms 是一种仅附加分类帐,但与传统区块链不同,您可能不需要基于分布式共识。 And you might find that actually having a separately secured audit log that you can cross-check if anything suspicious happens is enough for the scenarios you're expecting.您可能会发现,实际上拥有一个单独安全的审核日志,您可以交叉检查是否发生任何可疑情况,这对于您所期望的场景来说已经足够了。

Finally, if you do decide that a blockchain-based ledger solves a real problem you have, look for an existing implementation you can take advantage of, for the same reason you wouldn't try to write MySQL from scratch.最后,如果您确实认为基于区块链的分类账可以解决您遇到的实际问题,请寻找可以利用的现有实现,出于同样的原因,您不会尝试从头开始编写 MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM