返回字节数组时出错 [英] error while returning byte array

查看:108
本文介绍了返回字节数组时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码

 public byte[] encrypt(byte[] unencryptedString,String k)throws Exception {
    String encryptedString = null;

    String k1 = String.format("%024d", Integer.parseInt(k));
    myEncryptionKey = k1;
    myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
    arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
    ks = new DESedeKeySpec(arrayBytes);
    skf = SecretKeyFactory.getInstance(myEncryptionScheme);
    cipher = Cipher.getInstance(myEncryptionScheme);
    key = skf.generateSecret(ks);

    try {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plainText =
                unencryptedString/*.getBytes(UNICODE_FORMAT)*/;
        byte[] encryptedText = cipher.doFinal(plainText);
       // encryptedString = new String(Base64.encodeBase64(encryptedText));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedText;
}

返回语句提供以下错误:

The return statement gives the following error:


encryptedText无法解析为变量

encryptedText cannot be resolved to a variable


推荐答案

与其他答案相反,如果您无法加密文本,我不会更改代码以返回 null - 我会让这个失败起泡就是一个例外。我不会声明你的方法可以抛出异常 - 我会指定它可以抛出的异常。您可以在一个非常细粒度的级别上执行此操作,或者在这种情况下使用 GeneralSecurityException ,其中涵盖您感兴趣的所有特定于加密的异常。

Contrary to the other answers, I wouldn't change your code to return null if you fail to encrypt the text - I would let that failure bubble up as an exception. I wouldn't declare that your method can throw Exception either - I'd specify which exceptions it can throw. You could do this at a very fine-grained level, or in this case use GeneralSecurityException which covers all the crypto-specific exceptions you're interested in.

我会进一步停止使用不必要的字段 - 您不需要更改任何状态。

I'd further stop using fields unnecessarily - you don't need to change any state here.

所有这些重构后,您的方法将成为:

After all this refactoring, your method would become:

public static byte[] encrypt(String unencryptedString, String k)
    throws GeneralSecurityException {
    String keySpecText = String.format("%024d", Integer.parseInt(k));
    byte[] keySpecBytes = keySpecText.getBytes(StandardCharsets.UTF_16);
    KeySpec ks = new DESedeKeySpec(keySpecBytes);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME);
    Key key = skf.generateSecret(ks);
    Cipher cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] plainText = unencryptedString.getBytes(StandardCharsets.UTF_16);
    return cipher.doFinal(plainText);
}

我不能肯定这是提供一个键 - 你在这里限制了2个 32 键,这不是很好,即使你很高兴,为什么要用一个字符串来代替一个 INT ?但是至少代码编译,当它失败时它会正常失败。

I'm not at all sure that this is a good way of providing a key - you're limited to 232 keys here, which isn't great, and even if you're happy with that, why take a string for the key instead of an int? But at least the code compiles, and when it fails it will properly fail.

这篇关于返回字节数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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