user-0 提问: Linux下如何使用openssl库进行DES/CBC/PKCS5Padding加密后用base64编码

回复:
user: user-0
content: int kk_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,              unsigned char *iv, unsigned char *ciphertext) 196 { 197     EVP_CIPHER_CTX *ctx; 198  199     int len; 200  201     int ciphertext_len; 202     ctx = EVP_CIPHER_CTX_new(); 203     EVP_EncryptInit_ex(ctx, EVP_des_ecb(), NULL, key, iv); 204  205     EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len); 206     ciphertext_len = len; 207     EVP_EncryptFinal_ex(ctx, ciphertext + len, &len); 208     ciphertext_len += len; 209     EVP_CIPHER_CTX_free(ctx); 210  211     return ciphertext_len; 212 } 213  214 //解密 215 int kk_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, 216                 unsigned char *iv, unsigned char *plaintext) 217 { 218         EVP_CIPHER_CTX *ctx; 219  220         int len; 221         int plaintext_len; 222  223         ctx = EVP_CIPHER_CTX_new(); 224  225         EVP_DecryptInit_ex(ctx, EVP_des_ecb(), NULL, key, iv); 226         EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len); 227         plaintext_len = len; 228  229         EVP_DecryptFinal_ex(ctx, plaintext + len, &len); 230         plaintext_len += len; 231  232         EVP_CIPHER_CTX_free(ctx); 233  234         return plaintext_len; 235 }  char * base64Encode(const char *buffer, int length, bool newLine) {     BIO *bmem = NULL;     BIO *b64 = NULL;     BUF_MEM *bptr;     b64 = BIO_new(BIO_f_base64());     if (!newLine) {         BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);     }     bmem = BIO_new(BIO_s_mem());     b64 = BIO_push(b64, bmem);     BIO_write(b64, buffer, length);     BIO_flush(b64);     BIO_get_mem_ptr(b64, &bptr);     BIO_set_close(b64, BIO_NOCLOSE);     char *buff = (char *)malloc(bptr->length + 1);     memcpy(buff, bptr->data, bptr->length);     buff[bptr->length] = 0;     BIO_free_all(b64);     return buff; } char * base64Decode(char *input, int length, bool newLine) {         BIO *b64 = NULL;         BIO *bmem = NULL;         char *buffer = (char *)malloc(length);         memset(buffer, 0, length);         b64 = BIO_new(BIO_f_base64());         if (!newLine) {                 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);         }         bmem = BIO_new_mem_buf(input, length);         bmem = BIO_push(b64, bmem);         BIO_read(bmem, buffer, length);         BIO_free_all(bmem);         return buffer; } 这是我这边C的解密代码

user: user-0
content: 这是他边提供的C#解码编码  #region DESEncrypt DES加密  /// <summary>  /// 进行DES加密。  /// </summary>  /// <param name="pToEncrypt">要加密的字符串。</param>  /// <param name="sKey">密钥，且必须为8位。</param>  /// <returns>以Base64格式返回的加密字符串。</returns>  public static string DESEncrypt(string pToEncrypt, string sKey)  {  using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())  {  byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);  des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  System.IO.MemoryStream ms = new System.IO.MemoryStream();  using (CryptoStream cs = new CryptoStream(ms,  des.CreateEncryptor(), CryptoStreamMode.Write))  {  cs.Write(inputByteArray, 0, inputByteArray.Length);  cs.FlushFinalBlock();  cs.Close();  }  string str = Convert.ToBase64String(ms.ToArray());  ms.Close();  return str;  }  }  #endregion  #region DESDecrypt DES解密  /// <summary>  /// 进行DES解密。  /// </summary>  /// <param name="pToDecrypt">要解密的以Base64</param>  /// <param name="sKey">密钥，且必须为8位。</param> /// <returns>已解密的字符串。</returns>  public static string DESDecrypt(string pToDecrypt, string sKey)  {  byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);  using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())  {  des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  System.IO.MemoryStream ms = new System.IO.MemoryStream();  using (CryptoStream cs = new CryptoStream(ms,  des.CreateDecryptor(), CryptoStreamMode.Write))  {  cs.Write(inputByteArray, 0, inputByteArray.Length);  cs.FlushFinalBlock();  cs.Close();  }  string str = Encoding.UTF8.GetString(ms.ToArray());  ms.Close();  return str;  }  }  #endregion

user: user-1
content: 参考下吧： https://blog.csdn.net/p0x1307/article/details/40115567

user: user-2
content: 百度一下很多吧 比如https://blog.csdn.net/hanghang121/article/details/51435008  但是有一个注意点就是key的生成 很多时候,提供的并不是key,而是一个password 要从password生成key,方法有很多,这个加密方和解密方需要协商 比如先对password进行一次MD5,取结果前12(24)字节作为key

user: user-0
content: 引用 5 楼 早打大打打核战争 的回复:字符串编码不一样 使用了openssl 的命令是发现用16进制处理后的密码进行加密。

user: user-0
content: 引用 5 楼 早打大打打核战争 的回复:字符串编码不一样 使用了openssl 的命令是发现用16进制处理后的密码进行加密。

user: user-3
content: 字符串编码不一样