栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Java进行AES加密和解密

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Java进行AES加密和解密

在这里,您需要了解的是密文可能包含不可打印的字符。因此,当您使用readLine()时,它可能不会为您提供文件中的所有字节。

同样,

byteCipherText.toString()
它并没有给您您认为会得到的。在Java中,该
toString()
方法不提供数组内容的字符串表示形式。

无需在加密的文本中添加填充。它已经被填充。

import java.nio.file.Files;import java.nio.file.Paths;import javax.crypto.*;public class Main {    public static void main(String[] args) throws Exception {        String fileName = "encryptedtext.txt";        String fileName2 = "decryptedtext.txt";        KeyGenerator keyGen = KeyGenerator.getInstance("AES");        keyGen.init(128);        SecretKey secKey = keyGen.generateKey();        Cipher aesCipher = Cipher.getInstance("AES");        byte[] byteText = "Your Plain Text Here".getBytes();        aesCipher.init(Cipher.ENCRYPT_MODE, secKey);        byte[] byteCipherText = aesCipher.doFinal(byteText);        Files.write(Paths.get(fileName), byteCipherText);        byte[] cipherText = Files.readAllBytes(Paths.get(fileName));        aesCipher.init(Cipher.DECRYPT_MODE, secKey);        byte[] bytePlainText = aesCipher.doFinal(cipherText);        Files.write(Paths.get(fileName2), bytePlainText);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569421.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号