ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Android-Java RSA解密

2019-10-10 03:06:23  阅读:12  来源: 互联网

标签:android java rsa public-key



我需要从二进制字符串为RSA算法创建一个公共密钥.

我的代码是:

String pubKey = "tihq/Gk3OUs5NzP+XTRKXBwSxHtB0TWn0RREcpXEtp316tyD9DzKaIbdKexb/mRr";

byte[] keyBytes = Base64.decode(pubKey,Base64.DEFAULT);
//test if is correct: ok
Log.d("response keyBytes",new String(Base64.encode(keyBytes,Base64.DEFAULT)));

KeyFactory keyFactory = KeyFactory.getInstance("RSA");      
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);

RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(spec);
//PublicKey publicKey = (PublicKey) keyFactory.generatePublic(spec);

但是我有这个错误:

java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

解决方法:

解决了.
问题是我有模数和指数,所以我必须这样做:

String pubKey = "tihq/Gk3OUs5NzP+XTRKXBwSxHtB0TWn0RREcpXEtp316tyD9DzKaIbdKexb/mRr"; //64 caratteri
String exponent = "AQAB";

byte[] keyBytes = Base64.decode(pubKey,Base64.DEFAULT);
byte[] exponentByte = Base64.decode(exponent,Base64.DEFAULT);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(keyBytes), new BigInteger(exponentByte));
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);


标签:android,java,rsa,public-key

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有