2017-06-09 31 views
-3

我想使用AES/ECB使用第三方Java代碼來加密數據。提供數據和密鑰。代碼如下: -使用Java加密

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的知識。請幫助

+2

'字符串encryptedValue =新的String(..'你錯過了申報環節 –

+0

的錯誤是很清楚的 - 。encryptedValue沒有定義 – OldProgrammer

回答

3

兩個問題:

  1. 您必須聲明encryptedValue,在變量名前添加一個類型:

    String encryptedValue = new String(Base64... 
    
  2. 您正在使用java.util.Base64不正確。沒有方法Base64.encodeBase64。研究該類的Javadoc及其嵌套類Base64.DecoderBase64.Encoder以瞭解如何使用它們。

1

您從未聲明過encryptedValue。無論何時出現'符號'錯誤,通常都會談論一個庫中不存在的變量或方法,這可能是因爲沒有聲明它。

您需要添加Pavneet所說的內容,或者至少剛剛宣佈了encryptedValue。

String encryptedValue; 

'main'方法的開始。

+0

也就是說不止一個答案評論,你可以解釋一下什麼是OP需求要做到這一點 – Robert

+0

固定?對不起,我是新:) –