久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java加密算法怎么應用

157次閱讀
沒有評論

共計 3079 個字符,預計需要花費 8 分鐘才能閱讀完成。

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

丸趣 TV 網 – 提供最優質的資源集合!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-09發表,共計3079字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 金山区| 古丈县| 宾川县| 陇南市| 和顺县| 灯塔市| 高平市| 井研县| 女性| 兰州市| 蓬安县| 孙吴县| 钟祥市| 阜阳市| 理塘县| 长宁区| 湖北省| 酒泉市| 济阳县| 岳普湖县| 金溪县| 银川市| 宁蒗| 巨野县| 平南县| 贵港市| 揭西县| 老河口市| 合江县| 白朗县| 永仁县| 正安县| 开江县| 揭东县| 朝阳县| 独山县| 华安县| 科技| 鄱阳县| 宣汉县| 柳林县|