Java暗号化アルゴリズムの適用方法

Javaでは、暗号化アルゴリズムとして、DES、AES、RSAなどがよく使われます。以下にこれらのアルゴリズムの利用例を示します。

  1. DES暗号アルゴリズムの応用例:
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

public class DESExample {
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, BadPaddingException, IllegalBlockSizeException {
        String plaintext = "Hello, world!";
        String keyString = "mysecretkey"; // 密钥,长度必须为8字节

        // 生成密钥
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        DESKeySpec desKeySpec = new DESKeySpec(keyString.getBytes());
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

        // 创建加密器
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // 加密
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
        System.out.println("Encrypted: " + new String(encryptedBytes));

        // 创建解密器
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        // 解密
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        System.out.println("Decrypted: " + new String(decryptedBytes));
    }
}
  1. AES 暗号化アルゴリズムの適用例:
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public class AESExample {
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        String plaintext = "Hello, world!";
        String keyString = "mysecretkey123456"; // 密钥,长度必须为16字节

        // 生成密钥
        Key key = new SecretKeySpec(keyString.getBytes(), "AES");

        // 创建加密器
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        // 加密
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
        System.out.println("Encrypted: " + new String(encryptedBytes));

        // 创建解密器
        cipher.init(Cipher.DECRYPT_MODE, key);

        // 解密
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        System.out.println("Decrypted: " + new String(decryptedBytes));
    }
}
  1. RSA暗号化アルゴリズムの適用例:
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;

public class RSAExample {
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        String plaintext = "Hello, world!";

        // 生成密钥对
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        KeyPair keyPair = keyGen.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // 创建加密器
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        // 加密
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        System.out.println("Encrypted: " + new String(encryptedBytes));

        // 创建解密器
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        // 解密
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        System.out.println("Decrypted: " + new String(decryptedBytes));
    }
}

これらのサンプルでは、DES、AES、RSA 暗号化アルゴリズムの使用方法をそれぞれ示しています。これには、キーの生成、暗号化ツールの作成、暗号化と復号化の操作が含まれます。キーの長さはアルゴリズムの要件を満たす必要があることに注意してください。そうしないと、例外がスローされます。暗号化に使用されるキーと復号化に使用されるキーは一致する必要があります。

bannerAds