chinaEncrypt.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "chinaEncrypt.h"
  2. #include "CMBSMDLL.h"
  3. #include <stdio.h>
  4. #include <windows.h>
  5. int SM3Hash(unsigned char* pData, int nLen, unsigned char hash[32])
  6. {
  7. return CMBSM3Digest(pData, nLen, hash);
  8. }
  9. char* GetSMVersion()
  10. {
  11. return CMBSMGetVersion();
  12. }
  13. int CreateSM2KeyPair(unsigned char* pPubKeyBuf, int* pPubKeyBufLen, unsigned char* pPriKeyBuf, int* pPriKeyBufLen)
  14. {
  15. return CMBSM2KeyGen(pPubKeyBuf, pPubKeyBufLen, pPriKeyBuf, pPriKeyBufLen);
  16. }
  17. int EncWithSM2PubKey(unsigned char* pSource, int nSourceLen, unsigned char* pDest, int* pDestLen, unsigned char* pPubKey, int nKeyLen)
  18. {
  19. return CMBSM2Encrypt(pPubKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
  20. }
  21. int DecWithSM2PriKey(unsigned char* pSource, int nSourceLen, unsigned char* pDest, int* pDestLen, unsigned char* pPriKey, int nKeyLen)
  22. {
  23. return CMBSM2Decrypt(pPriKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
  24. }
  25. int SM2SignWithSM3(unsigned char* privkey, int privkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int* sig_len)
  26. {
  27. return CMBSM2SignWithSM3(privkey, privkey_len, msg, msg_len, signature, sig_len);
  28. }
  29. int SM2VerifyWithSM3(unsigned char* pubkey, int pubkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int sig_len)
  30. {
  31. return CMBSM2VerifyWithSM3(pubkey, pubkey_len, msg, msg_len, signature, sig_len);
  32. }
  33. void GenerateSM4_ECBkey(char *keyStr, int keyStr_len, unsigned char key[16])
  34. {
  35. unsigned char tempKey[32] = { 0 };
  36. if (keyStr != NULL)
  37. {
  38. SM3Hash((unsigned char *)keyStr, keyStr_len > 32 ? 32 : keyStr_len, tempKey);
  39. memcpy(key, tempKey, 16);
  40. }
  41. else
  42. memset(key, 0, 16);
  43. //Aschex_2_bcdhex(tempKey, 32, key);
  44. }
  45. int EncStrWithSM4_ECB(char *keyStr, int keyStr_len, unsigned char* input, int length, unsigned char* output, int* output_len)
  46. {
  47. unsigned char key[17] = { 0 };
  48. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  49. return CMBSM4EncryptWithECB(key, input, length, output, output_len);
  50. }
  51. int DecStrWithSM4_ECB(char *keyStr, int keyStr_len, unsigned char* input, int length, unsigned char* output, int* output_len)
  52. {
  53. unsigned char key[17] = { 0 };
  54. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  55. return CMBSM4DecryptWithECB(key, input, length, output, output_len);
  56. }
  57. int EncWithSM4_ECB(unsigned char key[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  58. {
  59. return CMBSM4EncryptWithECB(key, input, length, output, output_len);
  60. }
  61. int DecWithSM4_ECB(unsigned char key[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  62. {
  63. return CMBSM4DecryptWithECB(key, input, length, output, output_len);
  64. }
  65. int EncStrWithSM4_CBC(char *keyStr, int keyStr_len, unsigned char iv[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  66. {
  67. unsigned char key[17] = { 0 };
  68. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  69. return CMBSM4EncryptWithCBC(key, iv, input, length, output, output_len);
  70. }
  71. int DecStrWithSM4_CBC(char *keyStr, int keyStr_len, unsigned char iv[16], unsigned char* input, int length, unsigned char* output, int* output_len)
  72. {
  73. unsigned char key[17] = { 0 };
  74. GenerateSM4_ECBkey(keyStr, keyStr_len, key);
  75. return CMBSM4DecryptWithCBC(key, iv, input, length, output, output_len);
  76. }
  77. int SM3File(char* file, unsigned char hash[32])
  78. {
  79. return CMBSM3FileDigest(file, hash);
  80. }