21xrx.com
2024-11-22 02:48:56 Friday
登录
文章检索 我的文章 写文章
Java业务场景下的数据加密方案
2023-06-15 16:20:48 深夜i     --     --
Java 数据加密 对称加密 非对称加密

在现代的互联网时代,数据的安全性愈发重要。作为一名Java开发者,我们需要在业务实现过程中保障数据安全性,最常用的方法之一就是数据加密。本文将介绍Java业务场景下的数据加密方案,并提供代码案例。

在Java中,常见的加密方案包括对称加密和非对称加密。对称加密指的是使用同一密钥进行加密和解密的方式,优点是加密速度快,缺点是密钥管理麻烦;非对称加密指的是使用公钥进行加密,私钥进行解密的方式,优点是密钥管理方便,缺点是加解密速度慢。

以下是对称加密的示例代码:


import java.security.Key;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

public class SymmetricEncryption {

  private static final String ALGORITHM = "AES";

  private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";

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

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

    Key secretKey = new SecretKeySpec(KEY, ALGORITHM);

    Cipher cipher = Cipher.getInstance(TRANSFORMATION);

    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    return cipher.doFinal(data);

  }

  public static byte[] decrypt(byte[] data) throws Exception {

    Key secretKey = new SecretKeySpec(KEY, ALGORITHM);

    Cipher cipher = Cipher.getInstance(TRANSFORMATION);

    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    return cipher.doFinal(data);

  }

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

    String originalString = "hello world";

    byte[] encryptedString = encrypt(originalString.getBytes());

    byte[] decryptedString = decrypt(encryptedString);

    System.out.println("Original string: " + originalString);

    System.out.println("Encrypted string: " + new String(encryptedString));

    System.out.println("Decrypted string: " + new String(decryptedString));

  }

}

上述代码中使用了AES算法进行加密和解密,密钥为"mySecretKey12345",使用ECB模式和PKCS5Padding填充。可以根据具体业务需求进行调整。

在非对称加密的示例代码中,我们使用RSA算法进行加密和解密:


import java.io.ByteArrayOutputStream;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.SecureRandom;

import javax.crypto.Cipher;

public class AsymmetricEncryption {

  private static final int KEY_SIZE = 1024;

  private static final String ALGORITHM = "RSA";

  private static final String TRANSFORMATION = "RSA/ECB/PKCS1Padding";

  public static byte[] encrypt(byte[] data, PublicKey pubKey) throws Exception {

    Cipher cipher = Cipher.getInstance(TRANSFORMATION);

    cipher.init(Cipher.ENCRYPT_MODE, pubKey);

    return blockCipher(cipher, data, Cipher.ENCRYPT_MODE);

  }

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

    Cipher cipher = Cipher.getInstance(TRANSFORMATION);

    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    return blockCipher(cipher, data, Cipher.DECRYPT_MODE);

  }

  private static byte[] blockCipher(Cipher cipher, byte[] bytes, int mode) throws Exception {

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    int blockSize = cipher.getBlockSize();

    int bytesLength = bytes.length;

    int chunksLength = bytesLength / blockSize + (bytesLength % blockSize != 0 ? 1 : 0);

    for (int chunkIndex = 0; chunkIndex < chunksLength; chunkIndex++) {

      int chunkLength = Math.min(blockSize, bytesLength - chunkIndex * blockSize);

      out.write(cipher.doFinal(bytes, chunkIndex * blockSize, chunkLength));

    }

    return out.toByteArray();

  }

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

    String originalString = "hello world";

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

    byte[] encryptedString = encrypt(originalString.getBytes(), keyPair.getPublic());

    byte[] decryptedString = decrypt(encryptedString, keyPair.getPrivate());

    System.out.println("Original string: " + originalString);

    System.out.println("Encrypted string: " + new String(encryptedString));

    System.out.println("Decrypted string: " + new String(decryptedString));

  }

}

上述代码中使用RSA算法进行加密和解密,使用了1024位的密钥,使用ECB模式和PKCS1Padding填充。需要注意的是,RSA算法的安全性依赖于公钥和私钥的安全性,因此需要合理管理密钥。

总体来说,在Java业务场景下,数据加密是非常必要的一项工作。本文介绍了对称加密和非对称加密两种常见的加密方案,并提供了代码案例。

  
  

评论区

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