Public Key Encryption


The following code creates and returns a new public/private key pair to the variable strMyKeyPair. The public/private key pair is a structure with eight fields, including both the public and private keys. This structure is encoded as an XML string and should be kept confidential because the private key is what is used to decrypt data.

strMyKeyPair = PublicKey.CreateKeyPair()

The next line of code extracts the public key from the private key strPrivateKey and returns it as a string to strPublicKey. The public key can then be freely distributed.

strPublicKey = PublicKey.GetPublicKey(strPrivateKey)

The following code encrypts the string strPlainText by using the public key strPublicKey, and it assigns the encrypted string to the variable strMyCipherText. Either the public key or the public/private key pair can be used to encrypt the string. This function can encrypt a string of 58 characters or fewer. If you try to encrypt a string with more than 58 characters, the encryption will fail.

strMyCipherText = PublicKey.Encrypt(strPlainText, strPublicKey)

The following code decrypts the string strCipherText by using the private key strPrivateKey. (The public key can’t be used to decrypt the string.) The decrypted string is assigned to the variable strPlainText. For sucessful decryption, you need to pass the original encrypted string and the associated private key. If you try to pass the wrong private key or pass the public key instead, the decryption will fail with an exception. Likewise, if the encryped text has been altered since first encrypted, the decryption will fail with an exception.

strPlainText = PublicKey.Decrypt(strCipherText, strPrivateKey) 




Security for Microsoft Visual Basic  .NET
Security for Microsoft Visual Basic .NET
ISBN: 735619190
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net