//字符转换成ASCII 码数值 char charA = 'a'; int intA = charA; //char 强转为int 即得到对应的ASCII 码值,'a'的值为97 //ASCII 码值转成char int intA = 97;//97 对应的ASCII 码'a' char charA = (char) intA; //int 值强转为char 即得到对应的ASCII 字符,即'a'
/**
* 加密
* @param input 数据源(需要加密的数据)
* @param key 秘钥,即偏移量
* @return 返回加密后的数据
*/
public static String encrypt(String input, int key) {
//得到字符串里的每一个字符
char[] array = input.toCharArray();
for (int i = 0; i < array.length; ++i) {
//字符转换成ASCII 码值
int ascii = array[i];
//字符偏移,例如a->b
ascii = ascii + key;
//ASCII 码值转换为char
char newChar = (char) ascii;
//替换原有字符
array[i] = newChar;
//以上4 行代码可以简写为一行
//array[i] = (char) (array[i] + key);
}
//字符数组转换成String
return new String(array);
}
/**
* 解密
* @param input 数据源(被加密后的数据)
* @param key 秘钥,即偏移量
* @return 返回解密后的数据
*/
public static String decrypt(String input, int key) {
//得到字符串里的每一个字符
char[] array = input.toCharArray();
for (int i = 0; i < array.length; ++i) {
//字符转换成ASCII 码值
int ascii = array[i];
//恢复字符偏移,例如b->a
ascii = ascii - key;
//ASCII 码值转换为char
char newChar = (char) ascii;
//替换原有字符
array[i] = newChar;
//以上4 行代码可以简写为一行
//array[i] = (char) (array[i] - key);
}
//字符数组转换成String
return new String(array);
}
/**
* 频率分析法破解凯撒密码
*/
public class FrequencyAnalysis {
//英文里出现次数最多的字符
private static final char MAGIC_CHAR = 'e';
//破解生成的最大文件数
private static final int DE_MAX_FILE = 4;
public static void main(String[] args) throws Exception {
//测试1,统计字符个数
//printCharCount("article1_en.txt");
//加密文件
//int key = 3;
//encryptFile("article1.txt", "article1_en.txt", key);
//读取加密后的文件
String artile = file2String("article1_en.txt");
//解密(会生成多个备选文件)
decryptCaesarCode(artile, "article1_de.txt");
}
public static void printCharCount(String path) throws IOException{
String data = file2String(path);
List<Entry<Character, Integer>> mapList = getMaxCountChar(data);
for (Entry<Character, Integer> entry : mapList) {
//输出前几位的统计信息
System.out.println("字符'" + entry.getKey() + "'出现" + entry.getValue() + "次");
}
}
public static void encryptFile(String srcFile, String destFile, int key) throws IOException {
String artile = file2String(srcFile);
//加密文件
String encryptData = MyEncrypt.encrypt(artile, key);
//保存加密后的文件
string2File(encryptData, destFile);
}
/**
* 破解凯撒密码
* @param input 数据源
* @return 返回解密后的数据
*/
public static void decryptCaesarCode(String input, String destPath) {
int deCount = 0;//当前解密生成的备选文件数
//获取出现频率最高的字符信息(出现次数越多越靠前)
List<Entry<Character, Integer>> mapList = getMaxCountChar(input);
for (Entry<Character, Integer> entry : mapList) {
//限制解密文件备选数
if (deCount >= DE_MAX_FILE) {
break;
}
//输出前几位的统计信息
System.out.println("字符'" + entry.getKey() + "'出现" + entry.getValue() + "次");
++deCount;
//出现次数最高的字符跟MAGIC_CHAR的偏移量即为秘钥
int key = entry.getKey() - MAGIC_CHAR;
System.out.println("猜测key = " + key + ", 解密生成第" + deCount + "个备选文件" + "\n");
String decrypt = MyEncrypt.decrypt(input, key);
String fileName = "de_" + deCount + destPath;
string2File(decrypt, fileName);
}
}
//统计String里出现最多的字符
public static List<Entry<Character, Integer>> getMaxCountChar(String data) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
char[] array = data.toCharArray();
for (char c : array) {
if(!map.containsKey(c)) {
map.put(c, 1);
}else{
Integer count = map.get(c);
map.put(c, count + 1);
}
}
//输出统计信息
/*for (Entry<Character, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "出现" + entry.getValue() + "次");
}*/
//获取获取最大值
int maxCount = 0;
for (Entry<Character, Integer> entry : map.entrySet()) {
//不统计空格
if (/*entry.getKey() != ' ' && */entry.getValue() > maxCount) {
maxCount = entry.getValue();
}
}
//map转换成list便于排序
List<Entry<Character, Integer>> mapList = new ArrayList<Map.Entry<Character,Integer>>(map.entrySet());
//根据字符出现次数排序
Collections.sort(mapList, new Comparator<Entry<Character, Integer>>(){
@Override
public int compare(Entry<Character, Integer> o1,
Entry<Character, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
return mapList;
}
public static String file2String(String path) throws IOException {
FileReader reader = new FileReader(new File(path));
char[] buffer = new char[1024];
int len = -1;
StringBuffer sb = new StringBuffer();
while ((len = reader.read(buffer)) != -1) {
sb.append(buffer, 0, len);
}
return sb.toString();
}
public static void string2File(String data, String path){
FileWriter writer = null;
try {
writer = new FileWriter(new File(path));
writer.write(data);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
int i = 97; String bit = Integer.toBinaryString(i); //输出:97 对应的二进制数据为: 1100001 System.out.println(i + "对应的二进制数据为: " + bit);
//byte 的取值范围:-128 到127 System.out.println(Byte.MIN_VALUE + "到" + Byte.MAX_VALUE);
//1,得到cipher 对象(可翻译为密码器或密码系统)
Cipher cipher = Cipher.getInstance("DES");
//2,创建秘钥
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
//3,设置操作模式(加密/解密)
cipher.init(Cipher.ENCRYPT_MODE, key);
//4,执行操作
byte[] result = cipher.doFinal("黑马".getBytes());
//生成随机秘钥
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
//序列化秘钥到磁盘上
FileOutputStream fos = new FileOutputStream(new File("heima.key"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(secretKey);
//从磁盘里读取秘钥
FileInputStream fis = new FileInputStream(new File("heima.key"));
ObjectInputStream ois = new ObjectInputStream(fis);
Key key = (Key) ois.readObject();
//创建密钥写法1 KeySpec keySpec = new DESKeySpec(key.getBytes()); SecretKey secretKey = SecretKeyFactory.getInstance(ALGORITHM). generateSecret(keySpec); //创建密钥写法2 //SecretKey secretKey = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); //得到key 后,后续代码就是Cipher 的写法,此处省略...
//秘钥算法 private static final String KEY_ALGORITHM = "DES"; //加密算法:algorithm/mode/padding 算法/工作模式/填充模式 private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding"; //秘钥 private static final String KEY = "12345678";//DES 秘钥长度必须是8 位或以上 //private static final String KEY = "1234567890123456";//AES 秘钥长度必须是16 位 //初始化秘钥 SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); //加密 cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] result = cipher.doFinal(input.getBytes());
//AES、DES 在CBC 操作模式下需要iv 参数 IvParameterSpec iv = new IvParameterSpec(key.getBytes()); //加密 cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有