- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我没有什么问题。当我尝试加密文本然后解密此文本时,出现错误:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
这是我的代码:
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author Grzesiek
*/
public class SymmethricCipherCBC {
/* Klucz: */
private byte[] keyBytes = new byte[] {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x00,0x01,0x02,0x03,0x04,0x05
};
/* Wektor inicjalizacyjny: */
private byte[] ivBytes = new byte[] {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x00,0x01,0x02,0x03,0x04,0x05
};
private Cipher cipher;
private SecretKeySpec keySpec;
private IvParameterSpec ivSpec;
public SymmethricCipherCBC() throws NoSuchAlgorithmException, NoSuchPaddingException{
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //Utworzenie obiektu dla operacji szyfrowania/deszyfrowania algorytmem AES w trybie CBC.
keySpec = new SecretKeySpec(keyBytes, "AES"); // Utworzenie obiektu klucza dla algorytmu AES z tablicy bajtow
ivSpec = new IvParameterSpec(ivBytes); // // Utworzenie obiektu dla wektora inicjalizacyjnego
}
public String encryptText(String plainText) throws NoSuchAlgorithmException,
InvalidKeyException,
NoSuchPaddingException,
InvalidAlgorithmParameterException,
ShortBufferException,
IllegalBlockSizeException,
BadPaddingException,
UnsupportedEncodingException{
int cipherTextLength;
byte[] cipherText; // Bufor dla szyfrogramu
byte[] plainTextBytes = plainText.getBytes(); // Reprezentacja tekstu jawnego w bajtach
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); //Inicjalizacja obiektu dla operacji szyfrowania z kluczem okreslonym przez keySpec:
cipherText = new byte[cipher.getOutputSize(plainTextBytes.length)]; //Utworzenie buforu dla szyfrogramu
cipherTextLength = cipher.update(plainTextBytes, 0, plainTextBytes.length, cipherText, 0); // Szyfrowanie tekstu jawnego
cipherTextLength += cipher.doFinal(cipherText, cipherTextLength); //Zakonczenie szyfrowania
return new BigInteger(1, cipherText).toString(16); // zapisanie 16
}
public String decryptText(String ciptherTextString) throws InvalidKeyException, InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
byte[] cipherTextBytes = ciptherTextString.getBytes();
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); //Inicjalizacja obiektu cipher dla odszyfrowywania z kluczem okreslonym przez keySpec
byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)]; // Utworzenie wyzerowanej tablicy
int plainTextLength = cipher.update(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0);
plainTextLength += cipher.doFinal(plainTextBytes, plainTextLength);
return new String(plainTextBytes); //Odtworzona wiadomosc
}
}
有什么想法我应该怎么做?
最佳答案
你做得比必要的更难,而且你在做的时候加密了你的密文
cipher.doFinal(cipherText, cipherTextLength);
我会按原样重写它:
public String encryptText(String plainText) throws ... {
byte[] plainTextBytes = plainText.getBytes("UTF8");
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encrypted = cipher.doFinal(plainTextBytes);
return toHex(encrypted);
}
public String decryptText(String cipherTextString) throws ... {
byte[] cipherTextBytes = fromHex(cipherTextString);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] plainTextBytes = cipher.doFinal(cipherTextBytes);
return new String(plainTextBytes, "UTF8");
}
关于java - 使用 AES CBC Java 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124936/
我只想使用这 3 种模式从 openSSL 测试 AES: key 长度为 128,192 和 256,但我的解密文本与我的输入不同,我不知道为什么。此外,当我传递一个巨大的输入长度(比如说 1024
我正在使用 CBC 求解器在 PyCharm 上解决优化问题,但一直遇到此错误: 错误:求解器 (cbc) 返回非零返回代码 (3221225781) 和 pyutilib.common._excep
Java AES/CBC/PKCS5PADDING 函数 public static String encrypt_key_data(String password, String message)
我安装了 Laravel 并将其上传到 git现在我下载了但是当我尝试进入该网站时,出现此错误: The only supported ciphers are AES-128-CBC and AES-
我对 C# 和加密比较陌生,所以请多多包涵。我正在将一些数据写入文件,我想使用 Aes (CBC) 对其进行加密,但我每天都会获取新数据,我想每周、每月、每年将所有数据写入一个文件,这取决于在内容上。
我在解密之前在 PHP 应用程序中加密的 pgcrypto 数据时遇到问题。 我尝试了 3 种加密方式: 1) mcrypt - RIJNDAEL 128 CBC 2) mcrypt - RIJNDA
Laravel 5.3(在 Windows 服务器上)我也有同样的问题。我做了所有可能的尝试:检查 .env 文件、配置、 artisan 新 key 生成、缓存:清除、配置:清除、 Composer
我目前正在使用 AES/CBC/PKCS5Padding 来加密具有 256 字节 key 大小的 Java 文件,但在搜索时我在 stackexchange PKCS#5-PKCS#7 Paddin
我使用 Composer 安装了 Laravel 5.3 的新副本,但收到此错误: The only supported ciphers are AES-128-CBC and AES-256-CBC
这是我的网络应用程序的 Laravel 5.4 设置。页面加载时重复发生一件事。因此,我无法在我的页面上获取数据。 运行时异常:唯一受支持的密码是具有正确 key 长度的 AES-128-CBC 和
我想知道在下面的代码中是否添加了 PKCS#5 填充?如果不是如何添加? $message = "insert plaintext message here"; $iv = pack('H*', '
我正在根据解释实现希尔密码 Wikipedia 但我想使用 CBC 模式来实现它,该模式表示每个 block 必须与前一个 block 进行异或,那么第一个 block 怎么样?学士学位每个 bloc
我在 PL/SQL 中有一个用于 aes256cbc(使用 PAD_PKCS5)的加密和解密函数,这似乎工作正常。 DECLARE -- https://docs.oracle.com/data
我正在从事一个 HIPAA 项目,我注意到我需要加密所有包含或可能包含私有(private)健康信息的列级数据。也就是说,我希望在 block_encryption_mode=aes-256-cbc
我正在尝试在 Linux 上开发一种安全程序。所以,我目前的计划是使用 AES128bit-CBC。我听说 OSX 基本上支持 AES 128。 Linux 上有这样的库吗? 最佳答案 libcryp
我在标准 ecb 模式下使用 nettle 的 twofish 没有问题,但是我不确定这个 cbc 模式有什么问题?解密的消息将与原始消息不匹配。 (仅出于测试目的使用一些硬编码值,如 iv)。 ht
最近项目需要选择一套对称加密算法,来满足前后端之间的加解密操作。初步打算前端使用crypto-js来实现,后端使用java本身的加密算法实现,但遇到了一个问题:java本身只支持NoPadding和
帮助,谁能给我一个 RSA/CBC 的 Java 实现? 审计署表示 RSA/ECB/PKCS1Padding 太弱,无法提交法庭。 (他实际上是在挑战 Java PKI 吗?他建议使用 AES...
我目前正在学习加密,我希望能找到更多关于我学到的东西的说明。 假设消息“100 美元应该从账户 123456 转移到 555555”是使用 aes-128-cbc 和随机 IV 加密的。我的教授说可以
我的 php 脚本和我的 c# 应用程序将相互传递一个 32 个字符长的哈希字符串,最好的模式是什么?我认为是欧洲央行,但我不确定是否使用超过 1 个区 block 就不要使用。我怎么知道 block
我是一名优秀的程序员,十分优秀!