在 tomcat 上,我有两个网络应用程序,即 app1 和 app2。我以加密形式(使用下面的程序)将 app1 的 URL 发送到 app2 。我通过 response.sendRedirect(encryptedURL)
发送 URL。然后在 app2 我得到这个加密的 URL。我看到在 app2 的这个加密 URL 中有一些额外的字符,比如 +,space( )。我想要 app2 上的完全相同的 URL。我不明白为什么要插入这些额外的字符?
这里是app1加密后的URL
Rc9mgB+18chPk6nk1+gzpws6dO5/TSkcOYy8rbuVVTmjE9YjLt5w0EMpuhh+QBKzReYO34pAquf9
kmYOiwl99jo2kDCCkYHMh/+nxEgs1yrLYagsc/0p5KdYeMy1eWeUQB8KqmNlhtYysrHhOYVuunUi
dTaEKUTSWd8lPg9/wZfHdBoJgaF40aUW3FeRODVL
这是我使用 req.getParameter("encryptedURL"); 获取的 app2 上加密 URL 的值
Rc9mgB 18chPk6nk1 gzpws6dO5/TSkcOYy8rbuVVTmjE9YjLt5w0EMpuhh QBKzReYO34pAquf9 kmYOiwl99jo2kDCCkYHMh/ nxEgs1yrLYagsc/0p5KdYeMy1eWeUQB8KqmNlhtYysrHhOYVuunUi dTaEKUTSWd8lPg9/wZfHdBoJgaF40aUW3FeRODVL
这里是加密程序
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AESEncryptionDecryptionTest {
private static final String ALGORITHM = "AES";
private static final String myEncryptionKey = "ThisIsFoundation";
private static final String UNICODE_FORMAT = "UTF8";
public static String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
return encryptedValue;
}
private static Key generateKey() throws Exception {
byte[] keyAsBytes;
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
Key key = new SecretKeySpec(keyAsBytes, ALGORITHM);
return key;
}
}
编辑:- 看起来非常接近它。这是我的新代码和发现
这是我在app1加密的参数
mySession=84088586B45317C3600978DF9F52A699&securityTok=44a2e4dd89ed1aad438d5e3c16ff528&myurl=https://10.205.112.83:8443
//按照您在 app1 中的建议进行加密,并将 encryptedParam 作为请求参数发送
//现在从请求中获取 app2 的 encryptedParam 并根据您的建议解密。正如你在下面看到的
除了我的 URL 部分(即 =https://10.205
替换为 ½‡è∼�Àg¸l½.µbO’)以外,app2 的所有内容都已解密
mySession=84088586B45317C3600978DF9F52A699&securityTok=44a2e4dd89ed1aad438d5e3c16ff528&myurl½‡è˜�Àg¸l½.µbO’112.83:8443
代码是
public static String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
// Key key = buildKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes(UNICODE_FORMAT));
String encryptedValue1 = new BASE64Encoder().encode(encValue);
String encryptedValue = URLEncoder.encode(encryptedValue1);
return encryptedValue;
}
public static String decrypt(String encryptedValue) throws Exception {
Key key = generateKey();
// Key key = buildKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
String decordedValueStr = URLDecoder.decode(encryptedValue);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(decordedValueStr);
byte[] decValue = c.doFinal(decordedValue);
// String try1=new BASE64Encoder().encode(decValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
最佳答案
在您的 encrypt
方法中代替:
return encryptedValue;
使用这段代码:
return URLEncoder.encode(encryptedValue);
然后在接收端调用 URLDecoder#decode:
String tmpUrl = URLDecoder.decode(encryptedURL);
// call BASE64Decoder on tmpUrl
// call decrypt on return value of BASE64Decoder
关于java - response.sendRedirect 在 app2 中接收时用加密 URL 中的空格替换 +?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10836902/