tea algorithm

hi guys, i`m trying to figure out how to use these two methods of tea algorithm that i have gotten from wikipedia.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdint.h>
 
void encrypt (uint32_t* v, uint32_t* k) {
    uint32_t v0=v[0], v1=v[1], sum=0, i;           /* set up */
    uint32_t delta=0x9e3779b9;                     /* a key schedule constant */
    uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
    for (i=0; i < 32; i++) {                       /* basic cycle start */
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);  
    }                                              /* end cycle */
    v[0]=v0; v[1]=v1;
}
 
void decrypt (uint32_t* v, uint32_t* k) {
    uint32_t v0=v[0], v1=v[1], sum=0xC6EF3720, i;  /* set up */
    uint32_t delta=0x9e3779b9;                     /* a key schedule constant */
    uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
    for (i=0; i<32; i++) {                         /* basic cycle start */
        v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
        v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        sum -= delta;                                   
    }                                              /* end cycle */
    v[0]=v0; v[1]=v1;
}


i have been trying to initialize a uint32_t variable to contain a string. However it says invalid conversion from char * to unint32_t. How do i pump variables into these two methods?
and how do i declare uint32_t variables?
i also want to know how to convert an int to uint32_t type!

thanks in advance! =)
1
2
int x = 5;
uint32_t y = x;

Compiler will issue warning about conversion between signed and unsigned types. Actually it is better to use uint32_t to store info related to encryption everywhere.
thanks man!
so i will do the same for string?

correct me if i`m wrong
uint32_t always converts variables to 32 bits?

and i`m having trouble in encrypting my data with the tea algorithm
i converted the string values to ascii values and stored them in a vector.
Then i called them out in another for loop to encrypt them, however i keep getting segmentation faults. May i know whats wrong with my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int main()
{

    string userInput;
    int getKey=0x95a8882c;
    uint32_t *TEAKey=(uint32_t *) getKey;
    vector<int> storeAscii;

    cout<<"5-bit CFB TEA algorithm program"<<endl;
    cout<<"please enter the message to be encrypted :";
    cin>>userInput;


   for(int i=0;i<userInput.size();i++)
   {
       storeAscii.push_back((int)userInput[i]);

   }

   for(int p=0;p<userInput.size();p++)
   {
       int asciiValue=storeAscii[p];
       uint32_t * getASCII=(uint32_t * ) asciiValue;
      
       encrypt(getASCII,TEAKey);
   }






}
Last edited on
Topic archived. No new replies allowed.