java中RSA加解密的实现,最新Android架构师成长路线

java中RSA加解密的实现,最新Android架构师成长路线,第1张

java中RSA加解密的实现,最新Android架构师成长路线

}

public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {

try {

BigInteger b1 = new BigInteger(modulus);

BigInteger b2 = new BigInteger(exponent);

KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);

RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);

return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

public static String encryptByPublicKey(String data, RSAPublicKey publicKey)

throws Exception {

Cipher cipher = Cipher.getInstance(“RSA”);

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

// 模长

int key_len = publicKey.getModulus().bitLength() / 8;

// 加密数据长度 <= 模长-11

String[] datas = splitString(data, key_len - 11);

String mi = “”;

//如果明文长度大于模长-11则要分组加密

for (String s : datas) {

mi += bcd2Str(cipher.doFinal(s.getBytes()));

}

return mi;

}

public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey)

throws Exception {

Cipher cipher = Cipher.getInstance(“RSA”);

cipher.init(Cipher.DECRYPT_MODE, privateKey);

//模长

int key_len = privateKey.getModulus().bitLength() / 8;

byte[] bytes = data.getBytes();

byte[] bcd = ASCII_To_BCD(bytes, bytes.length);

System.err.println(bcd.length);

//如果密文长度大于模长则要分组解密

String ming = “”;

byte[][] arrays = splitArray(bcd, key_len);

for(byte[] arr : arrays){

ming += new String(cipher.doFinal(arr));

}

return ming;

}

public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {

byte[] bcd = new byte[asc_len / 2];

int j = 0;

for (int i = 0; i < (asc_len + 1) / 2; i++) {

bcd[i] = asc_to_bcd(ascii[j++]);

bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));

}

return bcd;

}

public static byte asc_to_bcd(byte asc) {

byte bcd;

if ((asc >= ‘0’) && (asc <= ‘9’))

bcd = (byte) (asc - ‘0’);

else if ((asc >= ‘A’) && (asc <= ‘F’))

bcd = (byte) (asc - ‘A’ + 10);

else if ((asc >= ‘a’) && (asc <= ‘f’))

bcd = (byte) (asc - ‘a’ + 10);

else

bcd = (byte) (asc - 48);

return bcd;

}

public static String bcd2Str(byte[] bytes) {

char temp[] = new char[bytes.length * 2], val;

for (int i = 0; i < bytes.length; i++) {

val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);

temp[i * 2] = (char) (val > 9 ? val + ‘A’ - 10 : val + ‘0’);

val = (char) (bytes[i] & 0x0f);

temp[i * 2 + 1] = (char) (val > 9 ? val + ‘A’ - 10 : val + ‘0’);

}

return new String(temp);

}

public static String[] splitString(String string, int len) {

int x = string.length() / len;

int y = string.length() % len;

int z = 0;

if (y != 0) {

z = 1;

}

String[] strings = new String[x + z];

String str = “”;

for (int i=0; i

if (i==x+z-1 && y!=0) {

str = string.substring(ilen, ilen+y);

}else{

str = string.substring(ilen, ilen+len);

}

strings[i] = str;

}

return strings;

}

public static byte[][] splitArray(byte[] data,int len){

int x = data.length / len;

int y = data.length % len;

int z = 0;

if(y!=0){

z = 1;

}

byte[][] arrays = new byte[x+z][];

byte[] arr;

for(int i=0; i

arr = new byte[len];

if(i==x+z-1 && y!=0){

System.arraycopy(data, i*len, arr, 0, y);

}else{

System.arraycopy(data, i*len, arr, 0, len);

}

最后

题外话,我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在IT学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多程序员朋友无法获得正确的资料得到学习提升,故此将并将重要的Android进阶资料包括自定义view、性能优化、MVC与MVP与MVVM三大框架的区别、NDK技术、阿里面试题精编汇总、常见源码分析等学习资料。

【Android思维脑图(技能树)】

知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。

【Android进阶学习视频】、【全套Android面试秘籍】

希望我能够用我的力量帮助更多迷茫、困惑的朋友们,帮助大家在IT道路上学习和发展

下还是坚持各种整理和分享。但苦于知识传播途径有限,很多程序员朋友无法获得正确的资料得到学习提升,故此将并将重要的Android进阶资料包括自定义view、性能优化、MVC与MVP与MVVM三大框架的区别、NDK技术、阿里面试题精编汇总、常见源码分析等学习资料。

【Android思维脑图(技能树)】

知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。

[外链图片转存中…(img-oYAXIbmD-1643531696732)]

【Android进阶学习视频】、【全套Android面试秘籍】

希望我能够用我的力量帮助更多迷茫、困惑的朋友们,帮助大家在IT道路上学习和发展

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5716484.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存