Java简单加密

发布于 2024-08-04 04:48:27 字数 439 浏览 10 评论 0原文

我想加密存储在磁盘上的文本(配置)文件。 尝试使用 DES 加密我在客户端计算机上遇到了致命错误,后来我发现了该算法无法处理重音字符 (!) 我怀疑这是因为我使用的是旧软件包(sun.misc.BASE64Decoder) - 但我不确定这就是原因。

但是,我正在寻找一个更简单的解决方案 - 我需要一个非常简单的加密(我知道有些人不会同意这一点) - 不是 RSA 大约 128 位密钥,只是遮挡了好奇的眼睛中的文本。

很奇怪我在网上找不到一个简单的解决方案。

如何实现一个简单的加密方案?

I would like to encrypt a textual (configuration) file stored on disk.
Trying to use DES encryption I've had fatal error on client machines, I later found out the algorithm could not handle accented characters (!)
I suspect that was because I was using old packages (sun.misc.BASE64Decoder) - but I'm not sure that is the reason.

However, I'm looking for a simpler solution - I need a really simple encryption (I know some people would not agree on that) - not RSA of 128 bit keys or so, just obscuring the text from curious eyes.

It is really weird that I could not find a simple trivial solution on the web.

How can I implement a simple encryption scheme?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

本宫微胖 2024-08-11 04:48:27

查看Java 简化加密 (Jasypt)

Jasypt 是一个 Java 库,它允许
开发者添加基本加密
他/她的项目能力
最小的努力,并且不需要
对如何进行深入的了解
密码学有效。

  • 高安全性、基于标准的加密技术,既适用于
    单向和双向
    加密。加密密码、文本、
    数字、二进制...
  • 与 Hibernate 的透明集成。
  • 适合集成到基于 Spring 的应用程序中,也适合
    可与 ACEGI 透明集成
    (Spring Security)。
  • 用于加密配置的集成功能
    应用程序(即数据源)。
  • 开放 API,可与任何 JCE 提供商一起使用。
  • ...以及更多

Check out the Java Simplified Encryption (Jasypt).

Jasypt is a java library which allows
the developer to add basic encryption
capabilities to his/her projects with
minimum effort, and without the need
of having deep knowledge on how
cryptography works.

  • High-security, standards-based encryption techniques, both for
    unidirectional and bidirectional
    encryption. Encrypt passwords, texts,
    numbers, binaries...
  • Transparent integration with Hibernate.
  • Suitable for integration into Spring-based applications and also
    transparently integrable with ACEGI
    (Spring Security).
  • Integrated capabilities for encrypting the configuration of
    applications (i.e. datasources).
  • Open API for use with any JCE provider.
  • ...and much more
宁愿没拥抱 2024-08-11 04:48:27

我正在使用这个简单的 One-Time-Pad 算法:

import org.apache.commons.codec.binary.Base64;
public class Cipher {
  private static final String KEY = "some-secret-key-of-your-choice";
  public String encrypt(final String text) {
    return Base64.encodeBase64String(this.xor(text.getBytes()));
  }
  public String decrypt(final String hash) {
    try {
      return new String(this.xor(Base64.decodeBase64(hash.getBytes())), "UTF-8");
    } catch (java.io.UnsupportedEncodingException ex) {
      throw new IllegalStateException(ex);
    }
  }
  private byte[] xor(final byte[] input) {
    final byte[] output = new byte[input.length];
    final byte[] secret = this.KEY.getBytes();
    int spos = 0;
    for (int pos = 0; pos < input.length; ++pos) {
      output[pos] = (byte) (input[pos] ^ secret[spos]);
      spos += 1;
      if (spos >= secret.length) {
        spos = 0;
      }
    }
    return output;
  }

不要忘记添加commons-codec 到类路径。

I'm using this simple One-Time-Pad algorithm:

import org.apache.commons.codec.binary.Base64;
public class Cipher {
  private static final String KEY = "some-secret-key-of-your-choice";
  public String encrypt(final String text) {
    return Base64.encodeBase64String(this.xor(text.getBytes()));
  }
  public String decrypt(final String hash) {
    try {
      return new String(this.xor(Base64.decodeBase64(hash.getBytes())), "UTF-8");
    } catch (java.io.UnsupportedEncodingException ex) {
      throw new IllegalStateException(ex);
    }
  }
  private byte[] xor(final byte[] input) {
    final byte[] output = new byte[input.length];
    final byte[] secret = this.KEY.getBytes();
    int spos = 0;
    for (int pos = 0; pos < input.length; ++pos) {
      output[pos] = (byte) (input[pos] ^ secret[spos]);
      spos += 1;
      if (spos >= secret.length) {
        spos = 0;
      }
    }
    return output;
  }

Don't forget to add commons-codec to classpath.

你是年少的欢喜 2024-08-11 04:48:27

加密算法适用于原始字节,而不是字符。

您无法处理重音字符的原因是您用来将字符与原始字节相互转换的代码无法处理 Unicode。

您应该使用 AES;有关如何在 Java 中使用它的示例,请参阅此处

编辑:现在,您可能只是将其隐藏起来以防止好奇的目光,但无法预测未来会发生什么,而且现在使用强加密总是得多更好却迟迟没有发现你应该这样做却没有。

Encryption algorithms work on raw bytes, not characters.

The reason you couldn't handle accented characters was because the code you were using to convert the characters to and from raw bytes didn't handle Unicode.

You should use AES; for an example of how to use it in Java, see here.

EDIT: Right now, you might just be hiding it from curious eyes, but there's no telling what the future will hold, and it is always much better to use strong encryption now and not find out, to late, that you should have but didn't.

給妳壹絲溫柔 2024-08-11 04:48:27

ROT13 怎么样?它可能是有史以来最简单和最糟糕的加密(也称为凯撒密码)

下面是 Jay Kominek 在 Java 中的基本实现:

import java.io.*;

public class rot13 {
  public static void main (String args[]) {
    int abyte = 0;
    try { while((abyte = System.in.read())>=0) {
      int cap = abyte & 32;
      abyte &= ~cap;
      abyte = ((abyte >= 'A') && (abyte <= 'Z') ? ((abyte - 'A' + 13) % 26 + 'A') : abyte) | cap;
      System.out.print(String.valueOf((char)abyte));
    } } catch (IOException e) { }
    System.out.flush();
  }
}

How about ROT13? It's probably the most simple and worst encryption ever (it was also called the Caeser's Cipher)

Here's a basic implementation in Java by Jay Kominek:

import java.io.*;

public class rot13 {
  public static void main (String args[]) {
    int abyte = 0;
    try { while((abyte = System.in.read())>=0) {
      int cap = abyte & 32;
      abyte &= ~cap;
      abyte = ((abyte >= 'A') && (abyte <= 'Z') ? ((abyte - 'A' + 13) % 26 + 'A') : abyte) | cap;
      System.out.print(String.valueOf((char)abyte));
    } } catch (IOException e) { }
    System.out.flush();
  }
}
小红帽 2024-08-11 04:48:27

如果您不想真正加密文本,为什么不使用 Base64 进行编码呢?它看起来像是无稽之谈,但很容易破译。另外,您已经在使用 Base64 代码...

If you're not looking to really encrypt the text, why not encode with Base64? It'll look like nonsense, and it's very easy to decode. Plus, you're already using Base64 code...

浮萍、无处依 2024-08-11 04:48:27

如果您要加密一段文本,那么一次性密码本怎么样?一次性本本非常容易创建;您所需要的只是一个随机字节序列,其长度与您要加密的数据相同

If you have a single piece of text to encrypt, what about a one-time-pad? A one-time-pad is very easy to create; all you need is a random sequence of bytes the same length as the data you are encrypting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文