您的位置:首页 > 编程语言 > Java开发

Java中使用hex string类型的RSA密钥加解密测试

2015-09-17 17:08 721 查看
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;

public class Main
{
/**
* 主函数
* @param args
*/
public static void main(String[] args)
{
try
{
String N = "c28e0ffa96fa48571dd31b3ea6ff7aa22e8b9ef28e6fc72fa0b731864685c922"
+  "ca27faa3a3e543991dca178820c224763e48a6c4fff7ecaa40065905c738fd18"
+  "80a93b74c2380bd0dcb79144e1e79fc621031a8316014961578b5f81a82b7071"
+  "dd65d440066d10655d91a1493f947e7e5f774b836c058754a17f131f755df783"
+  "582f0e21cd451dc50c8b652089671b1d9c71f2cf6755f38656a115ac3e644376"
+  "febf8f35f09e47bf8e4a2c8fb02a92bca9a026797186695e549e29656d768a9c"
+  "6df8fc3f37f0a7daf614028ada9c36c7f0c93c975b1e7642e39fdda050dae230"
+  "cc61d263c438abd033812a37767f35f84328b8666975c267ad8150eed367eab7";

String E = "00010001"; // hex:00010001 decimal: 65537 base64: AQAB

String D = "79cb3b5f91925f25024bbcfb8cc9d4b8a0d0d111616fc24f239a6b4b76ec9bcc"
+  "c6a71e75c0cd6e72f53e255b17bed1daa0051539b050417d1715a23746cf7b4a"
+  "12895eea2a07b205ef968f3f82f8608244fa4f678ea8018b09a5fb850c851d20"
+  "7b0c1b427583634741bb402fbdb8b533618a29e0bd07fcff53165d1f4d7724d1"
+  "a1e2b395b7f7ab1b79790848bb83d04db1325d643d97ace063378f9a850e41e0"
+  "cc2a696b4c6cad89a94009a2816451210140769a530e5d1117f802d6f3d3c446"
+  "d7c88a07dff9be278dabb6c97b8019101bcec8ad7329aa6f4d3b5b677df5634f"
+  "3ea828b18939337b367f5a8968471cb1f7dfec2f587f20e7c6141f8caf72d7a9";

PublicKey  pubKey = createPublicKey(N, E);
PrivateKey priKey = createPrivateKey(N, D);

//原文
final String text = "cly";
System.out.println("message: " + text);

//加密
byte[] cipher = encrypt(text, pubKey);
System.out.println("cipher length: "+ cipher.length);

//解密(解密时的cipherbyte长度不能大于256)
String plain = decrypt(cipher, priKey);
System.out.println("plain: " + plain);
}
catch(Exception e)
{
e.printStackTrace();
}
}

/**
* 从hex string生成公钥
* @param stringN
* @param stringE
* @return 构造好的公钥
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static PublicKey createPublicKey(String stringN, String stringE)
throws NoSuchAlgorithmException, InvalidKeySpecException
{
try
{
BigInteger N = new BigInteger(stringN,16); // hex base
BigInteger E = new BigInteger(stringE,16); // hex base

RSAPublicKeySpec spec = new RSAPublicKeySpec(N, E);
KeyFactory kf = KeyFactory.getInstance("RSA");
return  kf.generatePublic(spec);
}
catch(Exception e)
{
e.printStackTrace();
}

return null;
}

/**
* 从hex string 生成私钥
* @param stringN
* @param stringD
* @return 构造好的私钥
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static PrivateKey createPrivateKey(String stringN, String stringD)
throws NoSuchAlgorithmException, InvalidKeySpecException
{
try
{
BigInteger N = new BigInteger(stringN,16); // hex base
BigInteger D = new BigInteger(stringD,16); // hex base

RSAPrivateKeySpec spec = new RSAPrivateKeySpec(N, D);
KeyFactory kf = KeyFactory.getInstance("RSA");
return  kf.generatePrivate(spec);
}
catch(Exception e)
{
e.printStackTrace();
}

return null;
}

/**
* 用公钥加密信息
* @param message
* @param key
* @return 加密后的密文
*/
public static byte[] encrypt(String message, PublicKey key)
{
try
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] data = cipher.doFinal(message.getBytes());
return data;
}
catch(Exception e)
{
e.printStackTrace();
}
return new byte[1];
}

/**
* 用私钥解密信息
* @param cipherText
* @param key
* @return 解密后的明文
*/
public static String decrypt(byte[] cipherText, PrivateKey key)
{
try
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] data = cipher.doFinal(cipherText);
return new String(data);
}
catch(Exception e)
{
e.printStackTrace();
}
return new String("");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: