none
MAC PDU Header/Trailer RRS feed

  • Question

  • I have a C# program that does RSA, I need to attach a  header and trailer to the encrypted data and remove the header and trailer information before decryption. The header fields are as follows:

    1bit          1bit           11bits          1bit        1bit     2bits      1bit          11bits       16bits    8bits  

    HT

    EC

    Type

    Rsv

    CI

    EKS

    Rsv

    LEN

    CID

    HCS

    There is 4bytes trail also.

    My code is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Security.Cryptography;
    
    namespace WpfApplication_RSA
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            byte[] dataToEncrypt;
            byte[] encryptedData;
            byte[] decryptedData;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void btnEncrypt_Click(object sender, RoutedEventArgs e)
            {
                //Create a UnicodeEncoder to convert between byte array and string.
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                //Create byte arrays to hold original, encrypted, and decrypted data.
                dataToEncrypt = ByteConverter.GetBytes(txtMessage.Text.Trim());
    
                //Create a new instance of RSACryptoServiceProvider to generate
                //public and private key data.
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048))
                {
    
                    //Pass the data to ENCRYPT, the public key information 
                    //(using RSACryptoServiceProvider.ExportParameters(false),
                    //and a boolean flag specifying no OAEP padding.
                    encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
                    txtEncryptedPlainText.Text = ByteConverter.GetString(encryptedData);
                    //   txtEncryptedPlainText.Text = encryptedData.ToString();
    
                    //Pass the data to DECRYPT, the private key information 
                    //(using RSACryptoServiceProvider.ExportParameters(true),
                    //and a boolean flag specifying no OAEP padding.
                    decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
                    //Display the decrypted plaintext to the console. 
                    txtDecrypt.Text = ByteConverter.GetString(decryptedData);
                }
    
    
            }
    
            static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
            {
                try
                {
                    byte[] encryptedData;
                    //Create a new instance of RSACryptoServiceProvider.
                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048))
                    {
    
                        //Import the RSA Key information. This only needs
                        //toinclude the public key information.
                        RSA.ImportParameters(RSAKeyInfo);
    
                        //Encrypt the passed byte array and specify OAEP padding.  
                        //OAEP padding is only available on Microsoft Windows XP or
                        //later.  
                        encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
                    }
                    return encryptedData;
                }
                //Catch and display a CryptographicException  
                //to the console.
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
    
                    return null;
                }
            }
    
            static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
            {
                try
                {
                    byte[] decryptedData;
                    //Create a new instance of RSACryptoServiceProvider.
                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048))
                    {
                        //Import the RSA Key information. This needs
                        //to include the private key information.
                        RSA.ImportParameters(RSAKeyInfo);
    
                        //Decrypt the passed byte array and specify OAEP padding.  
                        //OAEP padding is only available on Microsoft Windows XP or
                        //later.  
                        decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
                    }
                    return decryptedData;
                }
                //Catch and display a CryptographicException  
                //to the console.
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.ToString());
    
                    return null;
                }
    
            }
    
            private void btnDecrypt_Click(object sender, RoutedEventArgs e)
            {
                lblDecrypt.Visibility = Visibility.Visible;
                txtDecrypt.Visibility = Visibility.Visible;
    
            }
        }
    }


    Tuesday, March 12, 2013 9:41 AM

All replies

  • Hi David,

    What is your problem?

    Have a nice day.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Wednesday, March 13, 2013 2:40 PM
  • Hi Ghost,

    How do I have the encrypted data be prefixed with the 6bytes(48bits) header fields and 4bytes trailer field. Say I am encypting "HELLO". I am a novice to programming, so i hope my question is more clear now. the header is a standard format for MAC PDU in WiMAX technology which uses RSA for encryption and decryption. The algorithm is working but I need to show the encrypted text in the PDU before decrypting so as relate the algorithm to WiMAX 802.16e standard.

    Best Regards,

    David

    Wednesday, March 13, 2013 3:32 PM