java常用加密

1.对称加密

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

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryption {
    public static byte[] encrypt(String key, byte[] data) throws Exception {
        SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, spec);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(String key, byte[] encrypted) throws Exception {
        SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, spec);
        return cipher.doFinal(encrypted);
    }
}

2.非对称加密

非对称加密使用一对公钥和私钥进行加密和解密。常见的非对称加密算法有RSA、DSA和ECC。

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;

public class RSAEncryption {
    public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(PrivateKey privateKey, byte[] encrypted) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encrypted);
    }

    public static KeyPair generateKeyPair(int keySize) throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(keySize);
        return keyPairGenerator.generateKeyPair();
    }
}

3.消息摘要

消息摘要算法是一种单向哈希函数,它将任意长度的消息压缩到固定长度的摘要值。常见的消息摘要算法有MD5、SHA-1和SHA-256。

import java.security.MessageDigest;

public class MessageDigestExample {
    public static byte[] sha256(byte[] data) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(data);
    }
}

4.数字签名

数字签名是一种用于验证消息完整性和认证消息来源的机制。它使用私钥对消息的摘要值进行加密,并将加密后的值和原始消息一起传递给接收方。接收方使用公钥对加密值进行解密,并验证摘要值是否与原始消息的摘要值相同。

import java.security.KeyPair;
   import java.security.KeyPairGenerator;
   import java.security.PrivateKey;
   import java.security.PublicKey;
   import java.security.Signature;
   
   public class DigitalSignatureExample {
       public static byte[] sign(PrivateKey privateKey, byte[] data) throws Exception {
           Signature signature = Signature.getInstance("SHA256withRSA");
           signature.initSign(privateKey);
           signature.update(data);
           return signature.sign();
       }
   
       public static boolean verify(PublicKey publicKey, byte[] data, byte[] signature) throws Exception {
           Signature verify = Signature.getInstance("SHA256withRSA");
           verify.initVerify(publicKey);
           verify.update(data);
           return verify.verify(signature);
       }
   
       public static KeyPair generateKeyPair(int keySize) throws Exception {
           KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
           keyPairGenerator.initialize(keySize);
           return keyPairGenerator.generateKeyPair();
       }
   }

你可能感兴趣的:(java,开发语言,jvm)