2
我有編碼和解碼字符串的代碼。Java無正向或反向斜槓的編碼和解碼字符串
當我輸入「9」加密方法返回「9iCOC73F/683bf5WRJDnKQ ==」
的問題是,當我有時編碼字符串它將返回編碼的字符串與(/或\)和欲從String中刪除(/或\)。
那麼我怎樣才能實現我的加密和解密兩種方法。
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class EncryptDecryptAESAlgo {
private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[] { 'A', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p' };
public String encrypt(String Data) throws Exception {
String encryptedValue = "";
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
} catch (Exception e) {
}
return encryptedValue;
}
public String decrypt(String encryptedData) throws Exception {
String decryptedValue = "";
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
decryptedValue = new String(decValue);
return decryptedValue;
} catch (Exception e) {
}
return decryptedValue;
}
private Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
我正在使用Java。
請添加輸入和輸出與您的期望 – Sanjeev
@Sanjeev當我輸入「9」的加密方法的返回「9iCOC73F/683bf5WRJDnKQ ==」 – user3441151
爲什麼是一個問題? 「/」是base64字符集的一部分。你不能真正避免那些字符串包含那個字符。你可以替換它,但你會遇到其他問題... – Fildor