我正在尝试实现基于密码的加密算法,但是我得到了这个例外:
javax.crypto.BadPaddingException:给定最终块未正确填充
可能是什么问题?(我是Java新手.)
这是我的代码:
public class PasswordCrypter {
private Key key;
public PasswordCrypter(String password) {
try{
KeyGenerator generator;
generator = KeyGenerator.getInstance("DES");
SecureRandom sec = new SecureRandom(password.getBytes());
generator.init(sec);
key = generator.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
}
public byte[] encrypt(byte[] array) throws CrypterException {
try{
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(array);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public byte[] decrypt(byte[] array) throws CrypterException{
try{
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(array);
} …
Run Code Online (Sandbox Code Playgroud) 我正在使用Cipher实现Cipher.getInstance(String algorithm)
.我的印象是,我可能传递的可用算法名称因我的类路径中存在的库而异.
我想编写一个简单的程序,我可以使用不同的类路径运行,列出可用的Cipher算法名称.获取此列表需要调用哪种方法?
我正在研究一个Java身份验证子系统,该子系统规定在DB中存储密码作为PBKDF2
生成的哈希,我现在正在尝试决定是否应该使用SHA1
或SHA512
作为PFR.我仔细检查了两者的规格,但我们在数学上非常密集地跟随它.有更好的加密理解的人可以解释一下有什么PBKDF2WithHmacSHA512
不同PBKDF2WithHmacSHA1
吗?
这是我正在尝试做的事情:
private static final int HASH_BYTE_SIZE = 64; // 512 bits
private static final int PBKDF2_ITERATIONS = 1000;
// generate random salt
SecureRandom random = new SecureRandom();
byte salt[] = new byte[SALT_BYTE_SIZE]; // use salt size at least as long as hash
random.nextBytes(salt);
// generate Hash
PBEKeySpec spec = new PBEKeySpec(password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); // we would like this to be "PBKDF2WithHmacSHA512" instead? What Provider …
Run Code Online (Sandbox Code Playgroud) 当我使用javax.crypto.Mac编译一个类时,我收到此错误消息?
包javax.crypto不存在
我可以通过在我的编译类路径中包含jre/lib/jce.jar来解决它.
为什么jce.jar不在默认的jdk类路径上?jre/lib/rt.jar在类路径上,包含其他javax包,但jce看起来很特别?
在Java加密库中,键有两种不同的表示形式 - Key
和KeySpec
.文档暗示两者之间存在差异 - 一个KeySpec
是'透明'(无论这意味着什么),但没有方法,而Key
有一个getEncoded
方法.你打算使用a KeyFactory
来转换它们(它确实有一个getKeySpec
转换方法).
但是,SecretKeySpec
实现Key
和KeySpec
!但是也有一个SecretKeyFactory
类,它不会继承KeyFactory
.
这一切让我彻底迷茫.什么是之间的不同Key
和KeySpec
,以及如何做SecretKeySpec
,并SecretKeyFactory
进入呢?
我正在寻找 CryptoKit 的设置/参数,这将允许我在 iOS 应用程序和 Java 应用程序之间共享数据。流程如下所示: - 使用 CryptoKit 使用固定密钥和随机初始化向量 (IV) 加密文本。- 在 Java 应用程序中,使用标准 javax 库使用相同的固定密钥执行解密。随机 IV 将与加密文本一起与应用程序传输/共享。
同样,反过来也是必需的,其中使用 JavaX 库使用固定密钥和随机 IV 对文本进行加密。随机 IV 和加密文本与 iOS 应用程序共享,它应该使用 CryptoKit 对其进行解密。
下面是Java中加密和解密的代码
public static byte[] encrypt(byte[] plaintext, byte[] key, byte[] IV) throws Exception
{
// Get Cipher Instance
Cipher cipher = Cipher.getInstance("AES_256/GCM/NoPadding");
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
// Create GCMParameterSpec
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);
// Initialize Cipher for ENCRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec); …
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码段来加密/解密我的应用程序数据库中的数据:
http://www.androidsnippets.com/encryptdecrypt-strings
似乎javax.crypto.KeyGenerator.generateKey()操作在Android 2.3.3操作系统中的工作方式与其他(以前的?)版本不同.当然,这会给我的用户带来一个主要问题,当他们将设备从2.2升级到2.3.3并且应用程序开始抛出解密数据库的错误.
这是一个已知的问题?我是否错误地使用了加密库?任何人都有任何关于如何解决这个问题的建议,以便2.2中加密的数据能够在2.3.3中解密?
我构建了一个测试应用程序,通过加密功能提供值.当我在2.2 AVD上运行它时,我得到一个结果.当我在2.3.3 AVD上运行时,我得到了不同的结果.
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class main extends Activity {
TextView tvOutput;
static String out;
String TEST_STRING = "abcdefghijklmnopqrstuvwxyz";
String PASSKEY = "ThePasswordIsPassord";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvOutput = (TextView) findViewById(R.id.tvOutput);
}
@Override
public void onResume() {
super.onResume();
out = "";
runTest();
tvOutput.setText(out);
}
private void runTest() …
Run Code Online (Sandbox Code Playgroud) 我已成功使用javax.crypto.Cipher.getInstance("DESede/CBC/NoPadding")在Android上使用DESFire卡进行身份验证(遵循以下示例:https://stackoverflow.com/a/14160507/2095694 ).它已经在Android 4到5的几个设备上工作,但是我的Nexus 7停止工作,更新到6 Marshmallow(和6.0.1).在更新之前,它一直在使用同一设备.
似乎Cipher的工作方式不同,为相同的密钥和数据提供了不同的结果.运行以下代码......
public static void testCipher() throws Exception
{
byte[] KEY =
new byte[]{
(byte) 0x0C, (byte) 0x09, (byte) 0x03, (byte) 0x0E,
(byte) 0x05, (byte) 0x0A, (byte) 0x0D, (byte) 0x02,
(byte) 0x03, (byte) 0x0A, (byte) 0x09, (byte) 0x0B,
(byte) 0x06, (byte) 0x10, (byte) 0x04, (byte) 0x10
};
byte[] DATA =
new byte[]{
(byte) 0x29, (byte) 0xDA, (byte) 0xC0, (byte) 0xC4,
(byte) 0xB8, (byte) 0x47, (byte) 0x13, (byte) 0xA2};
byte[] newByte8 = new byte[8]; //Zeroes …
Run Code Online (Sandbox Code Playgroud) 我已经使用 JDK8 在 xws-security (EncryptionProcessor.java) 中成功实现了对 GCM 加密的支持,并针对其他系统进行了测试。但是我在解密时遇到问题。第一个问题如下java.security.InvalidAlgorithmParameterException:不支持的参数:javax.crypto.spec.IvParameterSpec。我通过将初始化向量 (iv) 从 IvParameterSpec() 更改为 GCMParameterSpec() 解决了问题,如下所示(来自 DecryptionProcessor.java 的代码片段)
try {
String dataAlgorithm = JCEMapper.translateURItoJCEID(tmp);
decryptor = Cipher.getInstance(dataAlgorithm);
//decryptor = Cipher.getInstance("DESede/CBC/ISO10126Padding");
int ivLen = decryptor.getBlockSize();
byte[] ivBytes = new byte[ivLen];
System.arraycopy(cipherInput, 0, ivBytes, 0, ivLen);
if (dataAlgorithm.matches(".*[gG][cC][mM].*$")) { // TK 03/09/2015 - probably needs more places for decrypting body stuff
GCMParameterSpec iv = new GCMParameterSpec(ivLen * Byte.SIZE, ivBytes);
decryptor.init(Cipher.DECRYPT_MODE, symmetricKey, iv);
}
else {
IvParameterSpec iv = new IvParameterSpec(ivBytes);
decryptor.init(Cipher.DECRYPT_MODE, symmetricKey, iv); <===== …
Run Code Online (Sandbox Code Playgroud) 试图运行 Junit 测试
Run Code Online (Sandbox Code Playgroud)> Caused by: org.jasypt.exceptions.EncryptionInitializationException: > java.security.NoSuchAlgorithmException: PBEWithMD5AndDES > SecretKeyFactory not available > at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:716) > at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553) > at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:705) > at com.optum.pdm.nameaddressstandardizer.PropertyFileLoader.getDecryptedValue(PropertyFileLoader.java:104) > ... 29 more > Caused by: java.security.NoSuchAlgorithmException: PBEWithMD5AndDES SecretKeyFactory not available > at javax.crypto.SecretKeyFactory.<init>(SecretKeyFactory.java:121) > at javax.crypto.SecretKeyFactory.getInstance(SecretKeyFactory.java:159) > at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:703) > ... 32 more
- TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
Caused by: java.security.NoSuchAlgorithmException: class configured for TrustManagerFactory: sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory not a TrustManagerFactory
at sun.security.jca.GetInstance.checkSuperClass(GetInstance.java:258)
at sun.security.jca.GetInstance.getInstance(GetInstance.java:237)
at sun.security.jca.GetInstance.getInstance(GetInstance.java:164)
at javax.net.ssl.TrustManagerFactory.getInstance(TrustManagerFactory.java:138)
at com.optum.pdm.util.SSLConnectionHelper.getSslSocketFactory(SSLConnectionHelper.java:41)
at com.optum.pdm.util.SSLConnectionHelper.getSSLContext(SSLConnectionHelper.java:31)
... 33 …
Run Code Online (Sandbox Code Playgroud) javax.crypto ×10
java ×9
encryption ×4
cryptography ×3
android ×2
aes-gcm ×1
exception ×1
junit ×1
openssl ×1
pbkdf2 ×1
powermock ×1
secret-key ×1
ssl ×1
swift ×1