/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package blowfish;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
/**
2. * BlowFish的加密和復元的样本
3. */
public class BlowFish {
public static final String ENCODING = "UTF-8";
// 32bit - 128bit (448bit)
public static final String TEST_KEY = "*********";
/**
12. * 把byte数组变换为16進数的字符串。
13. *
14. * @param bytes
15. * byte数组
16. * @return 16進数的字符串
17. */
public static String byteToString(byte[] bytes) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
int d = bytes[i];
if (d < 0) {
d += 256;
}
if (d < 16) {
buf.append("0");
}
buf.append(Integer.toString(d, 16));
}
return buf.toString();
}
/**
37. * 把16進数的字符串变换为byte数组。
38. *
39. * @param string
40. * 16進数的字符串
41. * @return byte数组
42. */
public static byte[] stringToByte(String string) {
byte[] bytes = new byte[string.length() / 2];
String b;
for (int i = 0; i < string.length() / 2; i++) {
b = string.substring(i * 2, i * 2 + 2);
bytes[i] = (byte) Integer.parseInt(b, 16);
}
return bytes;
}
/**
57. * 返回把字符串用暗号键加密的byte数组。
58. *
59. * @param key
60. * 暗号键
61. * @param text
62. * 加密的字符串
63. * @return 把字符串加密的byte数组
64. * @throws NoSuchAlgorithmException
65. * @throws NoSuchPaddingException
66. * @throws InvalidKeyException
67. * @throws IllegalBlockSizeException
68. * @throws BadPaddingException
69. * @throws UnsupportedEncodingException
70. */
public static byte[] encrypt(String key, String text) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), "Blowfish");
// BlowFish的加密mode是ECB、Padding方式是PKCS5Padding。
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, sksSpec);
byte[] encrypted = cipher.doFinal(text.getBytes(ENCODING));
return encrypted;
}
/**
84. * 返回加密的byte数组用暗号键復元的字符串。
85. *
86. * @param key
87. * 暗号键
88. * @param encrypted
89. * 加密的byte数组
90. * @return
91. * @throws NoSuchAlgorithmException
92. * @throws NoSuchPaddingException
93. * @throws InvalidKeyException
94. * @throws IllegalBlockSizeException
95. * @throws BadPaddingException
96. * @throws UnsupportedEncodingException
97. */
public static String decrypt(String key, byte[] encrypted) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), "Blowfish");
// BlowFish的加密mode是ECB、Padding方式是PKCS5Padding。
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, sksSpec);
byte[] decrypted = cipher.doFinal(encrypted);
return new String(decrypted, ENCODING);
}
/**
111. * 从命令行开始的使用方法
112. */
private static void usage() {
System.out.println("Usage: java BlowFish <encode|decode> [key] <text>");
System.exit(-1);
}
/**
120. * <pre>
121. *
122. * Test
123. * <pre>
124. */
public static void main(String[] args) {
String key;
String text;
if (args.length < 2) {
usage();
}
if (args.length == 2) {
key = TEST_KEY;
text = args[1];
} else {
key = args[1];
text = args[2];
}
String type = args[0].toLowerCase();
try {
if (type.equals("encode")) {
System.out.println("Key = " + key);
System.out.println("Text = " + text);
System.out.println("Encrypted = " + byteToString(encrypt(key, text)));
} else if (type.equals("decode")) {
System.out.println("Key = " + key);
System.out.println("Text = " + text);
System.out.println("Decrypted = " + decrypt(key, stringToByte(text)));
} else {
usage();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
对称加密算法Blowfish(java).rar (11个子文件)
Blowfish
test
manifest.mf 85B
src
blowfish
BlowFish.java 5KB
build
classes
blowfish
BlowFish.class 3KB
.netbeans_automatic_build 0B
build.xml 4KB
nbproject
build-impl.xml 42KB
private
private.xml 211B
private.properties 121B
project.properties 2KB
project.xml 516B
genfiles.properties 475B
共 11 条
- 1
资源评论
yinhao223
- 粉丝: 37
- 资源: 81
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功