欢迎访问宙启技术站
智能推送

在Java中使用函数实现简单的加密解密

发布时间:2023-07-24 05:10:28

在Java中,可以使用函数来实现简单的加密和解密操作。下面将介绍两种常见的加密解密算法:替换密码和移位密码。

1. 替换密码(Substitution Cipher):

替换密码是一种基于字母替换的简单加密算法。它通过将明文中的字母按照一定的规则替换为其他字母来实现加密。例如,将'A'替换为'B','B'替换为'C',以此类推。解密与加密过程相反。

以下是一个示例函数,用于实现替换密码的加密和解密操作:

public class SubstitutionCipher {
    public static String encrypt(String plaintext, int shift) {
        StringBuilder ciphertext = new StringBuilder();
        for (int i = 0; i < plaintext.length(); i++) {
            char c = plaintext.charAt(i);
            if (Character.isLetter(c)) {
                if (Character.isUpperCase(c)) {
                    char shiftedChar = (char) ((c - 'A' + shift) % 26 + 'A');
                    ciphertext.append(shiftedChar);
                } else {
                    char shiftedChar = (char) ((c - 'a' + shift) % 26 + 'a');
                    ciphertext.append(shiftedChar);
                }
            } else {
                ciphertext.append(c);
            }
        }
        return ciphertext.toString();
    }

    public static String decrypt(String ciphertext, int shift) {
        return encrypt(ciphertext, 26 - shift);
    }

    public static void main(String[] args) {
        String plaintext = "Hello, World!";
        int shift = 3;

        String encryptedText = encrypt(plaintext, shift);
        System.out.println("Encrypted text: " + encryptedText);

        String decryptedText = decrypt(encryptedText, shift);
        System.out.println("Decrypted text: " + decryptedText);
    }
}

输出:

Encrypted text: Khoor, Zruog!
Decrypted text: Hello, World!

2. 移位密码(Caesar Cipher):

移位密码是替换密码的一种特例,它只是简单地将字母按照一个指定的固定偏移量进行替换。例如,将明文中的每个字母按照固定的偏移量向后移动几个位置。解密过程就是将密文中的每个字母按照相同的偏移量向前移动相同的位置数。

以下是一个示例函数,用于实现移位密码的加密和解密操作:

public class CaesarCipher {
    public static String encrypt(String plaintext, int shift) {
        StringBuilder ciphertext = new StringBuilder();
        for (int i = 0; i < plaintext.length(); i++) {
            char c = plaintext.charAt(i);
            if (Character.isLetter(c)) {
                if (Character.isUpperCase(c)) {
                    char shiftedChar = (char) ((c - 'A' + shift) % 26 + 'A');
                    ciphertext.append(shiftedChar);
                } else {
                    char shiftedChar = (char) ((c - 'a' + shift) % 26 + 'a');
                    ciphertext.append(shiftedChar);
                }
            } else {
                ciphertext.append(c);
            }
        }
        return ciphertext.toString();
    }

    public static String decrypt(String ciphertext, int shift) {
        return encrypt(ciphertext, 26 - shift);
    }

    public static void main(String[] args) {
        String plaintext = "Hello, World!";
        int shift = 3;

        String encryptedText = encrypt(plaintext, shift);
        System.out.println("Encrypted text: " + encryptedText);

        String decryptedText = decrypt(encryptedText, shift);
        System.out.println("Decrypted text: " + decryptedText);
    }
}

输出:

Encrypted text: Khoor, Zruog!
Decrypted text: Hello, World!

以上是使用函数在Java中实现简单的加密解密操作的示例代码。这些算法只是最基本的加密算法之一,不适合用于严格安全要求的场景。如果需要更强大的加密算法,请使用Java提供的加密库,如javax.crypto包中的算法。