net "RijndaelManaged" to pb with crypt32.dll ?

Windows specific forum
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

net "RijndaelManaged" to pb with crypt32.dll ?

Post by bingo »

how can convert this NET code to pb ? crypt32.dll with CryptAcquireContext , CryptHashData ... should be possible . :cry:

Rijndael with 2 16byte strings :

Code: Select all

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace HWREncryption
{
  //class implements Rijndael algorithm
  public class HwrEncryptor
  {                
    public static string EncryptData(string original, string key)
    {
      byte[] toEncrypt;
      byte[] encrypted;
            
      ASCIIEncoding textConverter = new ASCIIEncoding();
      RijndaelManaged myRijndael = new RijndaelManaged();

      //Set the key and IV.
      myRijndael.Key = Encoding.ASCII.GetBytes("key1000000000000"); 
      myRijndael.IV  = Encoding.ASCII.GetBytes("key2000000000000"); 
      
      //Get an encryptor.
      ICryptoTransform encryptor = myRijndael.CreateEncryptor(myRijndael.Key, myRijndael.IV);

      //Encrypt the data.
      MemoryStream msEncrypt = new MemoryStream();
           
      CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

      //Convert the data to a byte array.
      toEncrypt = textConverter.GetBytes(original);

      //Write all data to the crypto stream and flush it.
      csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
      csEncrypt.FlushFinalBlock();

      //Get encrypted array of bytes.
      encrypted = msEncrypt.ToArray();

      csEncrypt.Close();
      msEncrypt.Close();

      return ASCIIEncoding.Unicode.GetString(encrypted);
    }
                            
    public static string DecryptData(string data, string key)
    {     
      byte[] encrypted = ASCIIEncoding.Unicode.GetBytes(data);
      byte[] fromEncrypt = new byte[encrypted.Length]; ;
      ASCIIEncoding textConverter = new ASCIIEncoding();
      RijndaelManaged myRijndael = new RijndaelManaged();

      //Set the key and IV.
      myRijndael.Key = Encoding.ASCII.GetBytes("key1000000000000"); ;
      myRijndael.IV  = Encoding.ASCII.GetBytes("key2000000000000"); 
      
      MemoryStream msDescrypt = new MemoryStream(encrypted);
      
      //Get an descryptor.
      ICryptoTransform decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV);
      CryptoStream csDecrypt = new CryptoStream(msDescrypt, decryptor, CryptoStreamMode.Read);

        
      //Read the data out of the crypto stream.
      csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
      
      msDescrypt.Close();
      csDecrypt.Close();
      
      //Convert the byte array back into a string.
      return textConverter.GetString(fromEncrypt);;
    }
  }        
}
["1:0>1"]
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post by mskuma »

Did you try searching here using 'CryptAcquireContext' - there are some mildly related examples there that might get you going.
Post Reply