JAVA解密问题 [英] Decrypt in JAVA problem

查看:59
本文介绍了JAVA解密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我解决Java中的解密问题吗,我必须加密数据并保存在db(SQL SERVER 2005 nvarchar类型)中,如果我加密并尝试解密(在存储到db之前),一切都很好,但是我将它们存储在数据库中后,我无法解密它们.我收到以下消息
javax.crypto.BadPaddingException:给定的最终块未正确填充

我使用以下代码(在互联网上获取),我尝试使用其他算法解密(使用base64),但未成功.
谢谢很多

Could anybody help me to solve a Decryption problem in java, i have to encrypt data and to save in db (SQL SERVER 2005 nvarchar type), if i encrypt and try to decrypt (Before storing to db) everything is OK but after i have stored them in db i can not decrypt them. i got the following message
javax.crypto.BadPaddingException: Given final block not properly padded

i use the following code (got on internet), i tried to use other algorithms to decrypt(with base64 ) but no success.
THANX A LOT

import java.io.IOException;
    import javax.crypto.Cipher;
    import javax.crypto.BadPaddingException;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import java.security.Key;
    import java.security.InvalidKeyException;


public class EncriptAndDecript {

    private static String algorithm = "DESede";
    private static Key key = null;
    private static Cipher cipher = null;
    private static EncriptAndDecript obj = new EncriptAndDecript();

    public EncriptAndDecript() {
    try {
    key = KeyGenerator.getInstance(algorithm).generateKey();
    cipher = Cipher.getInstance(algorithm);
    } catch (Exception e) {
    }
    }

    public static EncriptAndDecript getInstance() {
    return obj;
    }

    public static byte[] encrypt(String input)
    throws InvalidKeyException,
    BadPaddingException,
    IllegalBlockSizeException {
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = input.getBytes();
    return cipher.doFinal(inputBytes);
    }

    public  String getEncryptStringValue(String input)
    {
        String vali = "";
        try{
                 vali= new String(encrypt(input));
         }
         catch(Exception e){vali= "";}
        return vali;
    }
   public String getDecryptStringValue(String input) throws IOException
    {
                 String vali ="";
            byte[] a = input.getBytes();
        //byte[] a = new BASE64Decoder().decodeBuffer(input);
        try{
                 vali= new String(decrypt(a));
         }
         catch(Exception e){vali= e.toString();}
        return vali;
    }


    public static String decrypt(byte[] encryptionBytes)
    throws InvalidKeyException,
    BadPaddingException,
    IllegalBlockSizeException {
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] recoveredBytes =
        cipher.doFinal(encryptionBytes);
        String recovered = new String(recoveredBytes);
        return recovered;
    }
      
  Cipher ecipher;
    Cipher dcipher;


    }

推荐答案

我会虚心地建议您不要获得用于解密的全部内容.为什么您的加密流是字符串?我认为,当您从crypto-byte []取得正确的输出-并将其转换为字符串时,就失去了一些东西.

再加上类和成员变量/成员的混合.我认为您不应该真正使用任何类变量或方法.即static.
I would humbly suggest you are not getting the full stream for decription. Why is your encrypted stream a string? I think when you take the output from encrypt - byte[] which is correct - and convert it to a string you''re loosing something.

Plus there is a nasty mixture of class and member variables / members. I don''t think you should really be using any class variables or methods; i.e. static.


这可能是* ss的痛苦,您永远都不知道为什么它不起作用.
试试这个-在我的几个解决方案中都可以使用(包括SQL Server上的存储):

This can be a pain in the *ss, you never know why it''s not working.
Try this one - it works in a couple of my solutions (incuding storage on SQL Servers):

package de.security;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
public final class DeEncrypter {
	private static DeEncrypter instance = new DeEncrypter(); 
	
	private Cipher cipher;
	private Key key;
//	private final BASE64Encoder b64Encoder = new BASE64Encoder();
//	private final Base64 b64Decoder = Base64.;
	
	public static DeEncrypter getInstance() {
		
		return DeEncrypter.instance;
	}	
	
	private DeEncrypter() {
		
		try {
			this.cipher = Cipher.getInstance("AES");
			byte[] raw = { (byte) 0xA5, (byte) 0x01, (byte) 0x7B, (byte) 0xE5,
					(byte) 0x23, (byte) 0xCA, (byte) 0xD4, (byte) 0xD2,
					(byte) 0xC6, (byte) 0x5F, (byte) 0x7D, (byte) 0x8B,
					(byte) 0x0B, (byte) 0x9A, (byte) 0x3C, (byte) 0xF1 };
			this.key = new SecretKeySpec(raw, "AES");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
	}
	
	public String encrypt(String aData) {
		String result = "";
		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] utf8 = aData.getBytes("UTF8");
			byte[] encryptedData = cipher.doFinal(utf8);
			result = Base64.encode(encryptedData);//this.b64Encoder.encode(encryptedData);
		} 
		catch (InvalidKeyException oException) { 			oException.printStackTrace(); } 
		catch (IllegalBlockSizeException oException) { 		oException.printStackTrace(); } 
		catch (BadPaddingException oException) { 			oException.printStackTrace(); } 
		catch (IOException oException) { 					oException.printStackTrace(); }
		return result;
	}
	public String decrypt(String aData) {
		String result = "";
			try {
				cipher.init(Cipher.DECRYPT_MODE, key);
				byte[] decodedData = Base64.decode(aData);//this.b64Decoder.decodeBuffer(aData);
				byte[] utf8 = cipher.doFinal(decodedData);
				result = new String(utf8, "UTF8");
			} 
			catch (InvalidKeyException oException) { 			oException.printStackTrace(); } 
			catch (Base64DecodingException oException) { 		oException.printStackTrace(); } 
			catch (IllegalBlockSizeException oException) { 		oException.printStackTrace(); } 
			catch (BadPaddingException oException) { 			oException.printStackTrace(); } 
			catch (UnsupportedEncodingException oException) { 	oException.printStackTrace(); }
		return result;
	}
}



电话是



the call is

DeEncrypter.getInstance().encrypt(someStringValue);



问候
Torsten



regards
Torsten


这篇关于JAVA解密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆