如何使用自己的密钥使用ecdeffiehellmancng加密数据 [英] How do I use my own key to encrypt data using ecdeffiehellmancng

查看:116
本文介绍了如何使用自己的密钥使用ecdeffiehellmancng加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是使用.Net中的Elliptic Curve Cryptography加密数据(特定于客户端),我尝试使用Microsoft的示例,但它似乎每次都会生成自己的密钥。我需要在此过程中使用自己的密钥,比如创建密码S3curEChaNNel_01?并将其转换为字节然后用作键,但我无法找到一种方法来执行此操作。



My task is to encrypt data using the Elliptic Curve Cryptography in .Net (specific from client), I tried using the sample from Microsoft but it seems to generate its own key everytime. I need to use my own key for this process, like creating a password "S3curEChaNNel_01?" and converting it to byte then use as a key, but i cant find a way to do this.

Using alice As New ECDiffieHellmanCng()
                Dim abData() As Byte
                Dim Str = txtKey.Text 'custom password
                abData = System.Text.Encoding.Default.GetBytes(Str)
                Str = System.Text.Encoding.Default.GetString(abData)

                Dim bobPublicKey() As Byte
                Dim bobKey() As Byte
                Dim bob As New ECDiffieHellmanCng()

                bob.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash
                bob.HashAlgorithm = CngAlgorithm.Sha256
                bobPublicKey = bob.PublicKey.ToByteArray()
                bob.HmacKey = abData

                bobKey = bob.DeriveKeyMaterial(CngKey.Create(CngAlgorithm.Sha256))
'at this line i get an exception, "The requested operation is not supported."

                'Dim aliceKey As Byte() = alice.DeriveKeyMaterial(CngKey.Create(CngAlgorithm.Sha256))
                Dim encryptedMessage As Byte() = Nothing
                Dim iv As Byte() = Nothing
                txtOutput.Text = ByteArrayToString(Encrypt(bobKey, txtPlainStr.Text, encryptedMessage, iv))
            End Using





请帮助我out,任何工作样品都将非常感谢



我尝试过:



我尝试过使用

bobKey = bob.DeriveKeyMaterial(CngKey.Import(abData,CngKeyBlobFormat.EccPrivateBlob))



并且



bobKey = bob.DeriveKeyMaterial(CngKey.Import(abData,CngKeyBlobFormat.EccPublicBlob))



Pls help me out, any working sample will be very much appreciated

What I have tried:

I have tried using the
bobKey = bob.DeriveKeyMaterial(CngKey.Import(abData, CngKeyBlobFormat.EccPrivateBlob))

and

bobKey = bob.DeriveKeyMaterial(CngKey.Import(abData, CngKeyBlobFormat.EccPublicBlob))

推荐答案

ÿ你不能。你需要一个椭圆曲线键。转换像S3curEChaNNel_01?这样的东西更加复杂和安全。一个字节数组。



ECC密钥的一个例子是由证书颁发机构颁发的ECC证书。



如果你不想去PKI之路。创建一个并导入如下:

You can't. You need an elliptic curve key. That is way more complex and secure that converting something like "S3curEChaNNel_01?" to a byte array.

An example for a ECC key would be an ECC certificate issued by an certificate authority.

If you don't want to go the PKI road. Create one and and import as follows:
//Save
var key = CngKey.Create(CngAlgorithm.Sha256);
var bob = new ECDiffieHellmanCng(key);
var x = bob.Key.Export(CngKeyBlobFormat.EccPrivateBlob);

//Load
bob = new ECDiffieHellmanCng(CngKey.Import(x, CngKeyBlobFormat.EccPrivateBlob))


这篇关于如何使用自己的密钥使用ecdeffiehellmancng加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆