473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Struggling with DES implementation in C

I am trying to encrypt a plaintext using DES in C. I read about the algorithm and how it works, but when i came to write the code i struggled. I hope that you could help me by answering the following questions for me:

How to locate the lowest 8-bits in a 64-bit key ?

How to shuffle the plaintext according to the algorithm description ?
(I read about bitwise operations, but i still cannot understand how i can use them to transfer for example the 5th bit to the location of the 30th bit)

Left shifting the key would not wrap the bits, so i just bitwise or with a mask that will add the bits that did not wrap around ?

Any information would be appreciated, thanks.
Jul 15 '14 #1
2 1094
weaknessforcats
9,208 Expert Mod 8TB
...transfer for example the 5th bit to the location of the 30th bit...

To get the 5th bit (count from 0 moving left) use a mask and the AND the mask to the data:

00100000 mask
11100110 data
AND
00100000 result

If the 5th bit is set the result is true. If not set, the result is false. Use this to set to reset the 30th bit.

I will use the 2nd bit rather than 30 to avoid big typing.

00000100 2nd bit (count from 0 moving left)

To set the 2nd bit use a mask and OR the data

00000100 2nd bit
00011100 data with 2nd bit already set
OR
00011100 data result

00000100 2nd bit
00011000 data with 2nd bit currently not set
OR
00011100 data result

To reset the 2nd bit use an inverted mask and AND the data:

11111011 mask
00011100 data with 2nd bit already set
AND
00011000 result

You can use bit shft operators to create your mask:

1 << 2 is 00000100

if you NOT this mask you get the inverted mask:

~(1 << 2) is 11111011.

So your real code might look like:

Reset the 2nd bit:

Expand|Select|Wrap|Line Numbers
  1. data = data & ~(1<<2);
which is usually written as:

Expand|Select|Wrap|Line Numbers
  1. data &= ~(1<<2);
Jul 15 '14 #2
Thank you very much.
Jul 15 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Stan | last post by:
Hey everyone, I've got a computer science class and we're working on C++. I am struggling with nested loops and have a really simple assignment, but I can't get it to run the loop. I need to run...
4
by: Rowan | last post by:
Hi there, it's me again. I am having trouble with a view again. I am trying to do a calculation, but there are some checks that need to be taken into consideration. Maybe a view is not the right...
8
by: Jason | last post by:
A week ago I posted a simple little hi-score routine that I was using to learn Python. I've only just managed to examine the code, and the responses that people gave, and I'm now seriously...
26
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() {...
2
by: Dan | last post by:
I've installed passport sdk on to windows 2003 and I have received a key from netservicesmanager.com. I have set securelevel in the registry to 0. When I click on the link generated by logotag2 it...
1
by: Greg Wilkerson | last post by:
I hope I can explain this in a way that can be understood. I have a custom control, I'll call it navigator, used to navigate "things". I has, among other controls, two standard buttons, previous...
17
by: Soumyadip Rakshit | last post by:
Hi, Could anyone direct me to a very good 1D DCT Implementation in C/C++ My data is in sets of 8 numbers. Thanks a ton, Soumyadip.
160
by: RG | last post by:
Greetings friends, This semester I have started a course in C programming. I was moving along fine until I reached to the topic of loops (feeling embarrassed among you elite programmers). My...
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
11
by: briankind | last post by:
Hello i have these radio buttons and i wish to have the corresponding textboxes to be visible when the radiobutton is selected. any Help? snippets. thanks thanks in adv Bry
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.