Source Code Cross Referenced for SimpleStringEncrypter.java in  » GIS » openjump » com » vividsolutions » jump » util » 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 » GIS » openjump » com.vividsolutions.jump.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


01:        package com.vividsolutions.jump.util;
02:
03:        import java.util.*;
04:
05:        /**
06:         * Provides a simple encyrption/decryption mechanism for ASCII string values.
07:         * The algorithm does not provide strong encryption, but serves
08:         * as a way of obfuscating the value of short strings (such as passwords).
09:         * The code symbol set is drawn from the set of printable ASCII symbols.
10:         * The encrypted strings are longer than the clear text (roughly double in length).
11:         * A random element is used, so that different encryptions of the same clear
12:         * text will result in different encodings.
13:         */
14:        public class SimpleStringEncrypter {
15:            private static final String INVALID_CODE_STRING_MSG = "Invalid code string";
16:
17:            private static String codeSymbols = "ABCDEFGHIJKLMNOP"
18:                    + "abcdefghijklmnop" + "QRSTUVWXYZ012345"
19:                    + "qrstuvwxyz6789@$";
20:
21:            /**
22:             * Creates a new encrypter
23:             */
24:            public SimpleStringEncrypter() {
25:            }
26:
27:            /**
28:             * Encrypts a string.
29:             *
30:             * @param clearText the string to encrypt
31:             * @return the encryted code
32:             */
33:            public String encrypt(String clearText) {
34:                char[] code = new char[clearText.length() * 2];
35:                for (int i = 0; i < clearText.length(); i++) {
36:                    char c = clearText.charAt(i);
37:                    setEncryptedSymbol(c, code, 2 * i);
38:                }
39:                return new String(code);
40:            }
41:
42:            public void setEncryptedSymbol(char c, char[] code, int i) {
43:                int charVal = c;
44:                int nibble0 = charVal & 0xf;
45:                int nibble1 = charVal >> 4;
46:
47:                code[i] = encodeNibble(nibble0);
48:                code[i + 1] = encodeNibble(nibble1);
49:            }
50:
51:            private char encodeNibble(int val) {
52:                int random4 = (int) (4 * Math.random());
53:                int randomOffset = 16 * random4;
54:                return codeSymbols.charAt(val + randomOffset);
55:            }
56:
57:            /**
58:             * Decrypts a code string.
59:             *
60:             * @param codeText the code to decrypt
61:             * @return the clear text for the code
62:             *
63:             * @throws IllegalArgumentException if the code string is invalid
64:             */
65:            public String decrypt(String codeText) {
66:                if (codeText.length() % 2 != 0)
67:                    throw new IllegalArgumentException(INVALID_CODE_STRING_MSG);
68:                char[] clear = new char[codeText.length() / 2];
69:                for (int i = 0; i < codeText.length() / 2; i++) {
70:                    char symbol0 = codeText.charAt(2 * i);
71:                    char symbol1 = codeText.charAt(2 * i + 1);
72:                    clear[i] = decryptedChar(symbol0, symbol1);
73:                }
74:                return new String(clear);
75:            }
76:
77:            private char decryptedChar(char symbol0, char symbol1) {
78:                int nibble0 = decodeNibble(symbol0);
79:                int nibble1 = decodeNibble(symbol1);
80:                int charVal = nibble1 << 4 | nibble0;
81:                return (char) charVal;
82:            }
83:
84:            private int decodeNibble(int symbolValue) {
85:                int nibbleValue = codeSymbols.indexOf(symbolValue);
86:                if (nibbleValue < 0)
87:                    throw new IllegalArgumentException(INVALID_CODE_STRING_MSG);
88:                return nibbleValue % 16;
89:            }
90:
91:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.