ग्रेट पर एक नज़र डालें! मैंने कोशिश की लेकिन संदेश को एन्क्रिप्ट करने के लिए इसका उपयोग कैसे करें। किसी भी "एन्क्रिप्ट" समारोह
यह System.Security.Cryptography.ECDiffieHellmanCng
के लिए MSDN नमूना है लगता नहीं है।
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Alice
{
public static byte[] alicePublicKey;
public static void Main(string[] args)
{
using (ECDiffieHellmanCng alice = new ECDiffieHellmanCng())
{
alice.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
alice.HashAlgorithm = CngAlgorithm.Sha256;
alicePublicKey = alice.PublicKey.ToByteArray();
Bob bob = new Bob();
CngKey k = CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob);
byte[] aliceKey = alice.DeriveKeyMaterial(CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob));
byte[] encryptedMessage = null;
byte[] iv = null;
Send(aliceKey, "Secret message", out encryptedMessage, out iv);
bob.Receive(encryptedMessage, iv);
}
}
private static void Send(byte[] key, string secretMessage, out byte[] encryptedMessage, out byte[] iv)
{
using (Aes aes = new AesCryptoServiceProvider())
{
aes.Key = key;
iv = aes.IV;
// Encrypt the message
using (MemoryStream ciphertext = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] plaintextMessage = Encoding.UTF8.GetBytes(secretMessage);
cs.Write(plaintextMessage, 0, plaintextMessage.Length);
cs.Close();
encryptedMessage = ciphertext.ToArray();
}
}
}
}
public class Bob
{
public byte[] bobPublicKey;
private byte[] bobKey;
public Bob()
{
using (ECDiffieHellmanCng bob = new ECDiffieHellmanCng())
{
bob.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
bob.HashAlgorithm = CngAlgorithm.Sha256;
bobPublicKey = bob.PublicKey.ToByteArray();
bobKey = bob.DeriveKeyMaterial(CngKey.Import(Alice.alicePublicKey, CngKeyBlobFormat.EccPublicBlob));
}
}
public void Receive(byte[] encryptedMessage, byte[] iv)
{
using (Aes aes = new AesCryptoServiceProvider())
{
aes.Key = bobKey;
aes.IV = iv;
// Decrypt the message
using (MemoryStream plaintext = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(encryptedMessage, 0, encryptedMessage.Length);
cs.Close();
string message = Encoding.UTF8.GetString(plaintext.ToArray());
Console.WriteLine(message);
}
}
}
}
}
स्रोत
2015-01-20 08:09:28
धन्यवाद Chochos। मैंने सफलतापूर्वक बाउंसी कैसल लाइब्रेरी का उपयोग किया है। हालांकि दस्तावेज को ढूंढना मुश्किल था! :) – Hemant
जावा के लिए अंतिम रिलीज 1.7, 7 अप्रैल 2011 जावा 1.51, 27 जुलाई 2014 के लिए है। सी # परियोजनाओं के लिए मुझे लापता कैसल पसंद नहीं है, क्योंकि गायब सुविधाओं और बनाए रखा कोड नहीं है। –
ध्यान दें कि सी # के लिए बाउंसीकास्टल में ईसीसी बहुत धीमी है और समय पर हमले के लिए कमजोर है। – CodesInChaos