首页 > Java > java教程 > 正文

如何进行Java功能开发的数据加密与解密

WBOY
发布: 2023-08-06 18:25:05
原创
1140人浏览过

如何进行java功能开发的数据加密与解密

数据加密与解密在信息安全领域中扮演着重要的角色。在Java开发中,有多种方法可以实现数据加密与解密功能。本文将介绍使用Java编程语言进行数据加密与解密的方法,并提供代码示例。

1.对称加密算法

对称加密算法使用相同的密钥进行数据的加密和解密。其中常用的对称加密算法有DES、3DES、AES等。

代码示例:

立即学习Java免费学习笔记(深入)”;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

public class SymmetricEncryptionExample {
    public static void main(String[] args) throws Exception {
        String plaintext = "Hello, World!";
        String secretKey = "0123456789abcdef";
        
        // Generate Key
        SecretKey key = new SecretKeySpec(secretKey.getBytes(), "AES");

        // Create Cipher
        Cipher cipher = Cipher.getInstance("AES");
        
        // Encrypt
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        System.out.println("Encrypted Text: " + encryptedText);

        // Decrypt
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        String decryptedText = new String(decryptedBytes);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}
登录后复制

2.非对称加密算法

非对称加密算法使用一对密钥,公钥用于加密,私钥用于解密。在Java中,常用的非对称加密算法有RSA。

代码示例:

立即学习Java免费学习笔记(深入)”;

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

public class AsymmetricEncryptionExample {
    public static void main(String[] args) throws Exception {
        String plaintext = "Hello, World!";

        // Generate Key Pair
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
        keyGenerator.initialize(2048);
        KeyPair keyPair = keyGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // Create Cipher
        Cipher cipher = Cipher.getInstance("RSA");

        // Encrypt
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        System.out.println("Encrypted Text: " + encryptedText);

        // Decrypt
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        String decryptedText = new String(decryptedBytes);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}
登录后复制

3.哈希算法

哈希算法可以将任意长度的数据转换为固定长度的哈希值,常用的哈希算法有MD5、SHA-1、SHA-256等。

代码示例:

立即学习Java免费学习笔记(深入)”;

import java.security.MessageDigest;

public class HashAlgorithmExample {
    public static void main(String[] args) throws Exception {
        String plaintext = "Hello, World!";

        // Create MessageDigest
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        // Hash
        byte[] hashedBytes = md.digest(plaintext.getBytes());
        StringBuilder stringBuilder = new StringBuilder();
        for (byte hashedByte : hashedBytes) {
            stringBuilder.append(Integer.toHexString(0xFF & hashedByte));
        }
        String hashedText = stringBuilder.toString();
        System.out.println("Hashed Text: " + hashedText);
    }
}
登录后复制

总结:

本文介绍了使用Java开发实现数据加密与解密的方法和代码示例。在实际开发中,根据不同需求选择合适的加密算法和密钥长度,以确保数据的安全性。

以上就是如何进行Java功能开发的数据加密与解密的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号