13种加密与解密算法之RC4篇【三】(rc5算法加密)

网友投稿 662 2022-10-08


13种加密与解密算法之RC4篇【三】(rc5算法加密)

【7、对称之BlowFish】

blowfish加密函数迭代执行16轮,分组长度64位,密钥长度从32位到448位。算法由两部分组成,密钥扩展部分和数据加密部分。加密算法是一种对称的分组加密算法,每次加密一个64位的分组。使用32-448位可变长度的密钥。加密过程分为两个阶段:密钥预处理和信息加密。【对称之 blowfish demo】略!

【8、对称之RC4】

/** * RC4加密 * @param data 需要加密的内容 * @param key 自定义密钥 * @return 加密后的内容 */ public static String encry_RC4_string(String data, String key) { if (data == null || key == null) { return null; } return toHexString(asString(encry_RC4_byte(data, key))); } private static String toHexString(String s) { String str = ""; for (int i = 0; i < s.length(); i++) { int ch = (int) s.charAt(i); String s4 = Integer.toHexString(ch & 0xFF); if (s4.length() == 1) { s4 = '0' + s4; } str = str + s4; } return str;// 0x表示十六进制 } public static byte[] encry_RC4_byte(String data, String key) { if (data == null || key == null) { return null; } byte b_data[] = data.getBytes(); return RC4Base(b_data, key); } private static String asString(byte[] buf) { StringBuffer strbuf = new StringBuffer(buf.length); for (int i = 0; i < buf.length; i++) { strbuf.append((char) buf[i]); } return strbuf.toString(); } private static byte[] RC4Base(byte[] input, String mKkey) { int x = 0; int y = 0; byte key[] = initKey(mKkey); int xorIndex; byte[] result = new byte[input.length]; for (int i = 0; i < input.length; i++) { x = (x + 1) & 0xff; y = ((key[x] & 0xff) + y) & 0xff; byte tmp = key[x]; key[x] = key[y]; key[y] = tmp; xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff; result[i] = (byte) (input[i] ^ key[xorIndex]); } return result; }

【RC4 解密】

/** * RC4解密 * @param data 需要解密的内容 * @param key 自定义密钥 * @return 解密后的内容 */ public static String decry_RC4(String data, String key) { if (data == null || key == null) { return null; } return new String(RC4Base(HexString2Bytes(data), key)); } private static byte[] initKey(String aKey) { byte[] b_key = aKey.getBytes(); byte state[] = new byte[256]; for (int i = 0; i < 256; i++) { state[i] = (byte) i; } int index1 = 0; int index2 = 0; if (b_key == null || b_key.length == 0) { return null; } for (int i = 0; i < 256; i++) { index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff; byte tmp = state[i]; state[i] = state[index2]; state[index2] = tmp; index1 = (index1 + 1) % b_key.length; } return state; } private static byte[] HexString2Bytes(String src) { int size = src.length(); byte[] ret = new byte[size / 2]; byte[] tmp = src.getBytes(); for (int i = 0; i < size / 2; i++) { ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); } return ret; } private static byte uniteBytes(byte src0, byte src1) { char _b0 = (char) Byte.decode("0x" + new String(new byte[] { src0 })).byteValue(); _b0 = (char) (_b0 << 4); char _b1 = (char) Byte.decode("0x" + new String(new byte[] { src1 })).byteValue(); byte ret = (byte) (_b0 ^ _b1); return ret; }

【对称之RC4小 demo】

String rc4_str = encry_RC4_string(str, "单位对我的风范"); System.out.println("对称之RC4加密后的内容" + rc4_str); System.out.println("对称之RC4解密后的内容" + decry_RC4(rc4_str, "单位对我的风范"));

【9、对称之RC5加密解密】

RC5加密算法有四种:1、原始RC5块加密,rc5密码使用固定的输入长度,使用一个依赖密钥的转换产生一个固定的输出块。2、RC5-CBC,是RC5的块密码链接模式。3、RC5-CBC-Pad,处理任意长度的明文。4、RC5-CTS是RC5加密算法的密文挪用模式,处理任意长度的明文且密文的长度匹配明文的长度。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:13种加密与解密算法【四】(加密算法和解密算法都需要保密)
下一篇:在Java中使用日志框架log4j的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~