标签: javax.crypto

鉴于最终块未正确填充

我正在尝试实现基于密码的加密算法,但是我得到了这个例外:

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)

java encryption cryptography exception javax.crypto

102
推荐指数
2
解决办法
24万
查看次数

如何列出可用的密码算法?

我正在使用Cipher实现Cipher.getInstance(String algorithm).我的印象是,我可能传递的可用算法名称因我的类路径中存在的库而异.

我想编写一个简单的程序,我可以使用不同的类路径运行,列出可用的Cipher算法名称.获取此列表需要调用哪种方法?

java encryption cryptography javax.crypto

32
推荐指数
4
解决办法
4万
查看次数

PBKDF2WithHmacSHA512比.PBKDF2WithHmacSHA1

我正在研究一个Java身份验证子系统,该子系统规定在DB中存储密码作为PBKDF2生成的哈希,我现在正在尝试决定是否应该使用SHA1SHA512作为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)

java pbkdf2 secret-key javax.crypto

23
推荐指数
1
解决办法
3万
查看次数

为什么我得到包javax.crypto不存在

当我使用javax.crypto.Mac编译一个类时,我收到此错误消息?

包javax.crypto不存在

我可以通过在我的编译类路径中包含jre/lib/jce.jar来解决它.

为什么jce.jar不在默认的jdk类路径上?jre/lib/rt.jar在类路径上,包含其他javax包,但jce看起来很特别?

java javax.crypto

16
推荐指数
2
解决办法
2万
查看次数

Key和KeySpec之间有什么区别?

在Java加密库中,键有两种不同的表示形式 - KeyKeySpec.文档暗示两者之间存在差异 - 一个KeySpec是'透明'(无论这意味着什么),但没有方法,而Key有一个getEncoded方法.你打算使用a KeyFactory来转换它们(它确实有一个getKeySpec转换方法).

但是,SecretKeySpec实现KeyKeySpec!但是也有一个SecretKeyFactory类,它不会继承KeyFactory.

这一切让我彻底迷茫.什么是之间的不同KeyKeySpec,以及如何做SecretKeySpec,并SecretKeyFactory进入呢?

java cryptography javax.crypto

9
推荐指数
1
解决办法
2565
查看次数

Java 中的 iOS CryptoKit

我正在寻找 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)

java encryption javax.crypto swift apple-cryptokit

8
推荐指数
1
解决办法
1507
查看次数

javax.crypto在不同版本的Android OS中的工作方式不同?

我正在使用此代码段来加密/解密我的应用程序数据库中的数据:

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)

java encryption android javax.crypto

6
推荐指数
1
解决办法
3630
查看次数

自Android 6 Marshmallow以来,javax.crypto.Cipher的工作方式不同

我已成功使用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)

java android openssl javax.crypto android-6.0-marshmallow

6
推荐指数
2
解决办法
1942
查看次数

xws-security(webservices-rt)中的GCM加密和解密

我已经使用 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)

java javax.crypto aes-gcm

5
推荐指数
1
解决办法
3090
查看次数

ssl 和 StandardPBEStringEncryptor 的 PowerMock 问题

试图运行 Junit 测试

  1. 下面是使用org.jasypt.encryption.pbe.StandardPBEStringEncryptor的密码解密
>      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
Run Code Online (Sandbox Code Playgroud)
  1. 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)

ssl junit powermock javax.crypto

5
推荐指数
1
解决办法
1239
查看次数