Java app-password-encrypted backend storage

Source: Internet
Author: User
Tags asymmetric encryption

A simple application, returning from Python to Java, and getting familiar with Java again. In addition, after reading the design pattern some time ago, I quickly forgot about it, I want to design and develop multiple applications.

I. The number of websites that need to be registered is too large, or the number of accounts or accounts is too large. I am afraid that some websites will be transmitted in plain text, and many passwords will be temporary, however, I cannot remember it later, so I want to make such a simple application to locally encrypt the password for RSA, and then save it to the remote storage based on the website domain name and account. It was originally intended to use a cloud note. Later, it took a lot of time to apply for permissions, and the API was relatively complex. In this case, the file, FTP, and Baidu cloud storage methods were used for backend storage. 2. Although the design requirement is very simple, but from the initial consideration of the demand, some designs including the structural level and class level have been done, and finally the implementation has found that many problems cannot be considered, you have to modify the design later. In addition, when you encounter a problem, you may not be able to remember it for the first time. You should pay attention to it later. This function is very clear to remember. It can be divided into three modules: the encryption and decryption module, the backend storage service module, and the front-end display module. The encryption and decryption module and the storage service are independent from each other. The front-end can call interfaces. Therefore, it is very important to consider interfaces. This section describes the design and implementation of the encryption and decryption module, which is basically fixed and has the minimum risk. 1. the encryption and decryption module has two main issues. One is the functions, methods, and parameters that may be used by the front-end, the other is how the underlying implementation can be well received by spring. Encryption and decryption are naturally designed for keys. If you want to adopt symmetric encryption, you need to provide the IV and key signature methods. asymmetric encryption requires the signature of the public and private keys. The class diagram at the beginning is as follows, due to the loss of the IV factor, the subsequent parameter structure is adjusted. A) The key pair is first provided to the front-end interface. From the perspective of the current application, it mainly imports a public or private key in a certain format from outside, A key object is returned. The key format is best provided by the front-end. In fact, it is not feasible to concatenate all backend implementations in the form of a responsible chain. This issue is not important and is not considered as a parameter. Then, this key will definitely need a byte array from the final implementation. Therefore, consider a file first, and then the file path. If it is a byte stream or byte array, it will be more convenient. Therefore, three different import interfaces are provided here, which are passed to the backend through adaptation. One problem is that it was discovered later, that is, the naming problem, which has always plagued me because of poor English ?.. Looks like yes. In short, there are some problems with the method name, and it will be very painful to change it later. Here, the backend implementation first takes into account the abstract factory. Basically, each imported key pair generates the same series. The second thing that comes to mind is that multiple implementations can be replaced with each other, although there is no need to replace each other here, you need to specify a specific format, but later we found that the bridge mode is very suitable for this scenario, dropping all the complex backend implementations to one side and providing interfaces to class adaptation, this makes Spring IoC easier to use. However, it seems that using object adaptation, and then not applying bridging, will not be much complicated. It seems that it is almost the same here, because future changes will not be large, and there will be no advantages, put it first and then consider the scenario. B) after encryption and decryption, it is an interface for encryption and decryption. We also consider what may be transmitted by the front-end. Generally, encryption and decryption are transmitted in byte arrays, but the front-end does not care much about it, A base64 code that is easy to process, or a plaintext string, or a byte array, may be required. Therefore, the interface is provided in a simpler way at the front end. However, many of these parameters are not concerned about the backend implementation. They also use an adapter for a layer of conversion operations. In the end, the underlying implementation is relatively simple. One is non-symmetric, the other is symmetric, and the corresponding implementation can be provided. However, because the problem of IV was initially taken into consideration, it seems awkward to add an IV get method and constructor. Now, it is more appropriate to encapsulate a layer of key, package the key pair object and IV as the key, or change the IV internally, and obtain the IV from the passed encapsulation object. 2. backend storage problems there is nothing to say about the backend storage design. It mainly takes into account the problem of using multiple fields as the key and whether the byte stream in the value is stored in the memory to read and return data. Here we simply provide the implementation part of the policy mode. Multiple backend storage types can be configured. If multiple backend storage is required, you can also add an implementation class such as mixedstore, to forward requests to multiple backend servers. The implementation of backend storage may be more important. 3. frontend problems: this application is different from other applications. It must be encrypted locally before it can be transmitted over the network. That is to say, a single web submission form is not feasible. Of course, if you want to use the web, it can be implemented by combining security controls or Java applets. Here, my personal needs are mostly local clients. Even a simple console terminal command is also quite good, and swing is better. But then I got lazy... Only the console is implemented. 3. implementing enterprise architect ECT can provide UML for code conversion. It is very convenient to design a graphic conversion, and the structure does not need to be re-built. 1. the encryption and decryption module has discussed the import implementation of key pairs. Please go to Java to parse various encoding key pairs (DER, PEM, and OpenSSH public keys) because these three are the most common RSA key pairs, other formats (including OpenSSH private keys, looks like the same as pem format ?) You do not have time to take a closer look. A) JCE block encryption provides AES symmetric mode and RSA Asymmetric mode. If you do not want to use the private key, you can use AES instead, (but the lazy front-end does not implement the AES replacement method.) The problem is that AES itself supports automatic block encryption beyond the size of one block, while RSA does not, however, this has little impact. Once tested, you only need to change the implementation part. In the subsequent implementation, a blockencryption is abstracted and AES is also extracted from the application to control block encryption. This unit test is not tested, and the use case is too simple. B) When block encryption is concerned with block encryption, you also need to note the padding method, which will affect the size of the encrypted content, for example, pkcs1padding requires 11 bytes of reserved information but does not know whether it is related to the length of the key pair. For details about Java support, see the Cipher Algorithm padding section in security guides, it seems that symmetric encryption supports pkcs5padding (8-byte block) and pkcs7padding (1-255 byte unfixed block). asymmetric encryption supports pkcs1padding and OAEP. C) the encryption mode and IV vector are followed by the encryption algorithm mode in the encryption algorithm. See Cipher Algorithm modescbc in security guides. If CBC is used in JCE, if you do not pass the IV, JCE will generate an IV for you. You need to obtain the IV later for decryption. If you do not pay attention to this place, you may find that you cannot decrypt it, and the ECB does not need IV. D) bouncycastle and Maven packaging problems if you need to package dependencies into a jar file in the application, such as executable jar files, you will find that Java occurs when loading BC. lang. securityexception: JCE cannot authenticate the provider BC. This is mainly because the signature is discarded after the package, and then the authentication fails. Currently, my solution is to drop the native bouncycastle package into ext and use the extension class loader to load it. 2. the backend storage module provides a method for listing all the keys for Future Search and get. The value is obtained by using the key, and the value obtained in the application is decrypted by using the private key. The Save method is stored to the remote end through the key and encrypted value. The design scheme is to save in one file. The name is all keys connected by a separator string and the file content is the value. This design facilitates the implementation of all backend services. In this case, the deletion interface may cause a lot of information loss, but it is difficult to provide the interface in this place. It is very difficult to implement the modification later. Empty implementations should be provided first and should not be provided at all. A) easymock unit test because the implementation of backend storage relies on peripheral functions, this can effectively use easymock to remove this dependency. This is not used by many people, I used to use stub in Python in the past. easymock is a bit similar, but there is still a big difference. I also read the easymock source code and used a state mode. First, let's take a note of the state mode. The general implementation is that in record mode, dynamic proxy objects only record the call state, instead of actually calling the proxy object method, compare the record in replay mode. I will have the opportunity to read the source code and write a blog analysis later. B) This Part of the Network Proxy has never been used before. This time, to connect to an intranet FTP, proxy access is required for the first access. For details about how to use the official website, set the system properties for Java networking and proxies, but do not set it to an error. For example, if your proxy type is socks, use HTTP instead. The FTP proxy does not know what it means. I will use the socks proxy to connect to the Intranet FTP server and use the socks attribute. I will study it later. C) Baidu cloud disk API is indeed easy to use. It is much easier than youdao cloud note API. Although Alibaba Cloud Notes SDK is available now, you can try this function later. After applying for a key from Baidu cloud disk, you need to give the user a link to confirm the authorization. Then, the returned Link contains the session ID and other things. The session ID is saved by parsing the link and valid for one month, configure it in the configuration file, and then authenticate and change it later. Then there is a problem with Baidu API, that is, sometimes the network stream is stuck on the read, even if there is no readable returned-1... The specific problem is unclear. It should be a persistent connection on the server side. The reading has not been closed yet. Later, the Length attribute in the header information is obtained first, and then read the specified length and exit reading. 3. The front-end displays how the front-end modules are completely independent, so that you can add more display methods in the future. Use the Spring IoC management implementation class to understand the connectivity issues previously designed with spring. A) password protection for the command line terminal
public static String readPassword(String prompt) {if (System.console() != null)return String.valueOf(System.console().readPassword("[%s]", prompt));Scanner scanner = new Scanner(System.in);System.out.print(prompt);return scanner.next();}

You can use system. Console to implement it, but the eclipse Console does not support it. Adding an unsupported read will not protect it. B) Maven package executable jar

<build><plugins><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><archive><manifest><mainClass>org.csp.client.Console</mainClass></manifest></archive><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id> <!-- this is used for inheritance merges --><phase>package</phase> <!-- bind to the packaging phase --><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>

Here, we can change the mainclass to put all dependencies together. We also need to pay attention to the bouncycastle signature issue mentioned above. C) After Gzip is installed in the configuration file of jar in Linux, you can directly open the jar file with VI, and then press enter on the configuration file to be modified to open the file, save the modification. In this way, you can easily package the configuration file in the jar file. You can avoid the problem of re-packaging when you need to modify the configuration file. D) encoding problem once mentioned in the blog before. The Maven compilation code must be consistent with the project code. Add an attribute to Pom. xml.

<properties><project.build.sourceEncoding>GBK</project.build.sourceEncoding></properties>

E) No log is added to the log project. It is discovered only after the code is written... Fault logs and comments are not much written, and habits are not developed. I have reminded myself many times before. I hope I can get rid of them later. f) the naming of classes and variables is not very good, use temp, or use the first lower case of the class name... Try again. The project is uploaded to GitHub, but it is on the three branches of a project and has three tags. If you are interested, let's take a look. The following link corresponds to the three tags, store/1.0 is the back-end storage module, EDM/1.0 is the encryption and decryption module, CSP-Client/1.0 is a console front-end display https://github.com/zhouyuzhy/java/tagsmaven Eclipse: Eclipse run, use eclipse to import data. Return to the Java field for more information ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.