Task: Data Encryption Standard



C#: using System;
using System.IO;
using System.Security.Cryptography;

namespace DES {
    class Program {
        
        static string ByteArrayToString(byte[] ba) {
            return BitConverter.ToString(ba).Replace("-", "");
        }

        
        
        static byte[] Encrypt(byte[] messageBytes, byte[] passwordBytes) {
            byte[] iv = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            ICryptoTransform transform = provider.CreateEncryptor(passwordBytes, iv);
            CryptoStreamMode mode = CryptoStreamMode.Write;

            
            MemoryStream memStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memStream, transform, mode);
            cryptoStream.Write(messageBytes, 0, messageBytes.Length);
            cryptoStream.FlushFinalBlock();

            
            byte[] encryptedMessageBytes = new byte[memStream.Length];
            memStream.Position = 0;
            memStream.Read(encryptedMessageBytes, 0, encryptedMessageBytes.Length);

            return encryptedMessageBytes;
        }

        
        
        static byte[] Decrypt(byte[] encryptedMessageBytes, byte[] passwordBytes) {
            byte[] iv = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            ICryptoTransform transform = provider.CreateDecryptor(passwordBytes, iv);
            CryptoStreamMode mode = CryptoStreamMode.Write;

            
            MemoryStream memStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memStream, transform, mode);
            cryptoStream.Write(encryptedMessageBytes, 0, encryptedMessageBytes.Length);
            cryptoStream.FlushFinalBlock();

            
            byte[] decryptedMessageBytes = new byte[memStream.Length];
            memStream.Position = 0;
            memStream.Read(decryptedMessageBytes, 0, decryptedMessageBytes.Length);

            return decryptedMessageBytes;
        }

        static void Main(string[] args) {
            byte[] keyBytes = new byte[] { 0x0e, 0x32, 0x92, 0x32, 0xea, 0x6d, 0x0d, 0x73 };
            byte[] plainBytes = new byte[] { 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87 };

            byte[] encStr = Encrypt(plainBytes, keyBytes);
            Console.WriteLine("Encoded: {0}", ByteArrayToString(encStr));

            byte[] decBytes = Decrypt(encStr, keyBytes);
            Console.WriteLine("Decoded: {0}", ByteArrayToString(decBytes));
        }
    }
}






VB: Imports System.IO
Imports System.Security.Cryptography

Module Module1

    
    Function ByteArrayToString(ba As Byte()) As String
        Return BitConverter.ToString(ba).Replace("-", "")
    End Function

    
    
    Function Encrypt(messageBytes As Byte(), passwordBytes As Byte()) As Byte()
        Dim iv As Byte() = {&H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0}

        
        Dim provider As New DESCryptoServiceProvider
        Dim transform = provider.CreateEncryptor(passwordBytes, iv)
        Dim mode = CryptoStreamMode.Write

        
        Dim memStream As New MemoryStream
        Dim cryptoStream As New CryptoStream(memStream, transform, mode)
        cryptoStream.Write(messageBytes, 0, messageBytes.Length)
        cryptoStream.FlushFinalBlock()

        
        Dim encryptedMessageBytes(memStream.Length - 1) As Byte
        memStream.Position = 0
        memStream.Read(encryptedMessageBytes, 0, encryptedMessageBytes.Length)

        Return encryptedMessageBytes
    End Function

    
    
    Function Decrypt(encryptedMessageBytes As Byte(), passwordBytes As Byte()) As Byte()
        Dim iv As Byte() = {&H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0}

        
        Dim provider As New DESCryptoServiceProvider
        Dim transform = provider.CreateDecryptor(passwordBytes, iv)
        Dim mode = CryptoStreamMode.Write

        
        Dim memStream As New MemoryStream
        Dim cryptoStream As New CryptoStream(memStream, transform, mode)
        cryptoStream.Write(encryptedMessageBytes, 0, encryptedMessageBytes.Length)
        cryptoStream.FlushFinalBlock()

        
        Dim decryptedMessageBytes(memStream.Length - 1) As Byte
        memStream.Position = 0
        memStream.Read(decryptedMessageBytes, 0, decryptedMessageBytes.Length)

        Return decryptedMessageBytes
    End Function

    Sub Main()
        Dim keyBytes As Byte() = {&HE, &H32, &H92, &H32, &HEA, &H6D, &HD, &H73}
        Dim plainBytes As Byte() = {&H87, &H87, &H87, &H87, &H87, &H87, &H87, &H87}

        Dim encStr = Encrypt(plainBytes, keyBytes)
        Console.WriteLine("Encoded: {0}", ByteArrayToString(encStr))

        Dim decStr = Decrypt(encStr, keyBytes)
        Console.WriteLine("Decoded: {0}", ByteArrayToString(decStr))
    End Sub

End Module