I tried to find a proper example on the Internet for days but did not succeed. I try to encrypt simple string (Hello World) with a key, then to decrypt the result.
However the decrypted result has nothing to do with the original text. Can anyone point me to a direction please?
The code I made:
AES_KEY aes_decryptKey;
AES_KEY aes_encryptKey;
const unsigned char mykey[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
unsigned char encrypted ;
unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char decrypted;
AES_set_encrypt_key(mykey, 256, &aes_encryptKey);
AES_set_decrypt_key(mykey, 256, &aes_decryptKey);
const unsigned char original[] = {0x48,0x65,0x6c,0x6c,0x6f,0x2c,0x77,0x6f,0x72,0x6c,0x64,0x21};
AES_cbc_encrypt(original, &encrypted, sizeof(original), &aes_encryptKey, iv, 0);
AES_decrypt( &encrypted, &decrypted, &aes_decryptKey);
NSLog(#"ORIGINAL: \"%s\"\n",original);
NSLog(#"ENCRYPTED: \"%s\"\n",&encrypted);
NSLog(#"DECRYPTED: \"%s\"\n",&decrypted);
You are decrypting twice -- that last 0 parameter for AES_cbc_encrypt should be a 1 or AES_ENCRYPT.
Also, you are overwriting your encryption and decryption chars, which should instead be arrays big enough to hold the encrypted size of original. Instead of:
unsigned char encrypted;
...
AES_cbc_encrypt(original, &encrypted, ...
use something like:
unsigned char encrypted[32];
...
AES_cbc_encrypt(original, encrypted, ...
And also something like:
unsigned char decrypted[32];
....
AES_decrypt(encrypted, decrypted, &aes_decryptKey);
Check out this link: http://marc.info/?l=openssl-users&m=122919878204439. I can't vouch for it all yet -- I'll come back and edit my answer later when I have time.
Jim, thanks for your help.
Seems I had to raise a question to find the answer.
After struggling many days this is what I came up with:
unsigned char inbuf[1024]="Hello,world!";
unsigned char encbuf[1024];
unsigned char key32[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
unsigned char deckey32[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa}
;
unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char deciv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
AES_KEY aeskey;
AES_KEY aesdeckey;
//Now enrypt
memset(encbuf, 0, sizeof(encbuf));
AES_set_encrypt_key(key32, 32*8, &aeskey);
AES_cbc_encrypt(inbuf, encbuf, 16, &aeskey, iv, AES_ENCRYPT);
//Now decrypt
unsigned char decbuf[1024];
memset(decbuf, 0, sizeof(decbuf));
AES_set_decrypt_key(deckey32, 32*8, &aesdeckey);
AES_cbc_encrypt(encbuf, decbuf, 16, &aesdeckey, deciv, AES_DECRYPT);
//Display the results
NSLog(#"ORIGINAL: \"%s\"\n", inbuf);
NSLog(#"ENCRYPTED: \"%s\"\n", encbuf);
NSLog(#"DECRYPTED: \"%s\"\n", decbuf);
return;
Credits to these guys (after Jim): http://www.mail-archive.com/openssl-users#openssl.org/msg50142.html
The key was to use AES_cbc_encrypt to decrypt.
Related
Considering having, for example, this type of hex string:
char hex_str[100] = "0x01 0x03 0x04 0x0A";
How to get out of this string the byte array representation in CAPL, like:
byte hex_str_as_byte_arr[4] = {0x01, 0x03, 0x04, 0x0A};
EDIT: Only Vector CANoe supported data types/functions are allowed!
Use strtok to split the character array into separate hex strings, then use long strtol( const char *restrict str, char **restrict str_end, int base ) to convert each hex string to an integral value.
Thanks to all...
Actually I've found a solution myself:
char hex_str[100] = "0x01 0x03 0x04 0x0A";
long data[4];
dword pos = 0;
pos = strtol(hex_str, pos, data[0]);
pos = strtol(hex_str, pos, data[1]);
pos = strtol(hex_str, pos, data[2]);
pos = strtol(hex_str, pos, data[3]);
write("0x%02x,0x%02x,0x%02x, 0x%02x", data[0], data[1], data[2], data[3]);
Now it's a simple cast: (byte) data[0]
We can use sscanf() to convert the numbers to unsigned char. In a loop, we'll need to also use a %n conversion to determine the reading position for the next iteration.
Here's a simple example (in real life, you'll need some range checking to make sure you don't overrun the output buffer):
#include <stdio.h>
int main(void)
{
const char hex_str[100] = "0x01, 0x03, 0x04, 0x0A";
unsigned char bytes[4];
{
int position;
unsigned char *b = bytes;
for (const char *input = hex_str; sscanf(input, "%hhi, %n", b, &position) == 1; ++b) {
input += position;
}
}
/* prove we did it */
for (size_t i = 0; i < sizeof bytes; ++i) {
printf("%hhu ", bytes[i]);
}
puts("");
}
int main()
{
unsigned short crc = 0x00;
unsigned char buffer[4] = {0x01,0x02,0x72,0xAE};
memcpy((void *)&crc, (void *)&buffer[2],2);
printf("crc = 0x%x \n",crc);
return 0;
}
for above program i was expecting crc value to be : 0x72AE but it comes out to be:
crc = 0xAE72
I am not able to understand why the bytes are shuffled, even though i am doing a memcpy?
Any kind of help will be appreciated. Thanks in advance.
Can anyone help converting the Int to char array
as i have buffer as
char *buffer = NULL;
int lengthOfComponent = -1;
char *obj;
buffer[index]= (char *)&lengthOfComponent;
if i do this it is thorwing EXCESS BAD ACCESS after the execution how to store the value of the obj to buffer using memcpy
Of course you cannot write in buffer[index], it is not allocated!
buffer = malloc(sizeof(char) * lengthOfBuffer);
should do it. After that you can write the buffer with memcpy or with an assignation, like you are doing.
buffer[index] = (char *)&lengthOfComponent;
buffer[index] is like dereferencing the pointer. But buffer is not pointing to any valid location. Hence the runtime error.
The C solution is using snprintf. Try -
int i = 11;
char buffer[10];
snprintf(buffer, sizeof(buffer), "%d", i);
ok
I'm using this to encrypt my data on iPhone:
- (NSData *)AES128EncryptWithKey:(NSString *)key{
char keyPtr[kCCKeySizeAES128 + 1]; // room for terminator (unused)
bzero( keyPtr, sizeof( keyPtr ) ); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode /*| kCCOptionPKCS7Padding*/,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted );
if( cryptStatus == kCCSuccess )
{
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free( buffer ); //free the buffer
return nil;}
on my server my php script uses:
$base64encoded_ciphertext = $pass;
mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($pass), 'ecb');
$decrypted = $res_non;
$dec_s2 = strlen($decrypted);
$padding = ord($decrypted[$dec_s2-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
However, no matter what the key is, it fails.
The keys are always 10 characters long. I build a password using the system clock to get values.
On the php side I duplicate the key building and according to my script, the key ALWAYS matches what the iPhone used to encrypt.
This code worked in a different script, from a different app… and still works.
I've done a dead cut and paste of all related code and still nothing.
I just don't know what I'm doing wrong… beyond what I'm trying to do maybe being absolutely impossible
For AES 128 you should use 16 byte Encryption key, AES 192 it is 24, AES 256 it is 32 byte. For your case you should use encryption key like #"1234567890123456".
I have this method to make a xor between 2 NSStrings, i´m printing the result on NSLog but it isn´t the expect.
Can´t figure out what i´m doing wrong.
(void)XorSecretKeyDeviceId
{
NSString* secretKey = #"123";//
NSString* deviceId = #"abcdef";//
NSData* stringKey = [secretKey dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringDeviceId = [deviceId dataUsingEncoding:NSUTF8StringEncoding];
unsigned char* pBytesInput = (unsigned char*)[stringKey bytes]; //Bytes
unsigned char* pBytesKey = (unsigned char*)[stringDeviceId bytes];
unsigned int vlen = [secretKey length]; //Keys Length
unsigned int klen = [deviceId length];
unsigned int v;
unsigned int k = vlen % klen;
unsigned char c;
for(v = 0; v < vlen; v++)
{
c = pBytesInput[v] ^ pBytesKey[k];
pBytesInput[v] = c;
NSLog(#"%c", c);
k = (++k < klen ? k : 0);
}
}
Are you setting your pBytesInput and pBytesKey variables correctly? At the moment, you have unsigned char* pBytesInput = (unsigned char*)[stringKey bytes]; (i.e. the input is the "key"), and pBytesKey is the device ID. This seems odd.
Also, be careful using UTF-8 encoding. UTF-8 uses the high bit on any byte in the string to indicate a "continuation" of a multi-byte character into the next byte. Your encoding could plausibly generate invalid UTF-8 by giving the setting the high bit of the final byte in the encryption.
For more than that, you'll have to say what the "wrong result" is.