【问题标题】:Encryption using Java [duplicate]使用 Java 加密 [重复]
【发布时间】:2017-06-09 15:04:11
【问题描述】:

我正在尝试使用第三方 Java 代码使用 AES/ECB 加密数据。提供数据和密钥。代码如下:-

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class encryptData {
  public static void main(String[] args) {

    String data="amount=10&expiryDate=20150101 151515&orderRefNum=11001&postBackURL=http://localhost:9081/local/status.php&storeId=28";
    String key="89OUITUPRL3I8H3G";

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    encryptedValue = new String(Base64.encodeBase64(cipher.doFinal(data.getBytes())));
  }
}

我正在https://www.compilejava.net/上试用

这是我得到的错误:-

/tmp/java_Ramvov/encryptData.java:16: error: cannot find symbol

encryptedValue = new String(Base64.encodeBase64(cipher.doFinal(data.getBytes()))); 
^
symbol: variable encryptedValue

location: class encryptData

/tmp/java_Ramvov/encryptData.java:16: error: cannot find symbol

encryptedValue = new String(Base64.encodeBase64(cipher.doFinal(data.getBytes())));
                                   ^ 
symbol: method encodeBase64(byte[])

location: class Base64

2 个错误

我对 Java 知之甚少。请帮忙

【问题讨论】:

  • String encryptedValue = new String(..你错过了声明部分
  • 错误很明显 - encryptedValue 未定义。

标签: java encryption


【解决方案1】:

两个问题:

  1. 你必须声明encryptedValue,在变量名前添加一个类型:

    String encryptedValue = new String(Base64...
    
  2. 您错误地使用了java.util.Base64。没有方法Base64.encodeBase64。研究该类及其嵌套类Base64.DecoderBase64.Encoder 的Javadoc 以了解如何使用它们。

【讨论】:

    【解决方案2】:

    您从未声明 encryptedValue。每当您收到“符号”错误时,通常是在谈论库中不存在的变量或方法,这可能是因为没有声明它。

    您需要添加 Pavneet 所说的内容,或者至少之前刚刚声明了 encryptedValue。

    String encryptedValue;
    

    在'main'方法的开始处。

    【讨论】:

    • 这更像是一个评论而不是一个答案。您可以解释 OP 需要做什么才能使其正常工作。
    • 已修复?对不起,我是新人:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-22
    • 2012-05-06
    • 2011-07-15
    • 2012-10-06
    • 2010-09-12
    • 2015-10-04
    相关资源
    最近更新 更多