I wrote a simple code where I add hexadecimal values multiplied by 0x1, 0x100 and so on together.
uid = (nuidPICC[0] * 0x1000000);
uid = uid + (nuidPICC[1] * 0x10000);
uid = uid + (nuidPICC[2] * 0x100);
uid = uid + nuidPICC[3];
when I pass numbers D1,55,BF,2D result is D154BF2D but on some numbers combination it is working well, I am using Arduino IDE 1.8.5, can you explain?
The code you have is working perfectly, so there's rather little to explain, if you assume that it is doing what you want it to do. If it's not doing what you want it to do, then we can guess what you want it to do, but then it could be that you wanted it to make you a nice cup of tea, and in that case this won't be much help.
Since you don't give the definitions of the variables, I will assume you have something like:
int8_t nuidPICC[] = { 0xD1,0x55,0xBF,0x2D };
printf("nuidPICC = { %d, %d, %d, %d }\n", nuidPICC[0], nuidPICC[1], nuidPICC[2], nuidPICC[3]);
int32_t uid = (nuidPICC[0] * 0x1000000);
uid = uid + (nuidPICC[1] * 0x10000);
uid = uid + (nuidPICC[2] * 0x100);
uid = uid + nuidPICC[3];
printf("uid = %d * %d + %d * %d + %d * %d + %d = %d\n",
nuidPICC[0], 0x1000000,
nuidPICC[1], 0x10000,
nuidPICC[2], 0x100,
nuidPICC[3], uid);
printf("%d in hex is %08x\n",uid,uid);
which outputs
nuidPICC = { -47, 85, -65, 45 }
uid = -47 * 16777216 + 85 * 65536 + -65 * 256 + 45 = -782975187
-782975187 in hex is d154bf2d
And you can verify that it does exactly what you asked it to.
However, given the values you're multiplying by, you seem to be trying to make a mask from four signed bytes.
Multiplying a int8_t by an integer literal causes it to be extended to an int, and so
int32_t x = int8_t(0xbf) * 0x100;
printf("0xbf * 0x100 = %d or 0x%08x\n",x,x);
0xbf * 0x100 = -16640 or 0xffffbf00
Those leading 0xffff are called 'sign extension' and are causing the next higher byte value to differ from what you would get if you were just shifting and combining the bits.
If you want to combine signed bytes, you need to mask off the sign extension:
uid = (nuidPICC[0] << 24);
uid = uid | (nuidPICC[1] << 16) & 0xff0000;
uid = uid | (nuidPICC[2] << 8) & 0xff00;
uid = uid | nuidPICC[3] & 0xff;
or
uid = ( 0xffffffd1 << 24)
| ( ( 0x00000055 << 16 ) & 0xff0000 )
| ( ( 0xffffffbf << 8 ) & 0xff00 )
| ( 0x0000002d & 0xff )
= 0xd155bf2d
but usually it's easier to use unsigned bytes for bit masks as they don't have sign extension:
uint8_t nuidPICC[] = { 0xD1,0x55,0xBF,0x2D };
uint32_t uid = (nuidPICC[0] << 24);
uid = uid | (nuidPICC[1] << 16);
uid = uid | (nuidPICC[2] << 8);
uid = uid | nuidPICC[3];
printf("uid = ( 0x%x << %d) | ( 0x%x << %d ) | ( 0x%x << %d ) | 0x%x = 0x%x\n",
nuidPICC[0], 24,
nuidPICC[1], 16,
nuidPICC[2], 8,
nuidPICC[3], uid);
uid = ( 0x000000d1 << 24)
| ( 0x00000055 << 16 )
| ( 0x000000bf << 8 )
| 0x0000002d
= 0xd155bf2d
Related
I have an IP address 10.0.2.0
The next IP after a block of 64 (10.0.2.0 to 10.0.2.63) is 10.0.2.64
After that (10.0.2.64 to 10.0.2.127 ) 10.0.2.128 etc
How do I calculate the nth?
I had assumed roughly
a = (n*64) mod 256
b = 255/n
10.0.2+b.a
Here's the final solution (in JavaScript):
function incrementIp(ip,nips){
var input = ip.split(".");
var ip = (input[0] << 24) | (input[1] << 16) | (input[2] << 8) | (input[3] << 0);
ip+=nips;
return (ip>>24 & 0xff )+ "." + (ip>>16 &0xff) + "." +( ip>>8 &0xff) + "." + (ip & 0xff);
}
I want to use two BulkSendApplications to send from node_0 to node_1 and from node_2 to node_3.
My code for creating the applications looks the following:
// node_0 to node_1
uint16_t rcv_port = 50000;
PacketSinkHelper sink1 ("ns3::TcpSocketFactory",
Address (InetSocketAddress (Ipv4Address::GetAny (), rcv_port)));
ApplicationContainer sinkApps1 = sink1.Install (node_1);
sinkApps1.Start (MilliSeconds (0));
sinkApps1.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
BulkSendHelper source1 (
"ns3::TcpSocketFactory",
Address (InetSocketAddress (node_1, rcv_port)));
ApplicationContainer sourceApps1 = source1.Install (node_0);
source.SetAttribute ("MaxBytes", UintegerValue (0));
sourceApps1.Start (MilliSeconds (start_sending_traffic));
sourceApps1.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
// node_2 to node_3
uint16_t rcv_port2 = 50001;
PacketSinkHelper sink2 ("ns3::TcpSocketFactory",
Address (InetSocketAddress (Ipv4Address::GetAny (), rcv_port2)));
ApplicationContainer sinkApps2 = sink2.Install (node_3);
sinkApps2.Start (MilliSeconds (0));
sinkApps2.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
BulkSendHelper source2 (
"ns3::TcpSocketFactory",
Address (InetSocketAddress (node_3, rcv_port2)));
ApplicationContainer sourceApps2 = source2.Install (node_2);
source2.SetAttribute ("MaxBytes", UintegerValue (0));
sourceApps2.Start (MilliSeconds (start_sending_traffic));
sourceApps2.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
The code is the same for both, just with the correct nodes. If I run this code, node_0->node_1 is transmitting the correct amount of traffic while node_2->node_3 is not transmitting anything.
I am checking the amount of bits received with the following code snippet:
Ptr<PacketSink> s1 = DynamicCast<PacketSink> (sinkApps1.Get (0));
std::cout << "Total Bits Received: " << s1->GetTotalRx () * 8 << std::endl;
Ptr<PacketSink> s2 = DynamicCast<PacketSink> (sinkApps2.Get (0));
std::cout << "Total Bits Received: " << s2->GetTotalRx () * 8 << std::endl;
Does anyone know what is going wrong here? Am I using the constructors the wrong way?
In PHP
<?php
$token = 'uid=pratik#gmail.com|ts=1412917909|hash=r1xWbgfHUxDLlppGYuOKQJdIM1MTrkryEArkMQx9ERw=|url=http://myintranet.com';
$key = 'a1cbbb6eb5cb2c1c27a9f02a4434d3af';
$token = mb_convert_encoding( $token ,'UTF-16LE' );
$blockSize = 16;
$pad = $blockSize - (strlen($token) % $blockSize);
$token .= str_repeat(chr($pad), $pad);
$token = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key, $token, MCRYPT_MODE_CBC, $iv);
$token = base64_encode($token);
echo "\n This is the token $token \n";
?>
Output -:
This is the token TXz3UEgAdjGhyriNGcMJBUk4QcW3dA7rttzjbKztw19X8bSIMDZt8s6uSQy2OP5QcSpJuReKv73wFXzPyCXt05CNY6XWlx9Lfrv6Nosj0+4mHdD7/Wvx0QWqxuuv5qv4sgtgSif59Wy/ZAoYhfH8yzN/3hWnx6zzOrV6jxyDttmffk1zcBwtJ3X41mMVbPLOd1/2K3ZYxCcJ1VxESFDNB4N1okvGMRkCM0tL77oZiKv+n6CP9FEgKivCfvytFB8JWc9K++8vbLdV/iGgkEa7h0pfAZtYpryQQjFzqLx8NSQ=
In Javascript
'use strict';
var CryptoJS = require("crypto-js");
String.prototype.repeat = function( num )
{
return new Array( num + 1 ).join( this );
}
function encodeUTF16LE(str) {
var out, i, len, c;
var char2, char3;
out = "";
len = str.length;
i = 0;
while(i < len) {
c = str.charCodeAt(i++);
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += str.charAt(i-1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
out += str.charAt(i-1);
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
}
}
var byteArray = new Uint8Array(out.length * 2);
for (var i = 0; i < out.length; i++) {
byteArray[i*2] = out.charCodeAt(i); // & 0xff;
byteArray[i*2+1] = out.charCodeAt(i) >> 8; // & 0xff;
}
return String.fromCharCode.apply( String, byteArray );
}
var token = 'uid=pratik#gmail.com|ts=1412917909|hash=r1xWbgfHUxDLlppGYuOKQJdIM1MTrkryEArkMQx9ERw=|url=http://myintranet.com';
var key = 'a1cbbb6eb5cb2c1c27a9f02a4434d3af';
var blockSize = 16;
token = encodeUTF16LE(token);
var pad = blockSize - (token.length % blockSize);
token = token + (String.fromCharCode(pad)).repeat(pad);
token = CryptoJS.AES.encrypt(token, key,
{ iv: iv,
mode: CryptoJS.mode.CBC
});
console.log("\n This is the token " + token + "\n");
token = token.ciphertext.toString(CryptoJS.enc.Base64);
console.log("\n This is the token " + token + "\n");
Output
This is the token U2FsdGVkX19iQjVHkx/vmhljCsRyTBUA0QFJ8I+pPvxAa2dK6iO4r9FUw2Um2j0H+iyXZ/G0UO0fhJTFzfJEfS1cMfAaq0Z7UBUpVhtrH5IArr2F3BI6yWC8Kpo4ZimyW+xnWp0BYUpLUNQTLsFooiIqPHv3s9HHMe3k0altm6ou1pAKaIr8IAY1OzIDTbaRO55mPf0rU6Z2XTLGR6kYoAx9Lk4dZ3RA66cynXWFMuHznL0fik3phZ8cUiKd/Twquil97YHT+CB/1ulxEBD17VQvnsCJI1lYNn9dyWAUG96KMgGk3jFxiW9eRzV5Poywnt0QNaRpmZiG41KNFmtMtw==
This is the token GWMKxHJMFQDRAUnwj6k+/EBrZ0rqI7iv0VTDZSbaPQf6LJdn8bRQ7R+ElMXN8kR9LVwx8BqrRntQFSlWG2sfkgCuvYXcEjrJYLwqmjhmKbJb7GdanQFhSktQ1BMuwWiiIio8e/ez0ccx7eTRqW2bqi7WkApoivwgBjU7MgNNtpE7nmY9/StTpnZdMsZHqRigDH0uTh1ndEDrpzKddYUy4fOcvR+KTemFnxxSIp39PCq6KX3tgdP4IH/W6XEQEPXtVC+ewIkjWVg2f13JYBQb3ooyAaTeMXGJb15HNXk+jLCe3RA1pGmZmIbjUo0Wa0y3
I think the problem is CryptoJS.AES.encrypt where I am not passing it the correct configuration?
Really stuck on this, so if you have any suggestions, I'd like to know.
In both cases the key is not treated in the way you expect. 'a1cbbb6eb5cb2c1c27a9f02a4434d3af' is the hex representation of a 16 byte (= 128 bit) key, that can be used for AES128.
mcrypt does not know its getting a hex representation and treats it as 32 byte string and - IIRC - throws away everything except its first 16 bytes ('a1cbbb6eb5cb2c1c'). You need to unhexlify the key to get the raw bytes: $key=pack('H*', 'a1cbbb6eb5cb2c1c27a9f02a4434d3af').
CryptoJS also does not know it's presented a hex representation of the key and treats it as password instead, which is used as input to PBKDF2. The library has its own unhexlify routines: var key = CryptoJS.enc.Hex.parse('a1cbbb6eb5cb2c1c27a9f02a4434d3af'); The resulting WordArray will be treated by CryptoJS as binary key input.
I'm very interested in cryptography, and since I like programming too, I decided to make a little program to encrypt files using XTEA encryption algorithm.
I got inspired from Wikipedia, and so I wrote this function to do the encryption (To save space, I won't post the deciphering function, as it is almost the same):
void encipher(long *v, long *k)
{
long v0 = v[0], v1 = v[1];
long sum = 0;
long delta = 0x9e3779b9;
short rounds = 32;
for(uint32 i = 0; i<rounds; i++)
{
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
}
v[0] = v1;
v[1] = v1;
}
Now when I want to use it, I wrote this code:
long data[2]; // v0 and v1, 64bits
data[0] = 1;
data[1] = 1;
long key[4]; // 4 * 4 bytes = 16bytes = 128bits
*key = 123; // sets the key
cout << "READ: \t\t" << data[0] << endl << "\t\t" << data[1] << endl;
encipher(data, key);
cout << "ENCIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
decipher(data, key);
cout << "DECIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
I always get either run-time crash or wrong decipher text:
I do understand the basics of the program, but I don't really know what is wrong with my code. Why is the enciphered data[0] and data1 the same? And why is deciphered data completely different from the starting data? Am I using the types wrong?
I hope you can help me solving my problem :) .
Jan
The problem is here:
v[0] = v1; // should be v[0] = v0
v[1] = v1;
Also, you only set the first 4 bytes of the key. The remaining 12 bytes are uninitialized.
Try something like this:
key[0] = 0x12345678;
key[1] = 0x90ABCDEF;
key[2] = 0xFEDCBA09;
key[3] = 0x87654321;
The fixed code gives me this output:
READ: 1
1
ENCIPHERED: -303182565
-1255815002
DECIPHERED: 1
1
There is a time represented in MJD and BCD format with 5 bytes .I am wondering what is the recommended format to save this date-time in the sqlite database so that user can search against it ?
My first attempt is to save it just as it is, that is a 5 bytes string. The user will use the same format to search and the result will be converted to unix time by the user with following code.
However, later, I was suggested to save the time in the integer - the UTC time, for example. But I can not find a standard way to do the conversion.
I feel this is a common issue and would like to hear your comments.
time_t sidate_to_unixtime(unsigned char sidate[])
{
int k = 0;
struct tm tm;
double mjd;
/* check for the undefined value */
if ((sidate[0] == 0xff) &&
(sidate[1] == 0xff) &&
(sidate[2] == 0xff) &&
(sidate[3] == 0xff) &&
(sidate[4] == 0xff)) {
return -1;
}
memset(&tm, 0, sizeof(tm));
mjd = (sidate[0] << 8) | sidate[1];
tm.tm_year = (int) ((mjd - 15078.2) / 365.25);
tm.tm_mon = (int) (((mjd - 14956.1) - (int) (tm.tm_year * 365.25)) / 30.6001);
tm.tm_mday = (int) mjd - 14956 - (int) (tm.tm_year * 365.25) - (int) (tm.tm_mon * 30.6001);
if ((tm.tm_mon == 14) || (tm.tm_mon == 15)) k = 1;
tm.tm_year += k;
tm.tm_mon = tm.tm_mon - 2 - k * 12;
tm.tm_sec = bcd_to_integer(sidate[4]);
tm.tm_min = bcd_to_integer(sidate[3]);
tm.tm_hour = bcd_to_integer(sidate[2]);
return mktime(&tm);
}
Use the SQLite date and time functions.
For example,
datetime(MJD + 2400000.5)
test:
sqlite> select datetime(49987 + 2400000.5);
1995-09-27 00:00:00
Save it as UTC . THis is your best bet.