[英]Base64 apache.commons .encodeBase64 symbol not found
我正在編寫一個加密類,在發送 TCP 數據之前/之后使用密鑰加密/解密數據。 我在讓org.apache.commons.codec.binary.Base64
在我的系統上工作時遇到問題。 在大多數情況下,我可以看到人們將其與 android studio 相關聯,但是,我使用的是 notepad++ 和命令行,但仍然遇到問題。
我已將commons-codec-1.10.jar
添加到我的項目目錄中。 我在命令行運行:
javac -cp .;commons-codec-1.10.jar Server.java ... CryptoUtil.java
我有這個在頂部
import org.apache.commons.codec.binary.Base64;
我的錯誤是:
CryptoUtil.java:60: error: cannot find symbol
String encStr = new Base64.encodeBase64String(out);
^
symbol: class encodeBase64String
location: class Base64
CryptoUtil.java:87: error: cannot find symbol
byte[] enc = new Base64.decodeBase64(encryptedText);
^
symbol: class decodeBase64
location: class Base64
2 errors
我的封閉功能:
public String encrypt(String secretKey, String plainText)
throws NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
UnsupportedEncodingException,
IllegalBlockSizeException,
BadPaddingException{
//Key generation for enc and desc
KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
//Enc process
ecipher = Cipher.getInstance(key.getAlgorithm());
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
String charSet="UTF-8";
byte[] in = plainText.getBytes(charSet);
byte[] out = ecipher.doFinal(in);
String encStr = new Base64.encodeBase64String(out);
return encStr;
}
public String decrypt(String secretKey, String encryptedText)
throws NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
UnsupportedEncodingException,
IllegalBlockSizeException,
BadPaddingException,
IOException{
//Key generation for enc and desc
KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
//Decryption process; same key will be used for decr
dcipher=Cipher.getInstance(key.getAlgorithm());
dcipher.init(Cipher.DECRYPT_MODE, key,paramSpec);
byte[] enc = new Base64.decodeBase64(encryptedText);
byte[] utf8 = dcipher.doFinal(enc);
String charSet="UTF-8";
String plainStr = new String(utf8, charSet);
return plainStr;
}
new
關鍵字期望創建一個類型。 正如小插號指出的那樣, Base64
后面應該有括號()
。
然而, Base64
是static
方法的集合,所以如果你在這種情況下刪除new
,你就完成了。
String encStr = Base64.encodeBase64String(out);
應該做的伎倆。
對於安卓開發者:
編碼:使用Base64.encode(yourByteArray,Base64.DEFAULT)
解碼:使用Base64.decode(stringData,Base64.DEFAULT)
確保你導入, android.util.Base64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.