Solved

Encryption in Visual C++

Posted on 2004-08-10
12
1,087 Views
Last Modified: 2013-12-14
hello!

I've to do a simple assignment on encryption and I came across this article. I wish to try it using Visual C++.
http://www.codeproject.com/netcf/teaencryption.asp

TEA encrypts 64 data bits at a time using a 128 bit key.
What I understand from the article is strings must be converted into array of bytes.
1) Can we convert a string to an array of bytes?
2) Say I have CString m_strInput and int TEA[16]. Is it correct if I assign 64bits of user-input data into every element of TEA? I don't quite get how to convert the string and split them up into the array.

I guess that's all for now. I think once I get the solution to this one I can proceed to understanding how TEA works. I've never done encryption before, I'm quite loss on how to start coding.

Thank you in advance for your help and guidance.


0
Comment
Question by:virlinz
12 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 11767104
I don't think you want to convert, rather just cast, i.e., look at the character string to be encrypted as an array of uints.

The easiest way to do this is with a union of the the string (char array) and a uint array:

char  cThing[128];
uint   uTHing[56];


0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11767543
If you want to work with CString, then you have to convert (because buffer will be converted to binary and could have null characters), but it is simple.

CString str = "some data here";
CByteArray arr;

arr.RemoveAll();    // Optional, to reuse it.
for (int i=0; i<str.GetLength(); i++)
     arr.Add(str[i]);

Now you can pass arr.GetData() to any function that requires it.
0
 
LVL 1

Accepted Solution

by:
rhodgson earned 50 total points
ID: 11773865
TEA is a block cipher and therefore not particuarly well suited to encrypting strings, because it requires that each encryption operation works on exactly 64 bit blocks.

If you want to encrypt a string then you need to use either

a) a padding system to make sure that the string is expanded so it is exactly divisible by a block size - typically one of the PKCS padding schemes are used.

b) use a steam cipher - by using CFB or OFB algorithms you can use a block cipher as a stream cipher - if you go down this route it is important to remember that you ALWAYS use the block cipher in encryption mode regardless of whether you are using encryption or decryption with the CFB/OFB algorithm
0
 

Author Comment

by:virlinz
ID: 11774545
thanks for providing me the answers.
I don't quite understand how CByteArray works. Do we use it like the normal array? What's the difference between the CDWordArray and the other array family members?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11774655
I thought you have discarded my answer, because it has not been accepted.
CByteArray is a Visual C++ MFC object that wraps and manages a simple byte array. It is similar to CDWordArray except that you fill it a byte at a time instead of a DWord (4 bytes) at a time.
As I said, you can use is by calling it's GetData member which returns you the pointer to the actual buffer (different from the pointer to the object itself).
The advantage of using a CArray derived class: you don't have to worry about memory allocation issues.
0
 

Author Comment

by:virlinz
ID: 11774658
btw sorry about the points. this is my first time posting a question so I got confused with the 'marking scheme' (I didn't read the FAQ first). I agree and accept all the answers. I'm really sorry about that :D
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:virlinz
ID: 11775590
Sorry about that Jaime. I thought I can accept all answers so I started pushing the button from the last answer :D
So for CByteArray, when copying using the for loop in ur example, str[0] means "s" while CDWordArray str[0]="some", am I right?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11775644
Yes, you are right.
0
 

Author Comment

by:virlinz
ID: 11776016
ok.. say after I transfer all this into the array I want to convert them into hexadecimal numbers. How can I do that? I can't use the Format() cuz it will convert into hex but as a CString type. I want the hex to be stored as int.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11776131
You can use Format() anyway as you expect because have to convert to hex every character, so you have to do a for... loop

CByteArray arr;

// some process who fills array here

CString hexString;
CString number;

hexString.Empty();
for (int i=0; i<arr.GetSize(); i++) {
     number.Format("%02X", arr[i]);
     hexString += number;
}
0
 

Author Comment

by:virlinz
ID: 11776463
I tried run the codes. curious to see what is stored in every hexString element, I add a few lines after your codes

CString output;
output=hexString[0];

I entered virlinz. I'm supposed to get  75 but instead only 7 was the output. How to get 75? Does this mean the hex string now takes two places (eg. hexString[0] and hexString[1]) for each converted char?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11776516
If you want a hex string representation, it will take 2 bytes for each converted char.
But you simply can use the CByteArray directly. arr[0] will be 75 (or its decimal representation, that depend just of point of view). Numbers are not stored as decimals or hexas, them are allways stored as binaries, decimal or hexa is just a display option.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 149
Modify a small python script 19 97
How to obtain Administrator permission when you are the Administrator 6 66
Advice in Xamarin 21 56
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now