簡體   English   中英

使用Java AES 256密鑰加密/解密

[英]Encrypting/Decrypting with Java AES 256 keys

我正在嘗試將我的應用程序從128位AES密鑰升級到256位AES密鑰。 但是,當我將第54行從128更改為256時,我得到以下鍵大小錯誤。

java.security.InvalidKeyException:非法的密鑰大小

我正確安裝了JCE文件,因為我的應用程序生成更長的密鑰。

package com.company;
import com.hazelcast.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.*;

class Encryption {

    public static String encrypt (String strKey, String strIv, String str) {
        String secret = "";
        try{
            byte[] key = Base64.decode(strKey.getBytes());
            byte[] iv  = Base64.decode(strIv.getBytes());

            SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            secret = new String(Base64.encode(cipher.doFinal(str.getBytes())));

        }
        catch(Exception e){
            e.printStackTrace();
        }
        return secret;
    }

    public static String decrypt (String strKey, String strIv, String str) {
        String secret = "";
        try{

            byte[] key = Base64.decode(strKey.getBytes());
            byte[] iv  = Base64.decode(strIv.getBytes());


            SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, keyspec,ivspec);
            secret = new String(cipher.doFinal(new Base64().decode(str.getBytes())));

        }
        catch(Exception e){
            e.printStackTrace();
        }
        return secret;
    }

    public static void main(String[] argv) {
        String strIv = "18A5Z/IsHs6g8/65sBxkCQ==";
        String strKey = "";
        int keyStrength = 256;
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(keyStrength);

            SecretKey skey = kgen.generateKey();
            byte[] raw = skey.getEncoded();
            strKey = new String(new Base64().encode(raw));
            System.out.println("Secret key is: " + strKey);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        String message = "My, it's a lovely day today!!!";
        String encrypted = Encryption.encrypt(strKey, strIv, message);
        System.out.println("Encrypted string is: " + encrypted);
        System.out.println("Decrypted string is: " + Encryption.decrypt(strKey, strIv, encrypted));


    }
}

我看過其他帖子中引用的“AES / CBC / PKCS7Padding”加密方法,但這只是讓我遇到這個例外:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
    at javax.crypto.Cipher.getInstance(Cipher.java:540)
    at com.company.Encryption.encrypt(Encryption.java:17)
    at com.company.Encryption.main(Encryption.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
    at javax.crypto.Cipher.getInstance(Cipher.java:540)
    at com.company.Encryption.decrypt(Encryption.java:39)
    at com.company.Encryption.main(Encryption.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

找到了...

Oracle在技術上標記了下載頁面上JCE文件的版本,但對我來說並不明顯。 我用java 8 JRE安裝java 6文件。 這就是我能夠生成密鑰但不加密/解密的原因。

我找到了正確的文件,安裝了它們,現在它正在運行。

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM