![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Example of using AES with ECB/CBC/PCBC modes
main.cpp@0:da8611a6d1bb, 2015-01-28 (annotated)
- Committer:
- Geremia
- Date:
- Wed Jan 28 18:00:28 2015 +0000
- Revision:
- 0:da8611a6d1bb
Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Geremia | 0:da8611a6d1bb | 1 | #include "mbed.h" |
Geremia | 0:da8611a6d1bb | 2 | #include "Crypto.h" |
Geremia | 0:da8611a6d1bb | 3 | |
Geremia | 0:da8611a6d1bb | 4 | Serial pc(USBTX, USBRX); |
Geremia | 0:da8611a6d1bb | 5 | unsigned char myKEY[16] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,}; |
Geremia | 0:da8611a6d1bb | 6 | unsigned char myIV[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, } ; |
Geremia | 0:da8611a6d1bb | 7 | unsigned char msg[0x60] = |
Geremia | 0:da8611a6d1bb | 8 | { |
Geremia | 0:da8611a6d1bb | 9 | 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, |
Geremia | 0:da8611a6d1bb | 10 | 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, |
Geremia | 0:da8611a6d1bb | 11 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Geremia | 0:da8611a6d1bb | 12 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
Geremia | 0:da8611a6d1bb | 13 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
Geremia | 0:da8611a6d1bb | 14 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Geremia | 0:da8611a6d1bb | 15 | } ; |
Geremia | 0:da8611a6d1bb | 16 | int main() |
Geremia | 0:da8611a6d1bb | 17 | { |
Geremia | 0:da8611a6d1bb | 18 | pc.baud (115200); |
Geremia | 0:da8611a6d1bb | 19 | pc.printf("\r\nCleartext"); |
Geremia | 0:da8611a6d1bb | 20 | for(char i=0; i<0x60; i++) |
Geremia | 0:da8611a6d1bb | 21 | { |
Geremia | 0:da8611a6d1bb | 22 | if(i%16==0) pc.printf("\r\n"); |
Geremia | 0:da8611a6d1bb | 23 | pc.printf("%.2X",msg[i]); |
Geremia | 0:da8611a6d1bb | 24 | } |
Geremia | 0:da8611a6d1bb | 25 | |
Geremia | 0:da8611a6d1bb | 26 | // init crypto |
Geremia | 0:da8611a6d1bb | 27 | //AES myAES(AES_128, myKEY); // will default to ECB_MODE |
Geremia | 0:da8611a6d1bb | 28 | //AES myAES(AES_128, myKEY, myIV); // will default to CBC_MODE |
Geremia | 0:da8611a6d1bb | 29 | AES myAES(AES_128, myKEY, myIV, PCBC_MODE); // specify all params, look at BlockCipher.h for modes |
Geremia | 0:da8611a6d1bb | 30 | pc.printf("\r\n\r\nFirst run\r\n"); |
Geremia | 0:da8611a6d1bb | 31 | myAES.encrypt(msg,msg,0x60); // same in and out buffer can be used |
Geremia | 0:da8611a6d1bb | 32 | pc.printf("\r\nEncrypted"); |
Geremia | 0:da8611a6d1bb | 33 | for(char i=0; i<0x60; i++) |
Geremia | 0:da8611a6d1bb | 34 | { |
Geremia | 0:da8611a6d1bb | 35 | if(i%16==0) pc.printf("\r\n"); |
Geremia | 0:da8611a6d1bb | 36 | pc.printf("%.2X",msg[i]); |
Geremia | 0:da8611a6d1bb | 37 | } |
Geremia | 0:da8611a6d1bb | 38 | pc.printf("\r\nDecrypted again"); |
Geremia | 0:da8611a6d1bb | 39 | myAES.decrypt(msg,msg,0x60); |
Geremia | 0:da8611a6d1bb | 40 | for(char i=0; i<0x60; i++) |
Geremia | 0:da8611a6d1bb | 41 | { |
Geremia | 0:da8611a6d1bb | 42 | if(i%16==0) pc.printf("\r\n"); |
Geremia | 0:da8611a6d1bb | 43 | pc.printf("%.2X",msg[i]); |
Geremia | 0:da8611a6d1bb | 44 | } |
Geremia | 0:da8611a6d1bb | 45 | // change key and IV without reinit all |
Geremia | 0:da8611a6d1bb | 46 | unsigned char newKey[16]; |
Geremia | 0:da8611a6d1bb | 47 | unsigned char newIV[16]; |
Geremia | 0:da8611a6d1bb | 48 | memset(newKey, 0x22, 16); |
Geremia | 0:da8611a6d1bb | 49 | memset(newIV, 0x33, 16); |
Geremia | 0:da8611a6d1bb | 50 | myAES.keyExpansion(newKey); |
Geremia | 0:da8611a6d1bb | 51 | myAES.setIV(newIV); |
Geremia | 0:da8611a6d1bb | 52 | pc.printf("\r\n\r\nKey and IV changed\r\n"); |
Geremia | 0:da8611a6d1bb | 53 | unsigned char plaintext[0x60]; |
Geremia | 0:da8611a6d1bb | 54 | unsigned char ciphertext[0x60]; |
Geremia | 0:da8611a6d1bb | 55 | myAES.encrypt(ciphertext,msg,0x60); // different buffers if you have space |
Geremia | 0:da8611a6d1bb | 56 | pc.printf("\r\nEncrypted"); |
Geremia | 0:da8611a6d1bb | 57 | for(char i=0; i<0x60; i++) |
Geremia | 0:da8611a6d1bb | 58 | { |
Geremia | 0:da8611a6d1bb | 59 | if(i%16==0) pc.printf("\r\n"); |
Geremia | 0:da8611a6d1bb | 60 | pc.printf("%.2X",ciphertext[i]); |
Geremia | 0:da8611a6d1bb | 61 | } |
Geremia | 0:da8611a6d1bb | 62 | pc.printf("\r\nDecrypted again"); |
Geremia | 0:da8611a6d1bb | 63 | myAES.decrypt(plaintext,ciphertext,0x60); // different buffers if you have space |
Geremia | 0:da8611a6d1bb | 64 | for(char i=0; i<0x60; i++) |
Geremia | 0:da8611a6d1bb | 65 | { |
Geremia | 0:da8611a6d1bb | 66 | if(i%16==0) pc.printf("\r\n"); |
Geremia | 0:da8611a6d1bb | 67 | pc.printf("%.2X",plaintext[i]); |
Geremia | 0:da8611a6d1bb | 68 | } |
Geremia | 0:da8611a6d1bb | 69 | |
Geremia | 0:da8611a6d1bb | 70 | } |