文章目录

  • 1. 对称加密
  • 1.1. AES(Advanced Encryption Standard)
  • 1.2. DES(Data Encryption Standard)
  • 2. 非对称加密
  • 2.1. RSA(Rivest-Shamir-Adleman)
  • 3. 哈希函数
  • 在Java开发中,常用的加密方式包括以下几种

    MessageDigest(消息摘要):Java提供了java.security.MessageDigest类来进行散列算法的使用。通过MessageDigest类可以计算数据的哈希值,例如使用SHA-256、MD5等算法对用户信息进行散列处理。

    AES(高级加密标准):AES是一种常用的对称加密算法,适用于保护用户敏感信息。Java中可以使用javax.crypto包下的相关类进行AES加密和解密操作。

    RSA(非对称加密):RSA是一种常用的非对称加密算法,用于加密和解密敏感信息。Java中可以使用java.security包下的相关类进行RSA加密和解密操作。

    在 Java 中,常用的加密方式包括对称加密、非对称加密、哈希函数等。不同的加密方式适用于不同的安全需求,如数据加密、数字签名、密码存储等。以下是一些常用的加密方式及其 Java 实现示例:

    1. 对称加密

    对称加密算法使用相同的密钥进行加密和解密。常见的对称加密算法包括 AES 和 DES。

    1.1. AES(Advanced Encryption Standard)

    ● 用途:数据加密,支持不同的密钥长度(如128位、192位、256位)。
    ● Java示例:

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.util.Base64;
    
    public class AESExample {
        public static void main(String[] args) throws Exception {
            String plaintext = "Hello, AES!";
            
            // 生成密钥
            SecretKey key = generateKey();
            String keyString = Base64.getEncoder().encodeToString(key.getEncoded());
            System.out.println("Key: " + keyString);
    
            // 加密
            String ciphertext = encrypt(plaintext, key);
            System.out.println("Ciphertext: " + ciphertext);
    
            // 解密
            String decryptedText = decrypt(ciphertext, key);
            System.out.println("Decrypted Text: " + decryptedText);
        }
    
        // 生成AES密钥
        private static SecretKey generateKey() throws Exception {
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128); // 128位密钥
            return keyGen.generateKey();
        }
    
        // 加密
        private static String encrypt(String plaintext, SecretKey key) throws Exception {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
    
        // 解密
        private static String decrypt(String ciphertext, SecretKey key) throws Exception {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
            byte[] decryptedBytes = cipher.doFinal(decodedBytes);
            return new String(decryptedBytes);
        }
    }
    

    1.2. DES(Data Encryption Standard)

    ● 用途:较旧的加密标准,通常推荐使用 AES 替代。
    ● Java示例:

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.util.Base64;
    
    public class DESEncryptionExample {
        public static void main(String[] args) throws Exception {
            String plaintext = "Hello, DES!";
            
            // 生成密钥
            SecretKey key = generateKey();
            String keyString = Base64.getEncoder().encodeToString(key.getEncoded());
            System.out.println("Key: " + keyString);
    
            // 加密
            String ciphertext = encrypt(plaintext, key);
            System.out.println("Ciphertext: " + ciphertext);
    
            // 解密
            String decryptedText = decrypt(ciphertext, key);
            System.out.println("Decrypted Text: " + decryptedText);
        }
    
        // 生成DES密钥
        private static SecretKey generateKey() throws Exception {
            KeyGenerator keyGen = KeyGenerator.getInstance("DES");
            keyGen.init(56); // 56位密钥
            return keyGen.generateKey();
        }
    
        // 加密
        private static String encrypt(String plaintext, SecretKey key) throws Exception {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
    
        // 解密
        private static String decrypt(String ciphertext, SecretKey key) throws Exception {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
            byte[] decryptedBytes = cipher.doFinal(decodedBytes);
            return new String(decryptedBytes);
        }
    }
    

    2. 非对称加密

    非对称加密算法使用一对密钥(公钥和私钥)。常见的非对称加密算法包括 RSA 和 ECC。

    2.1. RSA(Rivest-Shamir-Adleman)

    ● 用途:加密和数字签名,通常用于安全通信。
    ● Java示例:

    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Security;
    import javax.crypto.Cipher;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    
    public class RSAExample {
        public static void main(String[] args) throws Exception {
            Security.addProvider(new BouncyCastleProvider());
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            KeyPair keyPair = keyGen.generateKeyPair();
    
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
    
            String plaintext = "Hello, RSA!";
    
            // 加密
            String ciphertext = encrypt(plaintext, publicKey);
            System.out.println("Ciphertext: " + ciphertext);
    
            // 解密
            String decryptedText = decrypt(ciphertext, privateKey);
            System.out.println("Decrypted Text: " + decryptedText);
        }
    
        // 加密
        private static String encrypt(String plaintext, PublicKey publicKey) throws Exception {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
    
        // 解密
        private static String decrypt(String ciphertext, PrivateKey privateKey) throws Exception {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
            byte[] decryptedBytes = cipher.doFinal(decodedBytes);
            return new String(decryptedBytes);
        }
    }
    

    3. 哈希函数

    哈希函数用于生成数据的唯一标识符,常用于数据完整性检查和密码存储。
    3.1. SHA-256(Secure Hash Algorithm 256-bit)
    ● 用途:生成数据的哈希值,广泛用于数据完整性校验。
    ● Java示例:

    import java.security.MessageDigest;
    
    public class SHA256Example {
        public static void main(String[] args) throws Exception {
            String plaintext = "Hello, SHA-256!";
            
            // 计算哈希值
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hashBytes = digest.digest(plaintext.getBytes());
            String hashString = bytesToHex(hashBytes);
            System.out.println("SHA-256 Hash: " + hashString);
        }
    
        // 辅助方法:将字节数组转换为十六进制字符串
        private static String bytesToHex(byte[] bytes) {
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        }
    }
    
    1. 总结
      ● 对称加密(如 AES、DES):用于数据加密,使用相同的密钥进行加密和解密。
      ● 非对称加密(如 RSA):用于加密和数字签名,使用一对密钥(公钥和私钥)。
      ● 哈希函数(如 SHA-256):用于生成数据的哈希值,确保数据完整性。
      每种加密方式有其特定的应用场景和安全特性。根据需要选择合适的加密方式可以有效地保护数据和通信安全。

    作者:思静语

    物联沃分享整理
    物联沃-IOTWORD物联网 » Java常用的加密方式

    发表回复