Encryption Java7中的加密
在Java7中,我想使用 SHA-256和AES-256。 我试着使用带有HMACSHA256的PBKDF2,但这在Java7中是不受支持的。 你有什么想法吗?请告诉我图书馆的信息 谢谢 示例(Java8) 公共类PasswordUtil{ 私有静态最终字符串算法=“PBKDF2WithHmacSHA256”; 私有静态最终整数迭代计数=10000; 私有静态最终整数密钥长度=256; /** * *@param密码 *@param盐 *@返回 */ 公共静态字符串getSafetyPassword(字符串密码,字符串salt){ char[]passCharAry=password.toCharArray(); 字节[]hashedSalt=getHashedSalt(salt); PBEKeySpec keySpec=新的PBEKeySpec(passCharAry、hashedsate、迭代计数、密钥长度); 分泌键工厂; 试一试{ skf=SecretKeyFactory.getInstance(算法); }捕获(无算法异常){ 抛出新的运行时异常(e); } 秘钥秘钥; 试一试{ secretKey=skf.generateSecret(keySpec); }捕获(InvalidKeySpece异常){ 抛出新的运行时异常(e); } 字节[]passByteAry=secretKey.getEncoded(); StringBuilder sb=新的StringBuilder(64); for(字节b:passByteAry){ sb.append(字符串格式(“%02x”,b&0xff)); } 使某人返回字符串(); } /** * *@param盐 *@返回 */ 私有静态字节[]getHashedSalt(字符串salt){ 消息摘要消息摘要; 试一试{ messageDigest=messageDigest.getInstance(“SHA-256”); }捕获(无算法异常){ 抛出新的运行时异常(e); } update(salt.getBytes()); 返回messageDigest.digest(); } }Encryption Java7中的加密,encryption,java-7,Encryption,Java 7,在Java7中,我想使用 SHA-256和AES-256。 我试着使用带有HMACSHA256的PBKDF2,但这在Java7中是不受支持的。 你有什么想法吗?请告诉我图书馆的信息 谢谢 示例(Java8) 公共类PasswordUtil{ 私有静态最终字符串算法=“PBKDF2WithHmacSHA256”; 私有静态最终整数迭代计数=10000; 私有静态最终整数密钥长度=256; /** * *@param密码 *@param盐 *@返回 */ 公共静态字符串getSafetyPasswo
在JAVA 7中使用AES对密码进行加密和解密 Encryptionsss.java::
public class Encryptionsss {
public static void main(String[] args) throws Exception {
try {
String text = "Hello World";
String key = "1234567891234567";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted text: " + new String(encrypted));
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.out.println("Decrypted text: " + decrypted);
}catch(Exception e) {
e.printStackTrace();
}
String plainText = "Hello World";
/**
* Generate new Key
*/
// String str = generatenewkeyasString();
/*** Generate Cipher Text from Key(We are using same key stored in String-str)
****/
String str = "]˜??4I-S@æ,Ôt";
byte[] data = str.getBytes();
SecretKey key2 = new SecretKeySpec(data, 0, data.length, "AES");
byte[] cipherText = encryptText(plainText, key2);
String scipherText = new String(cipherText);
/**
*
* Decrypt Cipher Text with Key****/
cipherText = scipherText.getBytes();
String decryptedText = decryptText(cipherText, key2);
System.out.println("ScipherText:" + scipherText);
System.out.println("Original Text:" + plainText);
System.out.println("AES Key (Hex Form):"
+ bytesToHex(key2.getEncoded()));
System.out.println("Encrypted Text (Hex Form):"
+ bytesToHex(cipherText));
System.out.println("Descrypted Text:" + decryptedText);
}
/**
*
* @return byte[] as String
* @Generate Key
*/
private static String generatenewkeyasString() throws Exception {
SecretKey secKey = KeyGenerator.getInstance("AES").generateKey();
byte[] data = secKey.getEncoded();
String str = new String(data);
return str;
}
/**
*
* Encrypts plainText in AES using the secret key
*
* @param plainText
*
* @param secKey
*
* @return
*
* @throws Exception
*/
public static byte[] encryptText(String plainText, SecretKey secKey)
throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
return byteCipherText;
}
/**
*
* Decrypts encrypted byte array using the key used for encryption.
*
* @param byteCipherText
* @param secKey
*
* @return
*
* @throws Exception
*/
public static String decryptText(byte[] byteCipherText, SecretKey secKey)
throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
return new String(bytePlainText);
}
/**
*
* Convert a binary byte array into readable hex form
*
* @param hash
*
* @return
*/
private static String bytesToHex(byte[] hash) {
return DatatypeConverter.printHexBinary(hash);
}
}为什么要使用基于密码的密钥派生函数?你想用密码加密密码吗?对不起,我想用盐加密密码。请看上面的例子。我找到了解决方案。谢谢
public class Encryptionsss {
public static void main(String[] args) throws Exception {
try {
String text = "Hello World";
String key = "1234567891234567";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted text: " + new String(encrypted));
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.out.println("Decrypted text: " + decrypted);
}catch(Exception e) {
e.printStackTrace();
}
String plainText = "Hello World";
/**
* Generate new Key
*/
// String str = generatenewkeyasString();
/*** Generate Cipher Text from Key(We are using same key stored in String-str)
****/
String str = "]˜??4I-S@æ,Ôt";
byte[] data = str.getBytes();
SecretKey key2 = new SecretKeySpec(data, 0, data.length, "AES");
byte[] cipherText = encryptText(plainText, key2);
String scipherText = new String(cipherText);
/**
*
* Decrypt Cipher Text with Key****/
cipherText = scipherText.getBytes();
String decryptedText = decryptText(cipherText, key2);
System.out.println("ScipherText:" + scipherText);
System.out.println("Original Text:" + plainText);
System.out.println("AES Key (Hex Form):"
+ bytesToHex(key2.getEncoded()));
System.out.println("Encrypted Text (Hex Form):"
+ bytesToHex(cipherText));
System.out.println("Descrypted Text:" + decryptedText);
}
/**
*
* @return byte[] as String
* @Generate Key
*/
private static String generatenewkeyasString() throws Exception {
SecretKey secKey = KeyGenerator.getInstance("AES").generateKey();
byte[] data = secKey.getEncoded();
String str = new String(data);
return str;
}
/**
*
* Encrypts plainText in AES using the secret key
*
* @param plainText
*
* @param secKey
*
* @return
*
* @throws Exception
*/
public static byte[] encryptText(String plainText, SecretKey secKey)
throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
return byteCipherText;
}
/**
*
* Decrypts encrypted byte array using the key used for encryption.
*
* @param byteCipherText
* @param secKey
*
* @return
*
* @throws Exception
*/
public static String decryptText(byte[] byteCipherText, SecretKey secKey)
throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
return new String(bytePlainText);
}
/**
*
* Convert a binary byte array into readable hex form
*
* @param hash
*
* @return
*/
private static String bytesToHex(byte[] hash) {
return DatatypeConverter.printHexBinary(hash);
}