91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

java加密算法怎么應用

小億
90
2023-12-01 09:32:04
欄目: 編程語言

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加密算法的使用方法,包括生成密鑰、創建加密器、加密和解密操作。請注意,密鑰的長度必須滿足算法要求,否則會拋出異常。加密和解密時使用的密鑰必須匹配。

0
太谷县| 义马市| 盖州市| 滦平县| 永川市| 永福县| 日照市| 泌阳县| 嵊州市| 青海省| 南江县| 明水县| 丰宁| 缙云县| 祁连县| 安仁县| 双鸭山市| 榕江县| 云梦县| 卓尼县| 五大连池市| 平江县| 冕宁县| 杨浦区| 东港市| 上杭县| 通州市| 连云港市| 仙居县| 洪泽县| 阿图什市| 本溪市| 阿克| 丰都县| 金寨县| 新民市| 高陵县| 县级市| 龙门县| 左权县| 蓝山县|