java加密关键字判断
时间: 2023-08-24 15:08:33 浏览: 50
Java中的加密关键字有很多种,常见的包括:AES、DES、RSA、MD5、SHA等。判断一个字符串是否包含这些关键字,可以使用正则表达式进行匹配,示例代码如下:
```java
String input = "这是一个包含AES加密关键字的字符串";
boolean containsAES = input.matches(".*\\bAES\\b.*");
System.out.println(containsAES);
```
上述代码中,使用正则表达式`.*\bAES\b.*`匹配字符串中是否包含单词`AES`,如果包含则返回`true`,否则返回`false`。同理,可以使用正则表达式匹配其他加密关键字。
相关问题
java判断文件是否有加密
可以使用Java的IO流读取两个文件的内容,然后逐个字符进行比较,如果有任何一个字符不同,就说明两个文件内容不完全相同。具体实现可以参考以下代码:
```java
import java.io.*;
public class CompareFiles {
public static void main(String[] args) {
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try {
FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);
int b1, b2;
boolean flag = true;
while ((b1 = fis1.read()) != -1 && (b2 = fis2.read()) != -1) {
if (b1 != b2) {
flag = false;
break;
}
}
if (flag && fis1.read() == -1 && fis2.read() == -1) {
System.out.println("两个文件内容完全相同");
} else {
System.out.println("两个文件内容不完全相同");
}
fis1.close();
fis2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这段代码首先创建了两个File对象,分别表示要比较的两个文件。然后使用FileInputStream读取文件内容,逐个字符进行比较,如果有任何一个字符不同,就将flag标记为false,表示两个文件内容不完全相同。最后判断两个文件是否都已经读取完毕,如果都已经读取完毕且flag为true,就说明两个文件内容完全相同。
aes java加密
AES(Advanced Encryption Standard)是一种常用的对称加密算法,可以用数据的加密和解密。在Java中,你可以使用Java加密扩展(Java Cryptography Extension,JCE)中的AES算法来进行加密。
下面是一个使用AES算法进行加密的Java代码示例:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class AESExample {
public static void main(String[] args) {
try {
String plaintext = "Hello, world!";
String encryptionKey = "ThisIsASecretKey";
// 生成AES密钥
SecretKey secretKey = generateKey(encryptionKey);
// 加密
String encryptedText = encrypt(plaintext, secretKey);
System.out.println("Encrypted Text: " + encryptedText);
// 解密
String decryptedText = decrypt(encryptedText, secretKey);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SecretKey generateKey(String encryptionKey) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom(encryptionKey.getBytes());
keyGenerator.init(128, secureRandom);
return keyGenerator.generateKey();
}
public static String encrypt(String plaintext, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
}
```
在上面的示例中,我们使用了128位的AES密钥,并对字符串"Hello, world!"进行加密和解密。加密后的结果以Base64编码的形式打印出来。
请注意,这个示例只是为了演示AES加密算法的使用,实际应用中需要注意密钥的安全存储和传输等问题。