如何使用 java 中的一些唯一密钥加密随机值
How to encrypt random value using some unique key in java
您好,我想使用 Java 中的密钥加密一个唯一的随机值。我会将这个唯一的随机值发送到每个网络服务以确保系统安全,这样就没有人可以在其他客户端上访问我的网络服务 url。
请指导我实现此目标的方法。
提前致谢。
该问题有两种解决方案:
综上所述,第一;您通过套接字加密数据(通过逆向工程,如蛮力,您可以破解用于加密的密码)。第二;使用 SSL(安全套接字层)。我已经使用了第一种解决方案,那么我可以为您详细介绍如何实施。你在这里:
1- 有一些 API 可以帮助您做到这一点。我用了 jasypt a time ago, and I recommend. But there are others too; like bouncy castle.
通常,它们很容易实现。在 jasypt 中,你可以这样解决这个问题,只需 运行 test:
public class SecurityUtil {
private static String passEncrypt;
/*
* Get the message encrypted
* @param String string to encrypt
* @return encrypted message
*/
public static String Encryptor(String message){
SecurityUtil.testEncryptPassSet();
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(SecurityUtil.passEncrypt);
String encryptedText = encryptor.encrypt(message);
return encryptedText;
}
/*
* Get the decrypt message
* @param encrypted message
* @return String decrypted message
*
*/
public static String Decryptor(String message) {
SecurityUtil.testEncryptPassSet();
StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
decryptor.setPassword(SecurityUtil.passEncrypt);
String decryptedText = decryptor.decrypt(message);
return decryptedText;
}
/*
* set the encryption password
*/
public static void setPassEncrypt(String passEncrypt) {
SecurityUtil.passEncrypt = passEncrypt;
}
public static void testEncryptPassSet() {
if (SecurityUtil.passEncrypt == null){
System.out.println("Must set the password after");
}
}
public static void main (String[] args){
SecurityUtil.setPassEncrypt("Test"); //here you key
String encrypted;
System.out.println("Encrypted: "+(encrypted = SecurityUtil.Encryptor("This is a test message")));
System.out.println("Decryp: "+SecurityUtil.Decryptor(encrypted));
}
}
输出:
加密:eESU3c2IzRSl2VvHs4Otyh+Q3aBisiP6XPfyKpbXMdQ=
解密:这是一条测试消息
2- 您可以研究如何通过套接字实现 SSL here. Also, here are some examples. And here 我们在 Whosebug 中有一个类似主题的问题。
您好,我想使用 Java 中的密钥加密一个唯一的随机值。我会将这个唯一的随机值发送到每个网络服务以确保系统安全,这样就没有人可以在其他客户端上访问我的网络服务 url。
请指导我实现此目标的方法。
提前致谢。
该问题有两种解决方案:
综上所述,第一;您通过套接字加密数据(通过逆向工程,如蛮力,您可以破解用于加密的密码)。第二;使用 SSL(安全套接字层)。我已经使用了第一种解决方案,那么我可以为您详细介绍如何实施。你在这里:
1- 有一些 API 可以帮助您做到这一点。我用了 jasypt a time ago, and I recommend. But there are others too; like bouncy castle.
通常,它们很容易实现。在 jasypt 中,你可以这样解决这个问题,只需 运行 test:
public class SecurityUtil {
private static String passEncrypt;
/*
* Get the message encrypted
* @param String string to encrypt
* @return encrypted message
*/
public static String Encryptor(String message){
SecurityUtil.testEncryptPassSet();
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(SecurityUtil.passEncrypt);
String encryptedText = encryptor.encrypt(message);
return encryptedText;
}
/*
* Get the decrypt message
* @param encrypted message
* @return String decrypted message
*
*/
public static String Decryptor(String message) {
SecurityUtil.testEncryptPassSet();
StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
decryptor.setPassword(SecurityUtil.passEncrypt);
String decryptedText = decryptor.decrypt(message);
return decryptedText;
}
/*
* set the encryption password
*/
public static void setPassEncrypt(String passEncrypt) {
SecurityUtil.passEncrypt = passEncrypt;
}
public static void testEncryptPassSet() {
if (SecurityUtil.passEncrypt == null){
System.out.println("Must set the password after");
}
}
public static void main (String[] args){
SecurityUtil.setPassEncrypt("Test"); //here you key
String encrypted;
System.out.println("Encrypted: "+(encrypted = SecurityUtil.Encryptor("This is a test message")));
System.out.println("Decryp: "+SecurityUtil.Decryptor(encrypted));
}
}
输出:
加密:eESU3c2IzRSl2VvHs4Otyh+Q3aBisiP6XPfyKpbXMdQ=
解密:这是一条测试消息
2- 您可以研究如何通过套接字实现 SSL here. Also, here are some examples. And here 我们在 Whosebug 中有一个类似主题的问题。