如何使用另一个字符串作为密码加密/解密字符串? [英] How do I encrypt/decrypt a string with another string as a password?

查看:251
本文介绍了如何使用另一个字符串作为密码加密/解密字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



现在我拿字符串,将它分成一个char数组,然后对密码进行同样的操作。然后我获取密码,将所有的字符转换为整数,找到所有的平均值,然后使用它作为原始文本的字符的偏移量。类似的:textChars [1] =(char)((int)textChars [1] + offset);



然后我可以对加密字符串做相反的操作:encryptedChars [1] =(char)((int)encryptedChars [1] -offset);
问题是字符在不同的平台上有不同的值,所以有时偏移量会把char转换成一个疯狂的数字(例如一个负值),这将把字符变成一个问号。



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

$ b $我需要的是两个函数看起来像字符串加密(字符串文本,字符串密码),它吐出用密码加密的密钥作为解密它的密钥,和String decrypt(String encryptedText,String密码),它会吐出原始的文本(如果密码是垃圾,则是乱码)



任何帮助真的很感激,这只是一个个人项目,



提前感谢
Georgiy S。

解决方案

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



看看这个例子: http://www.java2s.com/Code/Java/Security/EncryptionanddecryptionwithAESECBPKCS7Padding.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库,这可以说是最好的加密库。


I'm making a simple program that takes that takes text entered in a text box, then 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, separating it into a char array, then doing the same for the password. Then I take the password, convert all thee chars to integers, find the average value for all of them, then 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.

Thanks in advance, Georgiy S.

解决方案

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.

这篇关于如何使用另一个字符串作为密码加密/解密字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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