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.
#include <iostream>
#include <cstdint>
using namespace std;
uint16_t selectID(uint16_t x) {
return (x & 0xFE00) >> 9;
}
uint16_t selectAccount(uint16_t x) {
return (x & 0x01FF);
}
uint16_t encrypt(uint16_t x) {
uint16_t a = 5, b = 233;
return a*x + b;
}
uint16_t decrypt(uint16_t y) {
uint16_t a = 5, b = 233;
uint32_t x = y - b;
for (uint16_t i=0; i<a; i++) {
if ( x % a == 0 ) {
break;
}
x += 0x10000;
}
return (uint16_t) (x/5);
}
int main() {
uint16_t x = 0x1234;
cout << "Customer ID is " << selectID(x) << endl;
cout << "Customer account is " << selectAccount(x) << endl;
for (uint16_t u = 100; u < 65000; u += 5000) {
uint16_t e = encrypt(u);
cout << "Encrypt of " << u << " is " << e << endl;
cout << "Decrypt of " << e << " is " << decrypt(e) << endl << endl;
}
}
Comments
Leave a comment