Source Code Cross Referenced for RSABlindedEngine.java in  » Security » Bouncy-Castle » org » bouncycastle » crypto » engines » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » Bouncy Castle » org.bouncycastle.crypto.engines 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.crypto.engines;
002:
003:        import org.bouncycastle.crypto.AsymmetricBlockCipher;
004:        import org.bouncycastle.crypto.CipherParameters;
005:        import org.bouncycastle.crypto.DataLengthException;
006:        import org.bouncycastle.crypto.params.ParametersWithRandom;
007:        import org.bouncycastle.crypto.params.RSAKeyParameters;
008:        import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
009:
010:        import java.math.BigInteger;
011:        import java.security.SecureRandom;
012:
013:        /**
014:         * this does your basic RSA algorithm with blinding
015:         */
016:        public class RSABlindedEngine implements  AsymmetricBlockCipher {
017:            private static BigInteger ZERO = BigInteger.valueOf(0);
018:
019:            private RSACoreEngine core = new RSACoreEngine();
020:            private RSAKeyParameters key;
021:            private SecureRandom random;
022:
023:            /**
024:             * initialise the RSA engine.
025:             *
026:             * @param forEncryption true if we are encrypting, false otherwise.
027:             * @param param the necessary RSA key parameters.
028:             */
029:            public void init(boolean forEncryption, CipherParameters param) {
030:                core.init(forEncryption, param);
031:
032:                if (param instanceof  ParametersWithRandom) {
033:                    ParametersWithRandom rParam = (ParametersWithRandom) param;
034:
035:                    key = (RSAKeyParameters) rParam.getParameters();
036:                    random = rParam.getRandom();
037:                } else {
038:                    key = (RSAKeyParameters) param;
039:                    random = new SecureRandom();
040:                }
041:            }
042:
043:            /**
044:             * Return the maximum size for an input block to this engine.
045:             * For RSA this is always one byte less than the key size on
046:             * encryption, and the same length as the key size on decryption.
047:             *
048:             * @return maximum size for an input block.
049:             */
050:            public int getInputBlockSize() {
051:                return core.getInputBlockSize();
052:            }
053:
054:            /**
055:             * Return the maximum size for an output block to this engine.
056:             * For RSA this is always one byte less than the key size on
057:             * decryption, and the same length as the key size on encryption.
058:             *
059:             * @return maximum size for an output block.
060:             */
061:            public int getOutputBlockSize() {
062:                return core.getOutputBlockSize();
063:            }
064:
065:            /**
066:             * Process a single block using the basic RSA algorithm.
067:             *
068:             * @param in the input array.
069:             * @param inOff the offset into the input buffer where the data starts.
070:             * @param inLen the length of the data to be processed.
071:             * @return the result of the RSA process.
072:             * @exception DataLengthException the input block is too large.
073:             */
074:            public byte[] processBlock(byte[] in, int inOff, int inLen) {
075:                if (key == null) {
076:                    throw new IllegalStateException(
077:                            "RSA engine not initialised");
078:                }
079:
080:                if (key instanceof  RSAPrivateCrtKeyParameters) {
081:                    RSAPrivateCrtKeyParameters k = (RSAPrivateCrtKeyParameters) key;
082:
083:                    if (k.getPublicExponent() != null) // can't do blinding without a public exponent
084:                    {
085:                        BigInteger input = core.convertInput(in, inOff, inLen);
086:                        BigInteger m = k.getModulus();
087:                        BigInteger r = calculateR(m);
088:                        BigInteger result = core.processBlock(r.modPow(
089:                                k.getPublicExponent(), m).multiply(input)
090:                                .mod(m));
091:
092:                        return core.convertOutput(result.multiply(
093:                                r.modInverse(m)).mod(m));
094:                    }
095:
096:                    return core.convertOutput(core.processBlock(core
097:                            .convertInput(in, inOff, inLen)));
098:                } else {
099:                    return core.convertOutput(core.processBlock(core
100:                            .convertInput(in, inOff, inLen)));
101:                }
102:            }
103:
104:            /*
105:             * calculate a random mess-with-their-heads value.
106:             */
107:            private BigInteger calculateR(BigInteger m) {
108:                int max = m.bitLength() - 1; // must be less than m.bitLength()
109:                int min = max / 2;
110:                int length = ((random.nextInt() & 0xff) * ((max - min) / 0xff))
111:                        + min;
112:                BigInteger factor = new BigInteger(length, random);
113:
114:                while (factor.equals(ZERO)) {
115:                    factor = new BigInteger(length, random);
116:                }
117:
118:                return factor;
119:            }
120:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.