CSharpTest.Net
RSAPrivateKey Class
Members  Example  See Also  Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace : RSAPrivateKey Class

Glossary Item Box

Provides a wrapper around encrypting/decrypting with public/private key pairs from Certificats or CSP

Object Model

RSAPrivateKey ClassRSAPublicKey Class

Syntax

Visual Basic (Declaration) 
Public Class RSAPrivateKey 
   Inherits RSAPublicKey
   Implements IEncryptDecrypt 
C# 
public class RSAPrivateKey : RSAPublicKey, IEncryptDecrypt  

Example

Library/Library.Test/TestRSACrypto.cs

C#Copy Code
byte[] data = new byte[100];
new Random().NextBytes(data);

foreach (HashAlgorithm ha in new HashAlgorithm[] { MD5.Create(), SHA1.Create(), SHA256.Create(), SHA384.Create(), SHA512.Create() })
{
    Hash hash = Hash.FromBytes(ha.ComputeHash(data));
    Assert.AreEqual(CryptoConfig.MapNameToOID(ha.GetType().FullName), hash.AlgorithmOID);

    using (RSAPrivateKey key = new RSAPrivateKey())
    {
        byte[] sig = key.SignHash(hash);

        using (RSAPublicKey pub = key.PublicKey)
        {
            Assert.IsTrue(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))));
            data[0] = (byte)~data[0];
            Assert.IsFalse(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))));
        }
    }
}
VB.NETCopy Code
Dim data As Byte() = New Byte(100) {}
New Random().NextBytes(data)

For Each ha As HashAlgorithm In New HashAlgorithm() {MD5.Create(), SHA1.Create(), SHA256.Create(), SHA384.Create(), SHA512.Create()}
    Dim hash As Hash = Hash.FromBytes(ha.ComputeHash(data))
    Assert.AreEqual(CryptoConfig.MapNameToOID(ha.[GetType]().FullName), hash.AlgorithmOID)

    Using key As New RSAPrivateKey()
        Dim sig As Byte() = key.SignHash(hash)

        Using pub As RSAPublicKey = key.PublicKey
            Assert.IsTrue(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))))
            data(0) = DirectCast(Not data(0), Byte)
            Assert.IsFalse(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))))
        End Using
    End Using
Next

Inheritance Hierarchy

System.Object
   CSharpTest.Net.Crypto.CryptoKey
      CSharpTest.Net.Crypto.AsymmetricKey
         CSharpTest.Net.Crypto.RSAPublicKey
            CSharpTest.Net.Crypto.RSAPrivateKey

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys