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

Java中基于AES-256密码的加密/解密

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

Java中基于AES-256密码的加密/解密

我认为您犯了两个错误:)

我已更正您的示例代码以使其正常工作:

import java.security.AlgorithmParameters;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.PBEKeySpec;import javax.crypto.spec.SecretKeySpec;import javax.xml.bind.DatatypeConverter;public class EncryptionDecryption {    private static String salt;    private static int iterations = 65536  ;    private static int keySize = 256;    private static byte[] ivBytes;    private static SecretKey secretKey;    public static void main(String []args) throws Exception {        salt = getSalt();        char[] message = "PasswordToEncrypt".toCharArray();        System.out.println("Message: " + String.valueOf(message));        System.out.println("Encrypted: " + encrypt(message));        System.out.println("Decrypted: " + decrypt(encrypt(message).toCharArray()));    }    public static String encrypt(char[] plaintext) throws Exception {        byte[] saltBytes = salt.getBytes();        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");        PBEKeySpec spec = new PBEKeySpec(plaintext, saltBytes, iterations, keySize);        secretKey = skf.generateSecret(spec);        SecretKeySpec secretSpec = new SecretKeySpec(secretKey.getEnpred(), "AES");        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        cipher.init(Cipher.ENCRYPT_MODE, secretSpec);        AlgorithmParameters params = cipher.getParameters();        ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();        byte[] encryptedTextBytes = cipher.doFinal(String.valueOf(plaintext).getBytes("UTF-8"));        return DatatypeConverter.printbase64Binary(encryptedTextBytes);    }    public static String decrypt(char[] encryptedText) throws Exception {        System.out.println(encryptedText);        byte[] encryptedTextBytes = DatatypeConverter.parsebase64Binary(new String(encryptedText));        SecretKeySpec secretSpec = new SecretKeySpec(secretKey.getEnpred(), "AES");        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        cipher.init(Cipher.DECRYPT_MODE, secretSpec, new IvParameterSpec(ivBytes));        byte[] decryptedTextBytes = null;        try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes);        }   catch (IllegalBlockSizeException e) { e.printStackTrace();        }   catch (BadPaddingException e) { e.printStackTrace();        }        return new String(decryptedTextBytes);    }    public static String getSalt() throws Exception {        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");        byte[] salt = new byte[20];        sr.nextBytes(salt);        return new String(salt);    }}

第一个错误是您生成2种不同的盐(使用加密方法时),因此加密/解密的日志是不同的(逻辑上,但是解密仍然有效,因为您在加密后立即调用解密)。

第二个错误是密钥。在加密而不是解密时,需要生成一个秘密密钥。简而言之,好像我正在使用密码“ encrypt”进行加密一样,而您正在尝试使用密码“
decrypt”对其进行解密。

我建议您在启动时生成所有随机的东西(例如私钥,盐等)。但要注意,当您停止应用程序时,除非获得完全相同的随机内容,否则您将无法解密旧内容。

希望我能帮到:)

问候,



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

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

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