如何使用另一个字符串作为密码来加密/解密一个字符串?

发布于 2024-12-09 21:58:56 字数 722 浏览 0 评论 0原文

我正在制作一个简单的程序,它接受在文本框中输入的文本,并接受另一个文本框中的密码,然后对其进行某种简单的加密并将其保存到文件中。之后,用户应该能够再次打开该文件并提供用于加密该文件的密码,并且它应该吐出原始文本。

现在我正在拿绳子。将其分成一个字符数组,然后对密码执行相同的操作。之后,我获取密码,将所有这些字符转换为整数,找到所有字符的平均值,并将其用作原始文本中字符的偏移量。有点像:

textChars[1]= (char)((int)textChars[1]+offset);

然后我可以对加密字符串进行相反的操作:

encryptedChars[1]= (char)((int)encryptedChars[1]-offset);

问题是字符在不同平台上具有不同的值,因此有时偏移量会将字符变成一些疯狂的数字(如负值),这只会将字符变成变成一个问号。

我查看了标准 Java API 中的加密库,但如果每次启动程序时随机生成密钥,我对密钥如何工作感到困惑。

我需要的是两个类似 String encrypt(String text,String Password) 的函数,它吐出用密码加密的文本作为解密的密钥,以及 String crypto(String cryptoText ,字符串密码) 它将吐出原始文本(如果密码是垃圾,则为乱码)

非常感谢任何帮助,这实际上只是一个个人项目,所以我不需要任何花哨的加密方法。

I'm making a simple program that takes text entered in a text box, and takes a password that's in another text box, then does some sort of simple encryption on it and saves it to a file. Afterwards, a user should be able to open up the file again and provide the password that was used to encrypt it and it should spit out the original text.

Right now I'm taking the string. Separate it into a char array, then doing the same for the password. After that, I take the password, convert all those chars to integers, find the average value for all of them, and use it as an offset to the chars int he original text. Kind of like:

textChars[1]= (char)((int)textChars[1]+offset);

Then I can do the reverse for the encrypted string:

encryptedChars[1]= (char)((int)encryptedChars[1]-offset);

The problem is that characters have different values on different platforms so sometimes the offset will turn the char into some crazy number (like a negative value) which will just turn the char into a question mark.

I looked at the crypto library in the standard Java API, but I feel confused as to how the key works if it's just randomly generated every time I start the program.

What I need is two functions that look like String encrypt(String text,String Password) which spits out the text encrypted with the password as a key to decrypting it, and String decrypt(String encryptedText, String Password) which would spit out the original text (or gibberish if the password is junk)

Any help is really appreciated, this is really just a personal project so I don't need any fancy encryption methods.

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

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

发布评论

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

评论(2

拥抱影子 2024-12-16 21:58:56

你正试图重新发明轮子。除非您只是为了好玩,否则我建议您使用 AES 之类的东西。如果您只是搜索“Java 中的 AES”,您会发现许多示例。

如果您这样做是为了好玩并且想要一些简单的实现方式,请查看 ROT13

以下是 Java 中的 AES 示例:

private static final String ALGORITHM = "AES";
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };

 public 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;
}

public String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGORITHM);
    return key;
}

您可能希望改进此代码。

You're trying to re-invent the wheel. Unless you're doing it for fun, I'd recommend using something like AES. If you just google "AES in java" you'll find a number of examples.

If you are doing it for fun and want something simple to implement, have a look at ROT13 as well.

Here's an example for AES in Java:

private static final String ALGORITHM = "AES";
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };

 public 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;
}

public String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGORITHM);
    return key;
}

You may want to improve on this code.

只是我以为 2024-12-16 21:58:56

您真正需要的是对称加密,即算法使用相同的密钥来加密和解密数据。有许多可用的算法支持对称加密,例如 DES、AES。

看一下这个示例: http://www.java2s.com/Code/ Java/Security/EncryptionanddecryptionwithASECBPKCS7Padding.htm

在上面的示例中,替换

byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
    0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

byte[] keyBytes = yourPassword.getBytes();

它使用了 bouncycastle 库,即可以说是最好的可用密码学库。

What you really need is Symmetric cryptography, i.e., the algorithm uses same key to encrypt and decrypt the data. There are many algorithms available which support symmetric cryptography like DES, AES.

Have a look at this example: http://www.java2s.com/Code/Java/Security/EncryptionanddecryptionwithAESECBPKCS7Padding.htm

In the above example, replace

byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
    0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

with

byte[] keyBytes = yourPassword.getBytes();

It uses the bouncycastle library, which is arguably the best cryptography libraries available.

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