Home  >  Article  >  Java  >  How to use Java and Linux script operations for file encryption and decryption

How to use Java and Linux script operations for file encryption and decryption

王林
王林Original
2023-10-05 12:48:111169browse

How to use Java and Linux script operations for file encryption and decryption

How to use Java and Linux script operations for file encryption and decryption

Introduction: File encryption and decryption is a common information security technology. Through encryption, files can be Content cannot be seen and modified by unauthorized persons, thus protecting file security. This article will introduce how to use Java and Linux script operations for file encryption and decryption, and provide specific code examples.

1. Use Java for file encryption and decryption
Java is a programming language widely used in software development. It has good cross-platform properties and a rich API library, and can easily implement file encryption and decryption functions. . The following is a sample code for file encryption and decryption using Java:

  1. File encryption code sample:

    import java.io.*;
    
    public class FileEncryption {
     public static void main(String[] args) {
         String sourceFile = "source.txt";
         String encryptedFile = "encrypted.txt";
         String key = "mykey";
    
         try {
             FileInputStream inputStream = new FileInputStream(sourceFile);
             FileOutputStream outputStream = new FileOutputStream(encryptedFile);
    
             byte[] buffer = new byte[1024];
             int bytesRead;
    
             while ((bytesRead = inputStream.read(buffer)) != -1) {
                 for (int i = 0; i < bytesRead; i++) {
                     buffer[i] = (byte) (buffer[i] ^ key.getBytes()[i % key.length()]);
                 }
                 outputStream.write(buffer, 0, bytesRead);
             }
    
             inputStream.close();
             outputStream.close();
    
             System.out.println("文件加密完成!");
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    }
  2. File decryption code sample:

    import java.io.*;
    
    public class FileDecryption {
     public static void main(String[] args) {
         String encryptedFile = "encrypted.txt";
         String decryptedFile = "decrypted.txt";
         String key = "mykey";
    
         try {
             FileInputStream inputStream = new FileInputStream(encryptedFile);
             FileOutputStream outputStream = new FileOutputStream(decryptedFile);
    
             byte[] buffer = new byte[1024];
             int bytesRead;
    
             while ((bytesRead = inputStream.read(buffer)) != -1) {
                 for (int i = 0; i < bytesRead; i++) {
                     buffer[i] = (byte) (buffer[i] ^ key.getBytes()[i % key.length()]);
                 }
                 outputStream.write(buffer, 0, bytesRead);
             }
    
             inputStream.close();
             outputStream.close();
    
             System.out.println("文件解密完成!");
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    }

In the above code example, the file encryption and decryption process is achieved by reading the byte stream of the file content and performing an XOR operation on each byte. The same key is used for encryption and decryption, and the encryption and decryption operations of the information are implemented by recycling each byte of the key to perform an XOR operation on the file content.

2. Use Linux scripts for file encryption and decryption
In addition to using Java for file encryption and decryption, you can also use the script language of the Linux system, such as Shell script, to implement file encryption and decryption functions. The following is a sample code for file encryption and decryption using Shell script:

  1. File encryption script example:

    #!/bin/bash
    
    sourceFile="source.txt"
    encryptedFile="encrypted.txt"
    key="mykey"
    
    while read -r line || [[ -n "$line" ]]; do
     encryptedLine=""
     for ((i=0; i<${#line}; i++)); do
         encryptedChar=$(printf "%d" "'${line:$i:1}")
         encryptedChar=$((encryptedChar ^ $(printf "%d" "'${key:$((i%${#key})):1}")))
         encryptedChar=$(printf "x$(printf "%x" "$encryptedChar")")
         encryptedLine+="${encryptedChar}"
     done
     echo "${encryptedLine}" >> "${encryptedFile}"
    done < "${sourceFile}"
    
    echo "文件加密完成!"
  2. File decryption script example:

    #!/bin/bash
    
    encryptedFile="encrypted.txt"
    decryptedFile="decrypted.txt"
    key="mykey"
    
    while read -r line || [[ -n "$line" ]]; do
     decryptedLine=""
     for ((i=0; i<${#line}; i++)); do
         decryptedChar=$(printf "%d" "'${line:$i:1}")
         decryptedChar=$((decryptedChar ^ $(printf "%d" "'${key:$((i%${#key})):1}")))
         decryptedChar=$(printf "x$(printf "%x" "$decryptedChar")")
         decryptedLine+="${decryptedChar}"
     done
     echo "${decryptedLine}" >> "${decryptedFile}"
    done < "${encryptedFile}"
    
    echo "文件解密完成!"

In the above code example, the file encryption and decryption process uses the characteristics of the Shell script, reading the file content line by line, and performing an XOR operation on each character. The same key is used for encryption and decryption, and the encryption and decryption operations of information are implemented by recycling each character of the key to perform an XOR operation on the file content.

Conclusion: This article introduces how to use Java and Linux scripts to encrypt and decrypt files, and provides specific code examples. Whether you choose to use Java or Linux scripts, you can easily implement file encryption and decryption functions. Based on actual needs, you can modify and optimize the sample code to meet your specific encryption and decryption needs. Hope this article helps you!

The above is the detailed content of How to use Java and Linux script operations for file encryption and decryption. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn