Java uses HashMap to encrypt and decrypt strings

Source: Internet
Author: User
Tags decrypt
Package P3;
Import Java.util.HashMap;

Import Java.util.Map;
   public class Encryption {//String str;
   Static map<character,character> map=new hashmap<character,character> ();
    Static Map<character,character>map1=new hashmap<character,character> ();
        Encryption () {String a= "abcdefghijklmnopqrstuvwsvz*";
        String b= "Veknohzf*iljxdmygbrcswqupta";
            for (int i=0;i<a.length (); i++) {Map.put (A.charat (i), B.charat (i));
        Map1.put (B.charat (i), A.charat (i));
       } public static string encrypt (String str) {string s2= str.tolowercase ();
       String d = "";
       for (int i = 0; i < s2.length (); i++) {d = d + map.get (S2.charat (i));
    return D;
        public static string decrypt (String str) {string d = "";
        for (int i = 0; i < str.length (); i++) {d = d + map1.get (Str.charat (i));
    return D;
 }
}
The    entire method is loaded into the class, it needs to be encrypted and decrypted just call
Encryption e1=new encryption ();
String stre= "Hello*world";
String Stre1=e1.encrypt (stre);
System.out.println (stre1);
System.out.println (E1.decrypt (stre1));
The program used for testing
Output results:
Fojjmaqmbjn
Hello*world
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.