相关疑难解决方法(0)

Android 4.2上的加密错误

以下代码适用于除最新4.2之外的所有Android版本

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Util class to perform encryption/decryption over strings. <br/>
 */
public final class UtilsEncryption
{
    /** The logging TAG */
    private static final String TAG = UtilsEncryption.class.getName();

    /** */
    private static final String KEY = "some_encryption_key";

    /**
     * Avoid instantiation. <br/>
     */
    private UtilsEncryption()
    {
    }

    /** The HEX characters */
    private final static String HEX = "0123456789ABCDEF"; …
Run Code Online (Sandbox Code Playgroud)

encryption android unit-testing

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

最后一个块与CipherInputStream/CipherOutputStream不完整,即使使用填充AES/CBC/PKCS5Padding

实际上,为了这个,我也从互联网和stackoverflow搜索了很多,

最初我在加密和解密中没有使用填充,

但最后我从这里得到了解决方案

/sf/answers/754290421/

并且我使用填充更新了我的代码作为AES/CBC/PKCS5Padding并且同样的错误即将到来,并且最后一个块没有被解密...

我正在为此工作最后两天,但没有找到解决方案

我的Crypter代码:

package mani.droid.browsedropbox;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Crypter {

    Cipher encipher;
    Cipher decipher;
CipherInputStream cis;
CipherOutputStream cos;
FileInputStream fis;
byte[] ivbytes = new byte[]{(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e',                                                                                          (byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m',     (byte)'n', (byte)'o', (byte)'p'};
    IvParameterSpec iv = new IvParameterSpec(ivbytes);

public boolean …
Run Code Online (Sandbox Code Playgroud)

java encryption android aes padding

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

如何处理"解密时最后一个块不完整"

我有一个简单的类来尝试包装加密以便在我的程序中的其他地方使用.

import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public final class StupidSimpleEncrypter
{
    public static String encrypt(String key, String plaintext)
    {
        byte[] keyBytes = key.getBytes();
        byte[] plaintextBytes = plaintext.getBytes();
        byte[] ciphertextBytes = encrypt(keyBytes, plaintextBytes);
        return new String(ciphertextBytes);
    }

    public static byte[] encrypt(byte[] key, byte[] plaintext)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec spec = new SecretKeySpec(getRawKey(key), "AES");
            cipher.init(Cipher.ENCRYPT_MODE, spec);
            return cipher.doFinal(plaintext);
        }
        catch(Exception e)
        {
            // some sort of problem, return null because we can't encrypt it.
            Utility.writeError(e); …
Run Code Online (Sandbox Code Playgroud)

encryption android aes

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

解密错误:"当预期时没有设置iv"

我几乎不熟悉加密.

我正在尝试解密一个字节数组,当我提供IV时,我得到一个异常:InvalidAlgorithmParameterException(当预期时没有设置iv).

这是我的代码(iv是一个16字节的数组,它不是null,并且具有加密时使用的值):

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, encriptionKey,new IvParameterSpec(iv));
Run Code Online (Sandbox Code Playgroud)

如果我没有指定IV,则密码初始化为ok:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, encriptionKey);
Run Code Online (Sandbox Code Playgroud)

试图找到答案我找到了JCEStreamCipher(这里)的实现,这可能与我正在使用的版本不对应,但有一些代码使我对我的理解不正确.

这是代码:

   if ((ivLength != 0) && !(param instanceof ParametersWithIV))
    {
        SecureRandom    ivRandom = random;

        if (ivRandom == null)
        {
            ivRandom = new SecureRandom();
        }

        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE))
        {
            byte[]  iv = new byte[ivLength];

            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV)param;
        }
        else
        {
            throw new InvalidAlgorithmParameterException("no IV set when one expected"); …
Run Code Online (Sandbox Code Playgroud)

java android cryptography

10
推荐指数
1
解决办法
6711
查看次数

图像文件的加密和解密

结合我的另一个问题,并在更改了这部分代码之后

FileOutputStream output = new FileOutputStream("sheepTest.png");
    CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);
    ImageIO.write(input, "PNG", cos);
    cos.close();
Run Code Online (Sandbox Code Playgroud)

从解密部分,我遇到了另一个错误,就是这个

Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
at javax.imageio.ImageIO.getWriter(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at encypt.com.trial.main(trial.java:82)
Run Code Online (Sandbox Code Playgroud)

当我点击sheepTest.png时,文件为空.错误在哪里?任何人都可以帮我解决错误吗?谢谢.

public class trial {
public static void main(String[] arg) throws Exception {

   // Scanner to read the user's password. The Java cryptography
   // architecture points out that strong passwords in strings is a
   // bad idea, but we'll let it go …
Run Code Online (Sandbox Code Playgroud)

java encryption image aes

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

标签 统计

android ×4

encryption ×4

aes ×3

java ×3

cryptography ×1

image ×1

padding ×1

unit-testing ×1