java加密算法怎么应用

48次阅读
没有评论

共计 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字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)