Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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项目中使用LiB钠/钾_Java_Libsodium - Fatal编程技术网

在java项目中使用LiB钠/钾

在java项目中使用LiB钠/钾,java,libsodium,Java,Libsodium,我正在尝试使用libnaid库对密码进行加密,使用Kalium作为Java转换器。我正在尝试安装它,但遇到了一些问题。我已将Kalium依赖项添加到pom.xml中,并将libsoidum放在我的javapath中,如前所述。现在我真的想用这个库来散列我的密码,然后开始把它们保存到我的数据库中。(我知道oAuth是首选,但这不是软件中的选项。)问题是我不知道如何实际使用包装器。我找不到任何文档或示例。有没有可以帮助我的消息来源?试试这个 import com.muquit.libsodiumjn

我正在尝试使用libnaid库对密码进行加密,使用Kalium作为Java转换器。我正在尝试安装它,但遇到了一些问题。我已将Kalium依赖项添加到pom.xml中,并将libsoidum放在我的javapath中,如前所述。现在我真的想用这个库来散列我的密码,然后开始把它们保存到我的数据库中。(我知道oAuth是首选,但这不是软件中的选项。)问题是我不知道如何实际使用包装器。我找不到任何文档或示例。有没有可以帮助我的消息来源?

试试这个

import com.muquit.libsodiumjna.SodiumLibrary;
import com.muquit.libsodiumjna.exceptions.SodiumLibraryException;
import java.nio.charset.StandardCharsets;

public class Encrypt2 {


private static String libraryPath = "D:/libsodium/libsodium.dll";

private static String ourPassPhrase = "your very secret password";
private static byte[] passPhrase = ourPassPhrase.getBytes();
private static String ourMessage = "password which you want to encrypt and decrypt";
private static byte[] privateKey = (ourMessage.getBytes());


public static void main(String[] args) throws Exception {

    SodiumLibrary.setLibraryPath(libraryPath);
    encrypt();
    decrypt();
}

private static void encrypt() throws SodiumLibraryException {

    System.out.println("----Encrypt-----");

    // The salt (probably) can be stored along with the encrypted data
    byte[] salt = SodiumLibrary.randomBytes(SodiumLibrary.cryptoPwhashSaltBytes());
    byte[] key = SodiumLibrary.cryptoPwhashArgon2i(passPhrase, salt);

    String saltedPrivateKey = new String(key, StandardCharsets.UTF_8);
    System.out.println("saltedPrivateKey bytes - " + saltedPrivateKey);
    /**
     * nonce must be 24 bytes length, so we put int 24 to randomBytes method
     * **/
    byte[] nonce = SodiumLibrary.randomBytes(24);
    byte[] encryptedPrivateKey = SodiumLibrary.cryptoSecretBoxEasy(privateKey, nonce, key);
    String encryptedPW = new String(encryptedPrivateKey, StandardCharsets.UTF_8);
    System.out.println("encryptedPrivateKey bytes  - " + encryptedPW);
}

private static void decrypt() throws SodiumLibraryException {

    System.out.println("----Decrypt-----");

    byte[] salt = SodiumLibrary.randomBytes(SodiumLibrary.cryptoPwhashSaltBytes());
    byte[] key = SodiumLibrary.cryptoPwhashArgon2i(passPhrase, salt);
    byte[] nonce = SodiumLibrary.randomBytes(24);
    byte[] encryptedPrivateKey = SodiumLibrary.cryptoSecretBoxEasy(privateKey, nonce, key);
    privateKey = SodiumLibrary.cryptoSecretBoxOpenEasy(encryptedPrivateKey, nonce, key);

    String dencryptedPW = new String(privateKey, StandardCharsets.UTF_8);
    System.out.println("our secret phrase - " + dencryptedPW);

    }
}

为什么要使用这个库?@Tim我现在使用的是windowsx64.dll。我必须找到某种方法将它移植到heroku(可能使用另一个库),因为最终应用程序将部署在那里;你说你想用Java加密密码;在
libnail
中有什么是你想要使用的,而在其他数百个更容易与Java集成的加密库中是不可用的?@Tim Ah我明白了。我一直在寻找破解密码的方法。我读过的大多数文章/帖子/解释都指向LibNasdium。这就是为什么我想用它。如果你有另一个很好的建议,那也没问题。LibNasdaq似乎使用argon2,这在其他Java库中也可用。否则,bcrypt在各种语言中都得到了很好的支持。