tools:encrypt files when tar them

本文介绍如何在Unix系统中使用tar命令创建受密码保护的.tgz文件。提供了多种方法,如使用ccrypt、gpg等工具进行加密,并解释了这些工具的基本用法。

https://superuser.com/questions/370389/how-do-i-password-protect-a-tgz-file-with-tar-in-unix

 

2028  tar zcvpf - demo_bak/ |ccrypt > demo_bak.tar.tgz.cpt
 2029  ls
 2030  mkdir test
 2031  mv demo_bak.tar.tgz.cpt test/
 2032  ls
 2033  cd test/
 2034  ls
 2035  tar -zxvpf demo_bak.tar.tgz.cpt
 2036  man ccrypt 
 2037  ccrypt -d demo_bak.tar.tgz.cpt 
 2038  ls
 2039  tar -zxvpf demo_bak.tar.tgz 
 2040  ls
 2041  history -30
 2042  history |tail -30

 

 

 

 

How do I password protect a .tgz file with tar in Unix?

Ask Question

I'm using the Unix tar command as follows to tar up a directory and its files:

tar cvzf fileToTar.tgz directoryToTar

Is there a way to password protect the .tgz file? I've created password-protected ZIP files on Windows so I would assume Unix has the same capability. Any ideas?

linux unix tar archiving password-protection

shareimprove this question

edited Mar 18 '14 at 14:39

vdd

10722 bronze badges

asked Dec 21 '11 at 19:00

c12

31611 gold badge33 silver badges44 bronze badges

migrated from stackoverflow.com Dec 21 '11 at 21:00

This question came from our site for professional and enthusiast programmers.

add a comment

5 Answers

activeoldestvotes

37

 

Use crypt or gpg on the file.

Simple examples:

cat filename | crypt > filename.crypt

gpg -c –o filename.gpg filename

shareimprove this answer

edited Oct 13 '16 at 8:12

 

Tanky Woo

13011 silver badge1010 bronze badges

answered Dec 21 '11 at 19:08

Christopher Neylan

63177 silver badges77 bronze badges

  • 2

    this makes no sense, where is the password? – Alexander Mills May 7 at 19:27

  • 2

    @AlexanderMills Most password-accepting tools prompt the user for it from the terminal rather than a command line argument, as to prevent the password showing up in history. – Daffy May 26 at 3:52

add a comment

33

 

You can use command:

zip -P password file.zip file

Or better:

zip -e file.zip file

man zip

shareimprove this answer

edited Jun 20 '12 at 8:14

mtk

67222 gold badges1313 silver badges2828 bronze badges

answered Dec 22 '11 at 6:27

Panta

43133 silver badges22 bronze badges

add a comment

16

 

Neither the tar format nor the gz format has built-in support for password-protecting files.

The Windows zip format combines several different piece of functionality: compression (e.g. gzip), archiving multiple files into one (e.g. tar), encryption (e.g. gnupg), and probably others. Unix tends to have individual tools, each of which does one thing well, and lets you combine them.

The Unix equivalent of a password-protected .zip file would probably be called something like foo.tar.gz.gpg or foo.tgz.gpg.

And there are open-source zip and unzip tools for Unix, though they may not provide all the capabilities of the Windows versions (I'm fairly sure the newer .zipx format isn't supported).

shareimprove this answer

edited Dec 21 '11 at 22:21

 

Chris W. Rea

7,7471414 gold badges7070 silver badges9494 bronze badges

answered Dec 21 '11 at 21:44

Keith Thompson

4,43022 gold badges1919 silver badges3131 bronze badges

add a comment

12

 

You can use gpg (=GnuPG):

gpg -o fileToTar.tgz.gpg --symmetric fileToTar.tgz

This will prompt you for a passphrase.

To decrypt the file later on, just do a:

gpg fileToTar.tgz.gpg

This will prompt you, again, for the passphrase.

shareimprove this answer

answered Jul 20 '15 at 5:57

thiagowfx

1,15411 gold badge1212 silver badges1616 bronze badges

  • Note: -c is short for --symmetric, i.e., use the default symmetric cipher, which means that the same passphrase is used for both encryption and decryption. (As opposed to asymmetric, which involves public keys and private keys.) – Evgeni Sergeev Nov 24 '17 at 5:30

add a comment

7

 

You can use ccrypt.

Things can be encrypted by a pipe:

tar cvvjf - /path/to/files | ccrypt > backup.tar.bz2.cpt

Or in place:

ccrypt backup.tar.bz2

For automating, you can save a passkey into a file and use this passkey to encrypt:

ccrypt -k ~/.passkey backup.tar.bz2

shareimprove this answer

### LZ4 Compression Using C Language on Linux Environment In a Linux environment, implementing LZ4 compression using the C language involves several steps to ensure efficient data handling and performance optimization. The process typically includes setting up the development environment, installing required libraries, writing code for compression and decompression functions, compiling this code, and executing it. #### Setting Up Development Environment To begin working with LZ4 in C under Linux: Ensure that the system has GCC installed as well as other essential build tools such as `make`. Additionally, one must obtain the official LZ4 source files from its repository or website. After downloading these sources, follow standard procedures like extracting them via commands similar to those mentioned previously[^2]: ```bash tar -zxvf lz4_v*.tar.gz && cd lz4_v* ``` Then configure the project according to specific needs before building it locally: ```bash make sudo make install ``` This installs both static (.a) and dynamic (.so) versions of the library along with command-line utilities which can be used directly without additional coding effort. #### Writing Code for Compression & Decompression Functions Once everything is set up correctly, developers may proceed by including appropriate headers within their programs while adhering closely to API documentation provided alongside each release version of LZ4. Below demonstrates how simple yet effective implementation might look when performing basic operations over strings stored inside memory buffers rather than disk-based I/O streams. For compressing content: ```c #include <stdio.h> #include "lz4.h" int main() { const char *input = "Example string"; int inputSize = strlen(input); // Allocate space for compressed output based upon maximum possible size. unsigned long maxCompressedSize = LZ4_compressBound(inputSize); void* buffer = malloc(maxCompressedSize); // Perform actual compression operation here... int resultLength; if ((resultLength = LZ4_compress_default((const char*)input, (char *)buffer, inputSize, maxCompressedSize)) == 0){ printf("Compression failed\n"); free(buffer); return -1; } // Output results after successful completion. printf("Original Size : %d bytes.\n", inputSize); printf("Compressed Size: %d bytes.\n", resultLength); free(buffer); } ``` Similarly, for decompressing compressed contents back into original form: ```c // Assuming 'compressedData' points towards already processed binary sequence... unsigned long destSize; // Must match expected uncompressed length exactly! if(LZ4_decompress_safe(compressedData, destinationBuffer, sizeof(compressedData), destSize) != destSize){ fprintf(stderr,"Decompression error occurred."); } else{ puts(destinationBuffer); } ``` These examples provide fundamental insights regarding usage patterns associated specifically with LZ4's public interface exposed through header files included during compilation phases. --related questions-- 1. How does the choice between static linking versus dynamic linking impact application deployment strategies involving LZ4? 2. What are some common pitfalls encountered while integrating third-party compression algorithms like LZ4 into existing projects written primarily in C/C++ languages? 3. Can you explain what benefits come from utilizing multithreaded approaches available within newer releases of LZ4 compared against single-thread implementations? 4. In terms of security considerations, should applications encrypt sensitive information prior to applying lossless compression techniques offered by libraries such as LZ4?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值