15,113,531 members
See more:
Write C++ program: Lets create your first cipher!!! Cipher is an algorithm that you can use to encrypt and decrypt sensitive information. In an information system, sensitive information is stored in a 16-bit unsigned number, where bit-wise information is stored in following format, 9-bits are reserved for account number and 7-bits are reserved for customer ID.

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

C C C C C C C A A A A A A A A A

a) Using bit wise operators, write a function to extract customer ID

b) Another function to extract account number.

c) Due to security concerns, you have been given a task to create a cipher. Write a function to encrypt the 16-bit number (X) using the following expression.

y = aX + b, consider a = 5 and b = 233

d) Write another function to decrypt the number and display the original number.

What I have tried:

```#include<iostream>
using namespace std;
int extract_customer_ID(int number);
int extract_account_number(int number);
int encrypt_the_16(int number);
int decrypt_the_number(int number);

int main()
{
int number;
extract_customer_ID(number);
extract_account_number(number);
decrypt_the_number(number);
encrypt_the_16(number);
return 0;
}

int extract_customer_ID(int number)
{
int new_number = number >> 7;
return new_number;
}

int extract_account_number(int number)
{
int new_number = number >> 9;
return new_number;
}

int encrypt_the_16(int number)
{
int a = 5, b = 233;
return number * a + b;
}

int decrypt_the_number(int number)
{
int a = 5, b = 233;
cout<<(number-b)/a;
}```
Posted
Updated 15-Oct-21 9:47am
jeron1 15-Oct-21 12:56pm

and your problem is?
Ans Javed 15-Oct-21 13:21pm

I am asking a question how to solve this question. I tried to make that program but it gives error.
jeron1 15-Oct-21 13:29pm

Care to elaborate on the error?
Patrice T 15-Oct-21 13:20pm

And you have a question ?

## Solution 2

I will give you a hint. Here are at least two ways to separate the two parts of the 16-bit value. First the mask-and-shift approach. To keep nine-bits of a value you can mask off everything else. I think in hexadecimal and the hex value for all nine least significant bits set is 0x1FF. To get mask everything else you can do this :
C++
```using USHORT = unsigned short;
const USHORT mask = 0x1FF;

USHORT customer = number & mask;```
Then to get what is left you can AND with the complement of the first mask and then shift the value. Here's what that would look like :
C++
```const USHORT negmask = ~ mask;
USHORT account = ( number & negmask ) >> 9;```
I recommend that you try this with several different values to verify it for yourself.

Another way this can be done is using bit fields and a union. Here what that would look like :
C++
```union Number
{
USHORT usv;
struct BFnumber
{
USHORT cust : 9;
USHORT acct : 7;
} bfv;
};```
and then to use that you can do this :
C++
```Number custdata;
custdata.usv = number;
printf( "customer is %4d or %04X", custdata.bfv.cust, custdata.bfv.cust );
printf( "account  is %4d or %04X", custdata.bfv.acct, custdata.bfv.acct );
```
Ans Javed 17-Oct-21 6:36am

Thanks

## Solution 1

Well ... for starters, you don't assign any value to `number`, and then you pass that through the whole system without worrying about it - depending on your compiler and settings the value will be:
1) An error.
2) Zero
or
3) Random.

Then you have a bunch on functions that do very little towards solving the problem as set but return values that you completely ignore.

Read the assignment again, and start trying to do it in stages.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 185 Rick York 65 #realJSOP 65 Richard MacCutchan 50 Mircea Neacsu 50
 OriginalGriff 5,196 Richard MacCutchan 2,418 Richard Deeming 1,455 CHill60 1,128 Rick York 790

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900