java 加密API AES RSA MD5 SHA-1 SHA-256
程序员文章站
2022-03-25 21:26:56
...
AES ----
public static String aesEncode(String message, String key) {
try {
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"),"AES");
c.init(Cipher.ENCRYPT_MODE,secretKeySpec);
byte[] bytes = c.doFinal(message.getBytes("UTF-8"));
byte[] encode = Base64.getEncoder().encode(bytes);
return new String(encode,"UTF-8");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public static String aesDecode(String message, String key) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"),"AES");
cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(message.getBytes("UTF-8")));
return new String(bytes,"UTF-8");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
RSA -----
private static String rsaEncode(String message,PublicKey key) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] bytes = cipher.doFinal(message.getBytes());
byte[] encode = Base64.getEncoder().encode(bytes);
System.out.println(new String(encode));
return new String(encode);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
public static String decode(String message, PrivateKey key){
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(message.getBytes()));
return new String(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
MD5 SHA-1 SHA-256 —
private static String md5Encode(String message) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] digest = messageDigest.digest(message.getBytes());
StringBuilder builder = byte2Hex(digest);
System.out.println(builder);
return builder.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
private static String sha1Encode(String message){
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] digest = messageDigest.digest(message.getBytes());
StringBuilder builder = byte2Hex(digest);
System.out.println(builder);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
private static String sha128Encode(String message){
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] digest = messageDigest.digest(message.getBytes());
StringBuilder builder = byte2Hex(digest);
System.out.println(builder);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
private static StringBuilder byte2Hex(byte[] digest) {
StringBuilder builder = new StringBuilder();
for (byte b : digest) {
String format = String.format("%02x", b);
builder.append(format);
}
return builder;
}