Add ch7_library sample code
New file |
| | |
| | | /******************************************************************************** |
| | | * Copyright: (C) 2018 LingYun IoT System Studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: crypto.h |
| | | * Description: This head file is LingYun crypto library head file |
| | | * |
| | | * Version: 1.0.0(2018年12月20日) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "2018年12月20日 12时32分09秒" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #ifndef _CRYPTO_H_ |
| | | #define _CRYPTO_H_ |
| | | |
| | | /* Description: This function used to encrypto message in $plaintext, and put ciphertext into $ciphertext |
| | | * Return value: 0: Success <0: Failure |
| | | */ |
| | | extern int encrypt(char *plaintext, char *ciphertext, int size); |
| | | |
| | | /* Description: This function used to decrypto message in $ciphertext, and put plaintext into $plaintext |
| | | * Return value: 0: Success <0: Failure |
| | | */ |
| | | extern int decrypt(char *ciphertext, char *plaintext, int size); |
| | | |
| | | #endif /* ----- #ifndef _CRYPTO_H_ ----- */ |
| | | |
New file |
| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2018 LingYun IoT System Studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: crypto.c |
| | | * Description: This file is LingYun crypto library |
| | | * |
| | | * Version: 1.0.0(2018年12月20日) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "2018年12月20日 11时56分04秒" |
| | | * |
| | | ********************************************************************************/ |
| | | #include <string.h> |
| | | |
| | | int encrypt(char *plaintext, char *ciphertext, int size) |
| | | { |
| | | int i; |
| | | |
| | | if( size < strlen(plaintext) ) |
| | | { |
| | | return -1; |
| | | } |
| | | |
| | | for(i=0; i<strlen(plaintext); i++) |
| | | { |
| | | ciphertext[i] = plaintext[i]+3; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | int decrypt(char *ciphertext, char *plaintext, int size) |
| | | { |
| | | int i; |
| | | |
| | | if( size < strlen(ciphertext) ) |
| | | { |
| | | return -1; |
| | | } |
| | | |
| | | for(i=0; i<strlen(ciphertext); i++) |
| | | { |
| | | plaintext[i] = ciphertext[i]-3; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
New file |
| | |
| | | /******************************************************************************** |
| | | * Copyright: (C) 2018 LingYun IoT System Studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: crypto.h |
| | | * Description: This head file is LingYun crypto library head file |
| | | * |
| | | * Version: 1.0.0(2018年12月20日) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "2018年12月20日 12时32分09秒" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #ifndef _CRYPTO_H_ |
| | | #define _CRYPTO_H_ |
| | | |
| | | /* Description: This function used to encrypto message in $plaintext, and put ciphertext into $ciphertext |
| | | * Return value: 0: Success <0: Failure |
| | | */ |
| | | extern int encrypt(char *plaintext, char *ciphertext, int size); |
| | | |
| | | /* Description: This function used to decrypto message in $ciphertext, and put plaintext into $plaintext |
| | | * Return value: 0: Success <0: Failure |
| | | */ |
| | | extern int decrypt(char *ciphertext, char *plaintext, int size); |
| | | |
| | | #endif /* ----- #ifndef _CRYPTO_H_ ----- */ |
| | | |
New file |
| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2018 LingYun IoT System Studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: main.c |
| | | * Description: This file is LingYun Crypto library test program |
| | | * |
| | | * Version: 1.0.0(2018年12月20日) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "2018年12月20日 12时33分32秒" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #include <stdio.h> |
| | | #include <string.h> |
| | | #include "crypto.h" |
| | | |
| | | int main(int argc, char *argv[]) |
| | | { |
| | | char *message="abcdefghijklmnopq"; |
| | | char ciphertext[64]; |
| | | char plaintext[64]; |
| | | |
| | | memset(ciphertext, 0, sizeof(ciphertext)); |
| | | if( encrypt(message, ciphertext, sizeof(ciphertext))< 0 ) |
| | | { |
| | | printf("encrypt plaintext failure\n"); |
| | | return -1; |
| | | } |
| | | printf("encrypt ciphertext: %s\n", ciphertext); |
| | | |
| | | |
| | | memset(plaintext, 0, sizeof(plaintext)); |
| | | if( decrypt(ciphertext, plaintext, sizeof(plaintext))< 0 ) |
| | | { |
| | | printf("decrypt ciphertext failure\n"); |
| | | return -1; |
| | | } |
| | | printf("decrypt plaintext: %s\n", plaintext); |
| | | |
| | | |
| | | return 0; |
| | | } |