标签:lse har standard 获得 使用 lis tab row utf-8
加密和解密使用同一个密钥,例如WinRAR。
WinRAR在对文件进行打包的时候,可以设置一个密码,在解压的时候需要使用同样的密码才能正确的解压。
加密:encrypt(key,message) -> s
解密:decrypt(key,s) -> message
算法 | 密钥长度 | 工作模式(参数) | 填充模式(格式的选择) |
---|---|---|---|
DES | 56/64 | ECB, CBC, PCBC, CTR... | NoPadding, PKCS5Padding |
AES | 128/192/256 | ECB,CBC,PCBC,CTR... | NoPadding,PKCS5Padding, PKCS7Padding |
IDEA | 128 | ECB | PKCS5Padding, PKCS7Padding |
package com.testList;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Base64;
public class SplitString {
//指定工作模式
static final String CIPHER_NAME = "AES/ECB/PKCS5Padding";
//加密
public static byte[] encrypt(byte[] key,byte[] input) throws GeneralSecurityException {
//传入加密算法的名字,获得Cipher实例
Cipher cipher = Cipher.getInstance(CIPHER_NAME);
//传入key的byte数组,转化为AES的key
SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
//初始化为加密模式,传入key
cipher.init(cipher.ENCRYPT_MODE,keySpec);
//传入加密的内容,获取加密后的数组
return cipher.doFinal(input);
}
//解密,传入key,加密的输入
public static byte[] decrypt(byte[] key,byte[] input) throws GeneralSecurityException{
//获得一个cipher实例
Cipher cipher = Cipher.getInstance(CIPHER_NAME);
//
SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
cipher.init(cipher.DECRYPT_MODE,keySpec);
return cipher.doFinal(input);
}
public static void main(String[] args) throws Exception{
//原文
String message = "Hello world!encrypted using AES!";
System.out.println("原文:"+message);
//128位密钥=16bytes key
byte[] key = "1234567890abcdef".getBytes("UTF-8");
byte[] data = message.getBytes(StandardCharsets.UTF_8);
//加密
byte[] encrypted = encrypt(key,data);
System.out.println("Encryped data:"+Base64.getEncoder().encodeToString(encrypted));
//解密
byte[] decrypted = decrypt(key,encrypted);
System.out.println("Decrypt data:"+new String(decrypted,"UTF-8"));
}
}
标签:lse har standard 获得 使用 lis tab row utf-8
原文地址:https://www.cnblogs.com/csj2018/p/10847053.html