如何加密Java中的String [英] How to encrypt String in Java

查看:78
本文介绍了如何加密Java中的String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是加密将显示在2D条形码(PDF-417)中的字符串,所以当有人得到扫描的想法将无法读取。

What I need is to encrypt string which will show up in 2D barcode(PDF-417) so when someone get an idea to scan it will get nothing readable.

其他要求:


  • 不应该复杂

  • 它不应该由RSA,PKI基础设施,密钥对等组成。

它必须足够简单摆脱人们的窥探,并为其他有意获取数据的公司轻松解密。他们打电话给我们,我们告诉他们标准或给他们一些简单的密钥,然后可以用于解密。

It must be simple enough to get rid of the people snooping around, and easy to decrypt for other companies interested in getting that data. They call us, we tell them the standard or give them some simple key which can then be used for decryption.

可能这些公司可能会使用不同的技术,所以坚持一些与某些特殊平台或技术无关的标准是件好事。

Probably those companies could use different technologies so it would be good to stick to some standard which is not tied to some special platform or technology.

你建议什么?有没有一些Java类在加密()decrypt()中实现高安全性标准的并发症呢?

What do you suggest? Is there some Java class doing encrypt() decrypt() without much complication in achieving high security standards?

推荐答案


警告

不要将其用作某种安全性测量。

Do not use this as some kind of security measurement.

此帖中的加密机制是一次性填充,这意味着秘密密钥攻击者可以使用2个加密消息轻松恢复。 XOR 2加密消息,你得到的钥匙。那么简单!

The encryption mechanism in this post is a One-time pad, which means that the secret key can be easily recovered by an attacker using 2 encrypted messages. XOR 2 encrypted messages and you get the key. That simple!

由Moussa指出

Pointed out by Moussa






我正在使用Sun的Base64Encoder / Decoder,它将在Sun的JRE中找到,以避免lib中的另一个JAR。从使用OpenJDK或其他JRE的角度来说这是很危险的。此外,还有另一个原因我应该考虑使用Apache commons lib与Encoder / Decoder?


I am using Sun's Base64Encoder/Decoder which is to be found in Sun's JRE, to avoid yet another JAR in lib. That's dangerous from point of using OpenJDK or some other's JRE. Besides that, is there another reason I should consider using Apache commons lib with Encoder/Decoder?

public class EncryptUtils {
    public static final String DEFAULT_ENCODING = "UTF-8"; 
    static BASE64Encoder enc = new BASE64Encoder();
    static BASE64Decoder dec = new BASE64Decoder();

    public static String base64encode(String text) {
        try {
            return enc.encode(text.getBytes(DEFAULT_ENCODING));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }//base64encode

    public static String base64decode(String text) {
        try {
            return new String(dec.decodeBuffer(text), DEFAULT_ENCODING);
        } catch (IOException e) {
            return null;
        }
    }//base64decode

    public static void main(String[] args) {
        String txt = "some text to be encrypted";
        String key = "key phrase used for XOR-ing";
        System.out.println(txt + " XOR-ed to: " + (txt = xorMessage(txt, key)));

        String encoded = base64encode(txt);       
        System.out.println(" is encoded to: " + encoded + " and that is decoding to: " + (txt = base64decode(encoded)));
        System.out.print("XOR-ing back to original: " + xorMessage(txt, key));
    }

    public static String xorMessage(String message, String key) {
        try {
            if (message == null || key == null) return null;

            char[] keys = key.toCharArray();
            char[] mesg = message.toCharArray();

            int ml = mesg.length;
            int kl = keys.length;
            char[] newmsg = new char[ml];

            for (int i = 0; i < ml; i++) {
                newmsg[i] = (char)(mesg[i] ^ keys[i % kl]);
            }//for i

            return new String(newmsg);
        } catch (Exception e) {
            return null;
        }
    }//xorMessage
}//class

这篇关于如何加密Java中的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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