21xrx.com
2024-12-22 22:37:48 Sunday
登录
文章检索 我的文章 写文章
Java加密与解密技术详解
2023-06-19 13:21:14 深夜i     --     --
Java 加密 解密

在现代互联网时代中,安全已经变得越来越重要。在数据传输和存储中,人们会使用各式各样的加密技术来确保信息的安全性。Java作为一种高度安全的编程语言,提供了一系列的加密和解密技术,包括对称加密、非对称加密等。

1. 对称加密

对称加密也被称为共享密钥加密,它使用的是同样的密钥进行加密和解密。典型的对称加密算法包括DES、TripleDES、AES等。下面是一个使用AES算法进行对称加密解密的示例代码。


import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

public class SymmetricEncryption {

  private static final String ALGORITHM = "AES";

  private static final byte[] KEY = "MySecretKey12345".getBytes();

  public static byte[] encrypt(String data) throws Exception {

    SecretKeySpec secretKeySpec = new SecretKeySpec(KEY, ALGORITHM);

    Cipher cipher = Cipher.getInstance(ALGORITHM);

    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

    return cipher.doFinal(data.getBytes());

  }

  public static String decrypt(byte[] encryptedData) throws Exception {

    SecretKeySpec secretKeySpec = new SecretKeySpec(KEY, ALGORITHM);

    Cipher cipher = Cipher.getInstance(ALGORITHM);

    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

    return new String(cipher.doFinal(encryptedData));

  }

  public static void main(String[] args) throws Exception {

    String testData = "Java Symmetric Encryption and Decryption Example";

    byte[] encryptedData = encrypt(testData);

    String decryptedData = decrypt(encryptedData);

    System.out.println("Original Data: " + testData);

    System.out.println("Encrypted Data: " + new String(encryptedData));

    System.out.println("Decrypted Data: " + decryptedData);

  }

}

2. 非对称加密

非对称加密也被称为公钥加密,它使用的是一对密钥进行加密和解密。用户可以使用公钥进行加密,但只有持有私钥的用户才能进行解密。典型的非对称加密算法包括RSA、DSA等。


import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.Signature;

public class AsymmetricEncryption {

  private static final String ALGORITHM = "RSA";

  public static byte[] sign(String data, PrivateKey privateKey) throws Exception {

    Signature signature = Signature.getInstance(ALGORITHM);

    signature.initSign(privateKey);

    signature.update(data.getBytes());

    return signature.sign();

  }

  public static boolean verify(String data, byte[] signatureBytes, PublicKey publicKey) throws Exception {

    Signature signature = Signature.getInstance(ALGORITHM);

    signature.initVerify(publicKey);

    signature.update(data.getBytes());

    return signature.verify(signatureBytes);

  }

  public static void main(String[] args) throws Exception {

    String testData = "Java Asymmetric Encryption and Decryption Example";

    KeyPair keyPair = KeyPairGenerator.getInstance(ALGORITHM).generateKeyPair();

    PrivateKey privateKey = keyPair.getPrivate();

    PublicKey publicKey = keyPair.getPublic();

    byte[] signature = sign(testData, privateKey);

    boolean verified = verify(testData, signature, publicKey);

    System.out.println("Original Data: " + testData);

    System.out.println("Signature: " + new String(signature));

    System.out.println("Verified: " + verified);

  }

}

3. 关键词

Java、加密、解密

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复