算法:RSA/ECB/PKCS1Padding
========================java==========================================================
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import
java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
public class RSATest {
String e = "65537";
String n =
"123410773237385713572440712840019405878257600213906351775134402766524785605776353635515879438375969303333340691224323217379791619946464100287854264933660919378485000299257054039555887477610831829409144592603086784397675690934246422666689022312589317493002336070775714030955737435316659994026756956753416169929";
String d =
"102299672961750099570264341757280532275542401837445663770632643616494888426681205847125069294737520917541038900032845310395266178574112466427178094767628391398378924953787043659322148498169017989730953803803890989295625028193153480552247210183981959942844345362118479513739632952091865360095551345350474614233";
@Test
public void testDecrypt() {
BigInteger bn = new BigInteger(n);
BigInteger be = new BigInteger(e);
BigInteger bd = new BigInteger(d);
RSAPublicKeySpec pub = new RSAPublicKeySpec(bn, be);
RSAPrivateKeySpec prv = new RSAPrivateKeySpec(bn, bd);
try {
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pubkey = kf.generatePublic(pub);
PrivateKey prvkey = kf.generatePrivate(prv);
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//加密
rsa.init(Cipher.ENCRYPT_MODE, pubkey);
String txt = "Welcome,RSA!from 博客园";
byte[] encdata = rsa.doFinal(txt.getBytes());
System.out.printf("RSA加密:\n明文:%s\n密文:%s\n", txt,
convertToBase64(encdata));
//解密
rsa.init(Cipher.DECRYPT_MODE, prvkey);
|