Advertisement
forextheblack

RSA

Nov 23rd, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. private int e;
  2. private BigInteger n;
  3. private BigInteger d;
  4. private BigInteger p;
  5. private BigInteger q;
  6. private BigInteger dmp1;
  7. private BigInteger dmq1;
  8. private BigInteger coeff;
  9.  
  10. private RSA(final BigInteger n, final int e, final BigInteger d, final BigInteger p, final BigInteger q, final BigInteger dmp1, final BigInteger dmq1, final BigInteger coeff){
  11. this.n = n;
  12. this.e = e;
  13. this.d = d;
  14. this.p = p;
  15. this.q = q;
  16. this.dmp1 = dmp1;
  17. this.dmq1 = dmq1;
  18. this.coeff = coeff;
  19. }
  20.  
  21. private int GetBlockSize() {
  22. return ((this.n.bitCount() + 7) / 8);
  23. }
  24.  
  25. private byte[] pkcs1unpad(final BigInteger _arg1, final int _arg2) {
  26. byte[] bytes = _arg1.toByteArray();
  27. int i = 0;
  28.  
  29. while (i < bytes.length && bytes[i] == 0) ++i;
  30. if (bytes.length - i != (_arg2 - 1) || bytes[i] != 0x2)
  31. {
  32. return null;
  33. }
  34. i++;
  35. while(bytes[i] != 0) {
  36. ++i;
  37. if(i >= bytes.length) {
  38. return null;
  39. }
  40. }
  41. byte[] result = new byte[bytes.length - i + 1];
  42.  
  43. int p = 0;
  44. while(++i < bytes.length) {
  45. result[p++] = bytes[i];
  46. }
  47.  
  48. return result;
  49. }
  50.  
  51. public BigInteger doPrivate2(final BigInteger k) {
  52. return k.modPow(this.d, this.n);
  53. }
  54.  
  55. public byte[] Decript(String Key) {
  56. BigInteger m = new BigInteger(Key, 16);
  57. BigInteger c = this.doPrivate2(m);
  58.  
  59. if (c.bitLength() == 0)
  60. {
  61. return null;
  62. }
  63.  
  64. byte[] bytes = this.pkcs1unpad(c, this.GetBlockSize());
  65.  
  66. if (bytes.length == 0)
  67. {
  68. return null;
  69. }
  70.  
  71. return bytes;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement