locked
RSA Encryption and Decryption

    Question

  • Hi All,

    I hope someone can point me to the correct way to implement encryption/decryption using RSA.  Based upon the documentation in Asymmetric key encryption (Windows Runtime apps): "Typically the private key is kept secret and is used to encrypt data while the public key is distributed to interested parties and is used to decrypt data."  I would think that the CryptographicEngine supports encrypting data with the private key and decrypting it with the public key.  However, every time I've attempted to get this to work I get an exception when I attempt to decrypt the data.  Here is a test showing the issue:

    [TestMethod]
    public void DoesNotWork()
    {
        string unEncryptedData = "This is a test of the RSA Encryption and Decryption.";
    
        AsymmetricKeyAlgorithmProvider keyProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
        CryptographicKey key = keyProvider.CreateKeyPair((uint)2048);
    
        // Import the keys to simulate them being passed in as a parameter.
        CryptographicKey keyPair = keyProvider.ImportKeyPair(key.Export()); 
        CryptographicKey publicKey = keyProvider.ImportPublicKey(key.ExportPublicKey());
    
        // Encrypt the data using the keyPair. 
        IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.Unicode.GetBytes(unEncryptedData));
        IBuffer encryptedData = CryptographicEngine.Encrypt(keyPair, dataBuffer, null);
    
        // THIS DOES NOT WORK? Decrypt the data using the publicKey.
        IBuffer decryptedData = CryptographicEngine.Decrypt(publicKey, encryptedData, null);
    
        byte[] decryptedDataBytes;
        CryptographicBuffer.CopyToByteArray(decryptedData, out decryptedDataBytes);
        string actualData = Encoding.Unicode.GetString(decryptedDataBytes, 0, decryptedDataBytes.Length);
    
        Assert.AreEqual(unEncryptedData, actualData);
    }
    

    I've tried every asymmetric algorithm name in the AsymmetricAlgorithmNames class, different key sizes, changed the CryptographicPrivateKeyBlobType & CryptographicPublicKeyBlobType on the import to every possible value, etc...  But I continue to get the "The method or operation is not implemented." exception.

    I've even tried just using the key pair to encrypt and decrypt; which surprisingly works, even though I would have assumed that it wouldn't because it's an asymmetric key.  Here's my test:

    public void ConfusingTest()
    {
        string unEncryptedData = "This is a test of the RSA Encryption and Decryption.";
        AsymmetricKeyAlgorithmProvider keyProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
        CryptographicKey key = keyProvider.CreateKeyPair((uint)2048);
    
        // Import the keys to simulate them being passed in as a parameter.
        CryptographicKey keyPair = keyProvider.ImportKeyPair(key.Export()); 
    
        // Encrypt the data using the keyPair. 
        IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.Unicode.GetBytes(unEncryptedData));
        IBuffer encryptedData = CryptographicEngine.Encrypt(keyPair, dataBuffer, null);
    
        // THIS WORKS?  But it should be Asymmetric key encryption, not symmetric key encryption.
        IBuffer decryptedDataShouldNotWork = CryptographicEngine.Decrypt(keyPair, encryptedData, null);
        byte[] decryptedDataBytesShouldNotWork;
        CryptographicBuffer.CopyToByteArray(decryptedDataShouldNotWork, out decryptedDataBytesShouldNotWork);
        string actualDataShouldNotWork = Encoding.Unicode.GetString(decryptedDataBytesShouldNotWork, 0, decryptedDataBytesShouldNotWork.Length);
    
        Assert.AreEqual(unEncryptedData, actualDataShouldNotWork);
    }

    Any input would be greatly appreciated.

    Thanks,

    rainman-63

    Wednesday, February 11, 2015 3:38 PM

All replies

  • Hope this can help you. 

    http://blogs.msdn.com/b/stcheng/archive/2013/03/12/windows-store-app-how-to-perform-rsa-data-encryption-with-x509-certificate-based-key-in-windows-store-application.aspx

    Thursday, February 12, 2015 2:14 PM