1.凯撒密码
- 通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文
- 明文:原始信息
- 密文:加密后的信息
- 秘钥:加密与解密算法的参数
- 加密算法:以秘钥为参数,对明文进行多种置换和转换的规则和步骤,变换结果为密文
- 解密算法:加密算法逆变换
2.对称加密
- 加密解密用同一个秘钥
- 常用数学运算:
- 移位和循环移位
- 置换
- 扩展
- 压缩
- 异或
- 迭代:多次重复相同运算
- DES算法(Data Encryption Stantard)
public static String encrypt(String clearText, String originalKey)
{
Cipher cipher = Cipher.getInstance("DES");
SecretKeySpec key = getKey(originalKey);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] dofinal = cipher.doFinal(clearText.getBytes());
return new String(dofinal);
}
public static SecretKeySpec getKey(String originalKey)
{
SecretKeySpec key = new SecretKeySpec(originalKey.getBytes(),"DES");
return key;
}
3. 写一个java 加密类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public abstract class MyCipher {
private static final String ALGORITHM = "DES";
private static final String KEY = "12345678";
private static final int N = 31;
private static final Base64.Encoder encoder = Base64.getEncoder();
private static final Base64.Decoder decoder = Base64.getDecoder();
public static byte[] encrypt(byte[] clearByte)
{
SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
Cipher cipher;
try {
cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE,skey);
byte[] dofinal = cipher.doFinal(clearByte);
byte[] encode = encoder.encode(dofinal);
return encode;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] decrypt(byte[] cipherByte)
{
SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
Cipher cipher;
try {
cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,skey);
byte[] decode = decoder.decode(cipherByte);
byte[] dofinal = cipher.doFinal(decode);
return dofinal;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void encryptFile(File source, File target) throws IOException
{
FileInputStream fin = new FileInputStream(source);
FileOutputStream fout = new FileOutputStream(target);
byte[] buffer = new byte[N];
int num = 0;
while((num=fin.read(buffer))!=-1)
{
byte[] temp = Arrays.copyOf(buffer, num);
byte[] encrypted = encrypt(temp);
fout.write(encrypted);
}
fin.close();
fout.close();
}
public static void decryptFile(File source, File target) throws IOException
{
FileInputStream fin = new FileInputStream(source);
FileOutputStream fout = new FileOutputStream(target);
int M = (N/8)*8 + (N%8!=0?8:0);
int Z = (M/3)*4 + (M%3!=0?4:0);
byte[] buffer = new byte[Z];
int num = 0;
while((num=fin.read(buffer))!=-1)
{
byte[] temp = Arrays.copyOf(buffer, num);
byte[] decrypted = decrypt(temp);
fout.write(decrypted);
}
fin.close();
fout.close();
}
private static void printByte(byte[] b, int n)
{
int m = b.length<n?b.length:n;
for(int i=0;i<m;i++)
{
System.out.print(b[i]+" ");
}
System.out.println();
}
public static String encryptString(String clearText)
{
SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
Cipher cipher;
try {
cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE,skey);
byte[] dofinal = cipher.doFinal(clearText.getBytes());
String encode = encoder.encodeToString(dofinal);
return encode;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decryptString(String cipherText)
{
SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
Cipher cipher;
try {
cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,skey);
byte[] decode = decoder.decode(cipherText);
byte[] dofinal = cipher.doFinal(decode);
return new String(dofinal);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}