Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用另一个字符串作为密码加密/解密字符串?_Java_String_Cryptography_Encryption - Fatal编程技术网

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

Java 如何使用另一个字符串作为密码加密/解密字符串?,java,string,cryptography,encryption,Java,String,Cryptography,Encryption,我正在制作一个简单的程序,它接收文本框中输入的文本,并获取另一个文本框中的密码,然后对其进行某种简单的加密,并将其保存到一个文件中。之后,用户应该能够再次打开该文件,并提供用于加密该文件的密码,并且该文件应该能够吐出原始文本 现在我要承担责任。将其分离为一个字符数组,然后对密码执行相同的操作。之后,我获取密码,将所有字符转换为整数,找到所有字符的平均值,并将其用作原始文本中字符的偏移量。有点像: textChars[1]= (char)((int)textChars[1]+offset); 然

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

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

textChars[1]= (char)((int)textChars[1]+offset);
然后我可以对加密字符串执行相反的操作:

encryptedChars[1]= (char)((int)encryptedChars[1]-offset);
问题是字符在不同的平台上有不同的值,所以有时候偏移量会将字符变成一些疯狂的数字(比如负值),这只会将字符变成问号

我查看了标准JavaAPI中的加密库,但如果每次启动程序时都随机生成密钥,我会对密钥的工作方式感到困惑

我需要的是两个类似于
String encrypt(String text,String Password)
的函数,它们将使用密码加密的文本作为密钥进行解密,而
String decrypt(String encryptedText,String Password)
将吐出原始文本(如果密码是垃圾,则会发出乱码)


非常感谢您的帮助,这只是一个个人项目,因此我不需要任何奇特的加密方法。

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

看看这个例子:

在上面的示例中,替换

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


它使用bouncycastle库,这可以说是目前最好的加密库。

您正在尝试重新发明轮子。除非你是为了好玩,否则我建议你用类似的东西。如果你在谷歌上搜索“java中的AES”,你会发现很多例子

如果您是为了好玩而做的,并且希望实现一些简单的东西,那么也可以看看

以下是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;
}

您可能需要改进这段代码。

哦,是的,MD5是我读整篇文章时无法满足的一种方式:)这段代码的可能副本似乎是迄今为止最有希望的解决方案,我将尝试一下。@Shahzeb java2s有什么问题吗?我个人认为网站上的例子是一个很好的开始,而不是从头开始。你不应该直接使用密码作为密钥-你应该首先使用密钥拉伸。这是完全不安全的。您需要使用类似PBKDF2的方法从密码中派生密钥。否则,密码的熵很低,很容易被人猜到,而且你们不想使用ECB。至少使用CBC或者更好的AEAD模式。好吧,我使用了您提供的代码,但改用DES,因为我不知道AES需要多少字符,而且我真的不关心强保护。谢谢你的帮助!大多数加密算法工作于字节,而不是字符和字符串。根据AES规范,密钥应为128、192或256位。要将字符串用作键,请检查以下内容:
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;
}