久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

AES 錯(cuò)誤:給定的最終塊未正確填充

AES Error: Given final block not properly padded(AES 錯(cuò)誤:給定的最終塊未正確填充)
本文介紹了AES 錯(cuò)誤:給定的最終塊未正確填充的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我需要有關(guān)此錯(cuò)誤的幫助:給定最終塊未正確填充.從標(biāo)題可以看出,我正在使用 AES.

I need help with this error: Given final block not properly padded. As you can see from the title, I am working with AES.

這是錯(cuò)誤所在行的代碼:

Here is the code of line where is error:

 byte[] decrypted = cipher.doFinal(bytes);

這里是完整的代碼:

public class AESCrypt {
private final Cipher cipher;
private final SecretKeySpec key;
private String encryptedText, decryptedText;

public AESCrypt(String password) throws Exception {
    // hash password with SHA-256 and crop the output to 128-bit for key
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(password.getBytes("UTF-8"));
    byte[] keyBytes = new byte[16];
    System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    key = new SecretKeySpec(keyBytes, "AES");
}

public String encrypt(String plainText) throws Exception {
    byte[] iv = new byte[cipher.getBlockSize()];
    new SecureRandom().nextBytes(iv);
    AlgorithmParameterSpec spec = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, spec);
    byte[] encrypted = cipher.doFinal(plainText.getBytes());
    encryptedText = asHex(encrypted);
    return encryptedText;
}

public String decrypt(String cryptedText) throws Exception {
    byte[] iv = new byte[cipher.getBlockSize()];
    AlgorithmParameterSpec spec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, spec);
    // decrypt the message
    byte[] bytes = cryptedText.getBytes("UTF-8");
    byte[] decrypted = cipher.doFinal(bytes);
    decryptedText = asHex(decrypted);
    System.out.println("Desifrovani tekst: " + decryptedText + "
");

    return decryptedText;
}

public static String asHex(byte buf[]) {
    StringBuilder strbuf = new StringBuilder(buf.length * 2);
    int i;
    for (i = 0; i < buf.length; i++) {
        if (((int) buf[i] & 0xff) < 0x10) {
            strbuf.append("0");
        }
        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }
    return strbuf.toString();
}

public static void main(String[] args) throws Exception {

    System.out.print("....AES....
");

    String message = "MESSAGE";
    String password = "PASSWORD";

    System.out.println("MSG:" + message);

    AESCrypt aes = new AESCrypt(password);
    String encryptedText = aes.encrypt(message).toString();
    System.out.println("SIFROVANA PORUKA: " + encryptedText);
    String decryptedText = aes.decrypt(encryptedText).toString();       
    System.out.print("DESIFROVANA PORUKA: " + decryptedText);
}

}

推薦答案

根據(jù)您的評(píng)論,您已經(jīng)非常接近讓加密貨幣工作了.

Per your comment, you are pretty close to getting the crypto working.

您需要將 IV 生成代碼從您的加密/解密方法移到其他地方,就像這樣

You need to move the IV generation code from your encryption/decryption methods to somewhere else, like so

public AlgorithmParameterSpec getIV() {
AlgorithmParameterSpec ivspec;
byte[] iv = new byte[cipher.getBlockSize()];
new SecureRandom().nextBytes(iv);
ivspec = new IvParameterSpec(iv);
}

然后將該 ivspec 傳遞給加密和解密方法(使它們看起來像 encrypt(String,AlgorithmParameterSpec)),以便加密和解密具有相同的 iv.

then pass that ivspec into both the encrypt and decrypt methods (making them look like encrypt(String,AlgorithmParameterSpec)), so that you have the same iv for both encryption and decryption.

另外,不要在decryptedByteArray上調(diào)用printBase64Binary,而是調(diào)用new String(decryptedByteArray, "UTF-8")

Also, don't call printBase64Binary on the decryptedByteArray, instead call new String(decryptedByteArray, "UTF-8")

這篇關(guān)于AES 錯(cuò)誤:給定的最終塊未正確填充的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
Using BouncyCastle to encrypt with ECIES in Java(在 Java 中使用 BouncyCastle 使用 ECIES 進(jìn)行加密)
主站蜘蛛池模板: 国产高清一区二区 | 91精品一区 | 爱高潮www亚洲精品 中文字幕免费视频 | 综合久久综合久久 | 国产精品美女www爽爽爽视频 | 色综合桃花网 | 五月婷婷丁香婷婷 | 国产特一级黄色片 | 男人的天堂avav | 亚洲天堂中文字幕 | 日本高清视频在线播放 | 色在线免费视频 | 国内精品一区二区 | 视频一区欧美 | 亚洲欧美在线视频 | 久草视| 国产亚洲精品美女久久久久久久久久 | 国产一区 在线视频 | 久久久精品一区二区 | 精品视频在线观看 | 日韩久久成人 | 精品国产乱码久久久久久蜜柚 | 亚洲精品一区中文字幕乱码 | 午夜精品在线 | 成人久久18免费网站图片 | 日本成人在线网址 | 成人久久网 | 三级黄色大片网站 | 91九色视频 | 精品欧美乱码久久久久久1区2区 | 久久成人综合 | 日韩成人一区 | 久久久www成人免费无遮挡大片 | 97精品超碰一区二区三区 | 日本网站在线看 | 中文字幕日韩在线 | 欧美一级在线免费观看 | 国产午夜三级一区二区三 | 成人久久视频 | 久久久久九九九女人毛片 | 日本一卡精品视频免费 |