避免加密和编码的URL字符串中的换行符 [英] Avoiding line breaks in encrypted and encoded URL string

查看:546
本文介绍了避免加密和编码的URL字符串中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个简单的字符串编码器来模糊URL字符串的某些部分(以防止用户被弄脏)。我使用的代码与 JCA指南,除了:




  • 使用DES(假设它比AES快一点,需要一个较小的键)和

  • Base64 en /解码字符串,以确保其对于URL保持安全。



由于我无法理解的原因,输出字符串结束于换行符,我认为这样做将无法正常工作。我不知道是什么原因造成的。对类似的东西的建议更容易,或指向其他一些资源阅读?我正在找到所有的加密引用一点点(和overkill),但一个简单的ROT13实现将无法工作,因为我想处理一个更大的字符集(并且不想浪费时间实施一些可能有没有想到的模糊角色的问题)。



示例输入(不换行):

  http://maps.google.com/maps?q=kansas&hl=en&sll=42.358431,-71.059773&sspn=0.415552,0.718918&hnear=Kansas&t = m& z = 7 

示例输出(换行符如下所示):



'pre> GstikIiULcJSGEU2NWNTpyucSWUFENptYk4m5lD8RJl8l1CuspiuXiE9a07fUEAGM / tC7h0Vzus +
jAH6cT4Wtz2RUlBdGf8WtQxVDKZVOzKwi84eQh2kZT9T3KomlnPOu2owJ / 2RAEvG + QuGem5UGw ==

我的编码片段:

  final Key key = new SecretKeySpec(seed.getBytes (),DES); 
final Cipher c = Cipher.getInstance(DES);
c.init(Cipher.ENCRYPT_MODE,key);
final byte [] encVal = c.doFinal(s.getBytes());
返回新的BASE64Encoder()。encode(encVal);


解决方案

Base64编码器通常强加一些最大行( chunk )长度,并在必要时添加换行符。您通常可以配置,但这取决于特定的编码器实现。
例如, Apache Commons 的课程有一个 linelength 属性,将其设置为零(或否定)将禁用行分隔。



BTW:我同意其他答案,因为今天DES几乎不可取。此外,你只是混淆还是加密?谁有钥匙?整个事情对我来说并不好听。


I am trying to implement a simple string encoder to obfuscates some parts of a URL string (to prevent them from getting mucked with by a user). I'm using code nearly identical to the sample in the JCA guide, except:

  • using DES (assuming it's a little faster than AES, and requires a smaller key) and
  • Base64 en/decoding the string to make sure it stays safe for a URL.

For reasons I can't understand, the output string ends up with linebreaks, which I presume won't work. I can't figure out what's causing this. Suggestions on something similar that's easier or pointers to some other resources to read? I'm finding all the cryptography references a bit over my head (and overkill), but a simple ROT13 implementation won't work since I want to deal with a larger character set (and don't want to waste time implementing something likely to have issues with obscure characters i didn't think of).

Sample input (no line break):

http://maps.google.com/maps?q=kansas&hl=en&sll=42.358431,-71.059773&sspn=0.415552,0.718918&hnear=Kansas&t=m&z=7

Sample Output (line breaks as shown below):

GstikIiULcJSGEU2NWNTpyucSWUFENptYk4m5lD8RJl8l1CuspiuXiE9a07fUEAGM/tC7h0Vzus+
jAH6cT4Wtz2RUlBdGf8WtQxVDKZVOzKwi84eQh2kZT9T3KomlnPOu2owJ/2RAEvG+QuGem5UGw==

my encode snippet:

final Key key = new SecretKeySpec(seed.getBytes(), "DES");
final Cipher c = Cipher.getInstance("DES");
c.init(Cipher.ENCRYPT_MODE, key);
final byte[] encVal = c.doFinal(s.getBytes());
return new BASE64Encoder().encode(encVal);

解决方案

Base64 encoders usually impose some maximum line (chunk) length, and adds newlines when necessary. You can normally configure that, but that depends on the particular coder implementation. For example, the class from Apache Commons has a linelength attribute, setting it to zero (or negative) disables the line separation.

BTW: I agree with the other answer in that DES is hardly advisable today. Further, are you just "obfuscating" or really encrypting? Who has the key? The whole thing does not smell very well to me.

这篇关于避免加密和编码的URL字符串中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆