如何返回1个以上的字符串值?

时间:2013-11-26 08:37:30

标签: java string encryption

我是新手,需要一些帮助。我这里有一个用于解密数据的代码。它的工作意义是它可以解密数据,但问题是;它只能显示解密数据的1个值(第29行),而不是3个或更多取决于我想要它(如何让它也解密第30行和第31行的数据?)。下面我发布了我的问题的相关代码,希望这里的任何人可以帮助我解决这个问题。

public class CipherUtils
{
static Log log = LogFactory.getLog(CipherUtils.class);
    private static byte[] key = "xxxxxx".getBytes();
    private static byte[] iv = "xxxxxx".getBytes();

public static String decrypt(String strToDecrypt)
{
    try
    {
        //Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        Cipher cipher = Cipher.getInstance("aes/cbc/nopadding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        IvParameterSpec ips = new IvParameterSpec(iv);
        //cipher.init(Cipher.DECRYPT_MODE, secretKey);
        cipher.init(Cipher.DECRYPT_MODE, secretKey,ips);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString.trim();
    }
    catch (Exception e)
    {
        log.error("Error while decrypting : " + strToDecrypt , e);
    }
    return null;
}

public static void main(String args[]) throws Exception
{
    String a = CipherUtils.decrypt("yXTVA6oG4kWOlvfKN/qXwa3VgEyiBu4kkgKh9WHt0s8="
                                  ,"yX7JI7IaExK3eBC6BU5RdCvkCrAAcyV3YTmHqYH5nG0="                                               
                                  ,"yj56tfZEh3405yEwladp+ml/nk/h8Cx56XnP5Ycdeio=");
    System.out.println(">>>"+a.trim());
}
}

提前致谢。

3 个答案:

答案 0 :(得分:1)

您的方法解密只接受一个参数(名为strToDecrypt),但您传递的是三个。

你可以在这里做几件事,最简单的是简单地称解密3次,每次都有不同的字符串。 您也可以重写列表来迭代列表。

http://www.tutorialspoint.com/java/java_using_iterator.htm

答案 1 :(得分:1)

public class CipherUtils
{
  static Log log = LogFactory.getLog(CipherUtils.class);
  private static byte[] key = "xxxxxx".getBytes();
  private static byte[] iv = "xxxxxx".getBytes();

  public static String[] decrypt(String[] strToDecrypt){
     try{
       String[] strDecrypted;
       //Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
       Cipher cipher = Cipher.getInstance("aes/cbc/nopadding");
       final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
       IvParameterSpec ips = new IvParameterSpec(iv);
       //cipher.init(Cipher.DECRYPT_MODE, secretKey);
       cipher.init(Cipher.DECRYPT_MODE, secretKey,ips);
       for(int i =0;i<strToDecrypt.length ; i++){
          final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt [i])));
          strDecrypted[i] = decryptedString ;
       }
       return strDecrypted;    
     }catch (Exception e){
         log.error("Error while decrypting : " + strToDecrypt , e);
      }
     return null;
 }

public static void main(String args[]) throws Exception
{
     String array[] =      {"yXTVA6oG4kWOlvfKN/qXwa3VgEyiBu4kkgKh9WHt0s8=","yX7JI7IaExK3eBC6BU5RdCvkCrAAcyV3YTmHqYH5nG0=","yj56tfZEh3405yEwladp+ml/nk/h8Cx56XnP5Ycdeio="};

     String a = CipherUtils.decrypt(array);
     System.out.println(">>>"+a.trim());
  }
}

我所做的是我已经将所有字符串解析为String数组并将其传递给方法。然后在方法内部,我解密字符串并将其添加到另一个数组。然后完成后,我返回包含所有解密行的数组。然后,您可以从阵列中获取它。

答案 2 :(得分:1)

public static String[] decrypt(String[] strToDecrypt)
{
    String decryptedString[] = new String[strToDecrypt.length];
    for(int i = 0;i<strToDecrypt.length;i++) {
    try
    {
        //Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        Cipher cipher = Cipher.getInstance("aes/cbc/nopadding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        IvParameterSpec ips = new IvParameterSpec(iv);
        //cipher.init(Cipher.DECRYPT_MODE, secretKey);
        cipher.init(Cipher.DECRYPT_MODE, secretKey,ips);
        decryptedString[i] = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt[i])));

    }
    catch (Exception e)
    {
        log.error("Error while decrypting : " + strToDecrypt[i] , e);
    }
    }return decryptedString;
}