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

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

问题描述

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



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

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

然后我可以对加密的字符串做相反的处理:

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

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



我看了标准Java API中的加密库,但是我感到困惑关于如何在每次启动程序时随机生成密钥的工作原理。



我需要的是两个类似于 String加密的函数(String text,String Password)将用密码加密的文本作为密钥解密,并且 String decrypt(String encryptedText,String Password)它会吐出原始文本(或者如果密码是垃圾,就会乱码)



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

解决方案

你真正需要的是Sy即密码术,即该算法使用相同的密钥对数据进行加密和解密。有许多可用的算法支持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 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.

解决方案

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天全站免登陆